#include <bits/stdc++.h>
int main(void) {
  int n, i;
  long long a[50001] = {0}, average = 0, ans = 0, d, b[50001] = {0};
  scanf("%d", &n);
  i = 1;
  while (i <= n) scanf("%I64d", a + i), b[i] = a[i], average += a[i++];
  average /= n;
  for (i = 1; i < n; i++) {
    d = a[i] - average;
    a[i + 1] += d;
    if (d >= 0)
      ans += d;
    else
      ans += -d;
  }
  for (d = 0; i > 1; i--) {
    b[i - 1] += b[i] - average;
    if (b[i] - average >= 0)
      d += b[i] - average;
    else
      d += average - b[i];
  }
  printf("%I64d\n", ans > d ? d : ans);
  return 0;
}
