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