#include <bits/stdc++.h>
using namespace std;
int main() {
  long long n, i, j, ans = 0;
  cin >> n;
  long long avg = 0, a[n], b[n];
  for (i = 0; i < n; i++) {
    cin >> a[i];
    avg += a[i];
  }
  avg /= n;
  for (i = 0; i < n; i++) {
    a[i] = avg - a[i];
    if (a[i] < 0)
      b[i] = -a[i];
    else
      b[i] = 0;
  }
  i = 0, j = 0;
  while (i < n && j < n) {
    while (i < n && a[i] <= 0) ++i;
    while (j < n && b[j] == 0) ++j;
    if (i == n || j == n) break;
    if (a[i] >= b[j]) {
      ans += b[j] * abs(i - j);
      a[i] -= b[j];
      b[j] = 0;
    } else {
      ans += a[i] * abs(i - j);
      b[j] -= a[i];
      a[i] = 0;
    }
  }
  cout << ans;
  return 0;
}
