#include <bits/stdc++.h>
using namespace std;
inline void inp(long long int &n) {
  n = 0;
  int ch = getchar();
  int sign = 1;
  while (ch < '0' || ch > '9') {
    if (ch == '-') sign = -1;
    ch = getchar();
  }
  while (ch >= '0' && ch <= '9')
    n = (n << 3) + (n << 1) + ch - '0', ch = getchar();
  n = n * sign;
}
char _;
int main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  long long int n;
  long long int sum = 0;
  cin >> n;
  long long int arr[n];
  for (long long int i = 0; i < n; i++) {
    cin >> arr[i];
    sum += arr[i];
  }
  long long int ans1 = 0;
  sum /= n;
  long long int now = 0;
  for (long long int i = 0; i < n; i++) {
    now += arr[i] - sum;
    if (now > 0) {
      ans1 += now;
    } else {
      ans1 -= now;
    }
  }
  cout << ans1;
  return 0;
}
