#include <bits/stdc++.h>
using namespace std;
long long a[500005];
int main() {
  int n;
  scanf("%d", &n);
  long long k = 0;
  for (int i = 1; i <= n; i++) {
    scanf("%I64d", &a[i]);
    k += a[i];
  }
  long long per = k / n;
  long long ans = 0, tmp;
  for (int i = 1; i <= n; i++) {
    tmp = a[i] - per;
    ans += tmp > 0 ? tmp : -tmp;
    a[i + 1] += tmp;
  }
  printf("%I64d\n", ans);
  return 0;
}
