#include <bits/stdc++.h>
using namespace std;
int main() {
  long long div, sum = 0, arr[50005], count = 0, diff;
  unsigned int n;
  cin >> n;
  unsigned int i;
  for (i = 0; i < n; i++) {
    scanf("%I64d", &arr[i]);
    sum += arr[i];
  }
  div = sum / n;
  sum = 0;
  for (i = 0; i < n; i++) {
    diff = arr[i] - div;
    arr[i + 1] += diff;
    count += abs(diff);
  }
  printf("%I64d", count);
  return 0;
}
