#include <bits/stdc++.h>
using namespace std;
int main() {
  int N;
  cin >> N;
  long long A[50001];
  long long total = 0;
  for (int i = 0; i < N; ++i) {
    cin >> A[i];
    total += A[i];
  }
  long long x = total / N;
  long long res = 0;
  long long bal = 0;
  for (int i = 0; i < N; ++i) {
    res += abs(bal);
    bal += x - A[i];
  }
  cout << res << endl;
  return 0;
}
