#include <bits/stdc++.h>
int main() {
  long long n, i, k = 0, min = 0, temp = 0;
  long long a[1000000] = {0};
  scanf("%I64d", &n);
  for (i = 0; i < n; i++) {
    scanf("%I64d", &a[i]);
    k += a[i];
  }
  k = k / n;
  for (i = 0; i < n; i++) {
    if (temp < 0)
      min += temp * -1;
    else
      min += temp;
    temp += a[i] - k;
  }
  printf("%I64d", min);
  return 0;
}
