#include <bits/stdc++.h>
using namespace std;
long long a[50005];
int main() {
  memset(a, 0, sizeof(a));
  long long n;
  cin >> n;
  int d;
  long long sum = 0;
  for (int i = 0; i < n; i++) {
    cin >> a[i];
    sum += (long long)a[i];
  }
  sum /= n;
  long long ans = 0;
  long long pre = 0;
  for (int i = 0; i < n; i++) {
    if (a[i] > sum) {
      a[i + 1] += a[i] - sum;
      ans += (long long)(a[i] - sum);
    } else if (a[i] < sum) {
      a[i + 1] -= sum - a[i];
      ans += sum - a[i];
    } else
      continue;
  }
  cout << ans << endl;
  return 0;
}
