#include <bits/stdc++.h>
using namespace std;
int main(int argc, char *argv[]) {
  long long int n, t;
  cin >> n;
  long long int sum = 0, m = 0, q = 0, r = 0;
  long long int *a = (long long int *)malloc(sizeof(long long int) * n);
  for (long long int i = 0; i < n; i++) {
    cin >> a[i];
    r = r + a[i];
    q = q + r / n;
    r = r % n;
  }
  t = q;
  q = t;
  for (long long int i = 0; i < n - 1; i++) {
    long long int j = t - a[i];
    a[i + 1] = a[i + 1] - j;
    a[i] = t;
    m += (j > 0 ? j : (0 - j));
  }
  cout << m;
}
