#include <bits/stdc++.h>
using namespace std;
int main() {
  long long int a[1000000], b = 0, c = 0, d = 0;
  long long int n, i;
  cin >> n;
  for (i = 1; i <= n; i++) {
    cin >> a[i];
    d += a[i];
  }
  d = d / n;
  for (i = 1; i <= n; i++) {
    b += a[i] - d;
    c += abs(b);
  }
  cout << c << endl;
  return 0;
}
