#include <bits/stdc++.h>
using namespace std;
const int maxn = (int)1e6;
const int inf = (int)1e9;
const int mod = (int)1e9 + 7;
const double eps = 1e-9;
long long n, a[maxn], cnt, d, cur, ans;
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(0);
  cin >> n;
  for (int i = 1; i <= n; ++i) {
    cin >> a[i];
    cnt += a[i];
  }
  d = cnt / n;
  int pos = 1;
  for (int i = 1; i <= n; ++i) {
    while (a[i] < d) {
      while (pos < n && a[pos] <= d) pos++;
      cur = min(d - a[i], a[pos] - d);
      a[i] += cur;
      a[pos] -= cur;
      ans += abs(pos - i) * cur;
    }
  }
  cout << ans;
  return 0;
}
