#include <bits/stdc++.h>
using namespace std;
deque<pair<int, int>> need_more;
long long a[100000];
int n;
int main() {
  scanf("%d", &n);
  long long sum = 0;
  for (int i = 0; i < n; i++) {
    scanf("%I64d", &a[i]);
    sum += a[i];
  }
  long long need = sum / n;
  long long ans = 0;
  long long extra = 0;
  for (int i = 0; i < n; i++) {
    if (extra != 0) {
      ans += extra;
      a[i] += extra;
      extra = a[i] - need;
      if (extra < 0) {
        need_more.push_back(make_pair(i, -extra));
        extra = 0;
      }
    } else {
      while (!need_more.empty() && a[i] > 0) {
        pair<int, int> cur = need_more.front();
        need_more.pop_front();
        long long mini = min((long long)cur.second, a[i]);
        a[i] -= mini;
        cur.second -= mini;
        ans += (i - cur.first) * 1LL * mini;
        if (cur.second > 0) need_more.push_front(cur);
      }
      if (a[i] > need) {
        extra += a[i] - need;
      } else if (a[i] < need) {
        need_more.push_back(make_pair(i, need - a[i]));
      }
    }
  }
  while (need_more.size() > 0) {
    pair<int, int> cur = need_more.front();
    need_more.pop_front();
    ans += (n - 1 - cur.first) * 1LL * (cur.second);
  }
  printf("%I64d", ans);
  return 0;
}
