#include <bits/stdc++.h>
using namespace std;
double PI = acos(-1);
double EPS = 1e-7;
int INF = 1000000000;
int MOD = 1000000007;
int MAXINT = 2147483647;
long long INFLL = 1000000000000000000LL;
long long MAXLL = 9223372036854775807LL;
int mx[8] = {-1, 1, 0, 0, -1, -1, 1, 1};
int my[8] = {0, 0, -1, 1, -1, 1, -1, 1};
int x[100005];
int main() {
  int n;
  long long sum = 0;
  scanf("%d", &n);
  for (int(a) = (1); (a) <= (n); (a)++) {
    scanf("%d", &x[a]);
    sum += x[a];
  }
  sum /= n;
  long long ans = 0;
  long long sisa = 0;
  queue<pair<int, int> > st;
  for (int(a) = (1); (a) <= (n); (a)++) {
    x[a] = x[a] - sum;
    if (x[a] > 0) st.push(make_pair(x[a], a));
  }
  for (int(a) = (1); (a) <= (n); (a)++) {
    if (x[a] < 0) {
      x[a] = -x[a];
      while (x[a] && !st.empty()) {
        int take = min(x[a], st.front().first);
        st.front().first -= take;
        x[a] -= take;
        ans += (long long)take * abs(st.front().second - a);
        if (st.front().first == 0) st.pop();
      }
    }
  }
  cout << ans << endl;
}
