#include <bits/stdc++.h>
using namespace std;
long long int ans, n, x, sum, i;
long long int arr[500004];
vector<pair<long long int, long long int> > eks, art;
int main() {
  cin >> n;
  for (i = 1; i <= n; i++) {
    cin >> arr[i];
    sum += arr[i];
  }
  sum /= n;
  for (i = 1; i <= n; i++) {
    if (arr[i] > sum) {
      arr[i + 1] += arr[i] - sum;
      ans += arr[i] - sum;
    } else {
      arr[i + 1] += arr[i] - sum;
      ans += sum - arr[i];
    }
  }
  cout << ans << endl;
}
