#include <bits/stdc++.h>
using namespace std;
const int N = 50 * 1000 + 10;
long long a[N];
int main() {
  long long n;
  long long sum = 0, ans = 0;
  cin >> n;
  for (long long i = 0; i < n; i++) {
    cin >> a[i];
    sum += a[i];
  }
  sum /= n;
  long long t = 1;
  for (long long i = 0; i < n; i++) {
    if (a[i] > sum) {
      ans += a[i] - sum;
      a[i + 1] += a[i] - sum;
      a[i] = sum;
    }
    if (a[i] < sum) {
      for (t = max(t, i + 1); t < n && a[i] < sum; t++) {
        long long need = min(a[t] - sum, sum - a[i]);
        a[t] -= need;
        a[i] += need;
        ans += need * (t - i);
      }
    }
  }
  cout << ans;
}
