#include <bits/stdc++.h>
using namespace std;
int main() {
  long long int t, sum = 0, l, m;
  cin >> t;
  long long int* a = new long long int[t];
  for (long long int i = 0; i < t; i++) {
    cin >> a[i];
    sum += a[i];
  }
  l = sum % t;
  if (l == 0) {
    l = sum / t;
    sum = 0;
    for (long long int i = 0; i < t - 1; i++) {
      if (a[i] < l) {
        m = l - a[i];
        sum += m;
        a[i] += m;
        a[i + 1] -= m;
      }
      if (a[i] > l) {
        sum += a[i] - l;
        a[i + 1] += a[i] - l;
        a[i] -= l;
      }
    }
    cout << sum;
  } else {
    printf("0");
  }
}
