#include <bits/stdc++.h>
using namespace std;
long long ar[1000005];
int main() {
  long long n;
  cin >> n;
  long long a;
  long long ans = 0, s = 0;
  for (long long i = 0; (i) < (n); ++i) {
    cin >> ar[i];
    s += ar[i];
  }
  a = s / n;
  for (long long i = 0; (i) < (n - 1); ++i) {
    if (ar[i] > a) {
      ar[i + 1] += ar[i] - a;
      ans += ar[i] - a;
      ar[i] = a;
    } else if (ar[i] < a) {
      ar[i + 1] -= a - ar[i];
      ans += a - ar[i];
      ar[i] = a;
    }
  }
  cout << ans << endl;
}
