#include <bits/stdc++.h>
using namespace std;
long long arr[50001];
int main() {
  long long n;
  cin >> n;
  long long sum = 0;
  for (int i = 0; i < n; i++) {
    cin >> arr[i];
    sum += arr[i];
  }
  long long res = 0;
  long long one = sum / n;
  for (int i = 0; i < n; i++) {
    if (arr[i] == one) continue;
    if (arr[i] < one) {
      res += (one - arr[i]);
      arr[i + 1] -= (one - arr[i]);
      arr[i] = one;
    } else {
      res += (arr[i] - one);
      arr[i + 1] += (arr[i] - one);
      arr[i] = one;
    }
  }
  cout << res << endl;
  return 0;
}
