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