#include <bits/stdc++.h>
int main() {
  int i, n;
  scanf("%d", &n);
  long long int sum = 0, a[n];
  for (i = 0; i < n; i++) {
    scanf("%lld", &a[i]);
    sum += a[i];
  }
  long long int avg = sum / n;
  long long int dif = 0, ans = 0;
  for (i = 0; i < n - 1; i++) {
    dif = a[i] - avg;
    a[i + 1] += dif;
    ans = (dif > 0) ? ans + dif : ans - dif;
  }
  printf("%lld", ans);
}
