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