#include<iostream>
#include<algorithm>
using namespace std;
typedef long long LL;
const int N = 1e6 + 10;
int n;
int w[N], x[N], su[N];
int main(){
cin >> n;
for(int i = 1; i <= n; i++){
cin >> w[i];
su[i] = su[i - 1] + w[i];
}
int m = su[n] / n;
x[1] = 0;
for(int i = 2; i <= n; i++){
x[i] = su[i - 1] - m * (i - 1);
}
sort(x + 1, x + n + 1);
LL res = 0;
for(int i = 1; i <= n/2; i++){
res += (x[n - i + 1] - x[i]);
}
cout << res;
}