#include <bits/stdc++.h>
using namespace std;
template <typename T>
T abs(T x) {
  return x > 0 ? x : -x;
}
long long a[50005];
long long n, i, j, cn, ans = 0, s;
int main() {
  cin >> n;
  for (i = 1; i <= n; i++) {
    scanf("%I64d", &a[i]);
    s += a[i];
  }
  s /= n;
  i = 1;
  j = 1;
  while (i <= n && j <= n) {
    while (i <= n && a[i] >= s) i++;
    while (j <= n && a[j] <= s) j++;
    if (i <= n && j <= n) {
      cn = min(s - a[i], a[j] - s);
      ans += cn * abs(i - j);
      a[i] += cn;
      a[j] -= cn;
    }
  }
  cout << ans;
  return 0;
}
