n=int(input())
a=list(map(int,input().split()))
inf=[[a[i],i] for i in range(n)]
inf.sort(reverse=True)
ans=[False]*n
l=0;r=n-1
for i in range(n):
    a=inf[i][0];x=inf[i][1]
    if x-l>=r-x: ans[l]=[a,x];l+=1
    if x-l<r-x: ans[r]=[a,x];r-=1

x=0
for i in range(n):
    x+=ans[i][0]*abs(ans[i][1]-i)
print(x)