#include <bits/stdc++.h>
using namespace std;
long long int N, A[50005], top, son, fark[50005];
long long int md(long long int a) { return (a < 0) ? -a : a; }
int main() {
  int i, j;
  cin >> N;
  for (i = 0; i < N; i++) {
    cin >> A[i];
    top += A[i];
  }
  top = top / N;
  for (i = 0; i < N; i++) fark[i] = top - A[i];
  for (i = 0; i < N; i++) {
    son += md(fark[i]);
    fark[i + 1] += fark[i];
  }
  cout << son << endl;
  return 0;
}
