#include <bits/stdc++.h>
using namespace std;
const int INF = 1000000000 + 7;
long long ans, sum, a[50000 + 10];
int n;
int main() {
  ({
    int neg = 0;
    n = 0;
    char ch;
    for (ch = getchar(); ch < '0' || ch > '9'; ch = getchar())
      if (ch == '-') neg = 1 - neg;
    n = ch - 48;
    for (ch = getchar(); ch >= '0' && ch <= '9'; ch = getchar())
      n = (n << 3) + (n << 1) + ch - 48;
    if (neg) n = -n;
  });
  sum = 0;
  for (int i = (1), _b = (n); i <= _b; i++)
    ({
      int neg = 0;
      a[i] = 0;
      char ch;
      for (ch = getchar(); ch < '0' || ch > '9'; ch = getchar())
        if (ch == '-') neg = 1 - neg;
      a[i] = ch - 48;
      for (ch = getchar(); ch >= '0' && ch <= '9'; ch = getchar())
        a[i] = (a[i] << 3) + (a[i] << 1) + ch - 48;
      if (neg) a[i] = -a[i];
    }),
        sum += a[i];
  sum /= n;
  ans = 0;
  for (int i = (1), _b = (n); i <= _b; i++) {
    if (a[i] > sum) {
      ans += a[i] - sum;
      a[i + 1] += (a[i] - sum);
    } else if (a[i] < sum) {
      ans += (sum - a[i]);
      a[i + 1] -= (sum - a[i]);
    }
  }
  cout << ans;
}
