program fy;
var a,c:array[0..1000000]of real;
i,n:longint;tot,ans,x1,m:real;
procedure qs(l,r:integer);
var i,j:longint;mid,t:real;
begin
i:=l;
j:=r;
mid:=c[(l+r)div 2];
repeat
while c[i]<mid do inc(i);
while c[j]>mid do dec(j);
if i<=j then
begin
t:=c[i];
c[i]:=c[j];
c[j]:=t;
inc(i);
dec(j);
end;
until i>j;
if l<j then qs(l,j);
if i<r then qs(i,r);
end;
begin
assign(input,'Wealth.in');reset(input);
assign(output,'Wealth.out');rewrite(output);
while not eoln do
begin
readln(n);tot:=0;
for i:=1 to n do
begin
readln(a[i]);
tot:=tot+a[i];
end;
m:=tot/n;
c[0]:=0;
for i:=1 to n-1 do
c[i]:=c[i-1]+a[i]-m;
qs(0,n-1);
if n mod 2=1 then x1:=c[(n div 2)] else x1:=c[n div 2];
ans:=0;
for i:=0 to n-1 do
ans:=ans+abs(x1-c[i]);
writeln(round(ans));
end;
close(input);
close(output);
end.