#include <bits/stdc++.h>
using namespace std;
void printarr(long long int arr[], long int n) {
  for (long int i = 0; i < n; i++) {
    cout << arr[i] << " ";
  }
  cout << "\n";
}
int main() {
  long int n;
  cin >> n;
  long long int arr[n], sum = 0;
  for (long int i = 0; i < n; i++) {
    cin >> arr[i];
    sum += arr[i];
  }
  long long int factor = sum / n, ans = 0;
  long long int buffer = 0;
  for (long int i = 0; i < n - 1; i++) {
    ans += abs(arr[i] - factor);
    if (arr[i] > factor)
      arr[i + 1] += (arr[i] - factor);
    else if (arr[i] < factor)
      arr[i + 1] -= (factor - arr[i]);
    arr[i] = factor;
  }
  cout << ans << endl;
  return 0;
}
