N=int(input())
A=list(map(int,input().split()))

AA=[]
for i in range(N):
    AA.append([A[i],i])
AA.sort(reverse=True)

f=0
l=N-1
ans=0

for j in range(N):
    if abs(AA[j][1]-f)>abs(l-AA[j][1]):
        ans+=AA[j][0]*abs(AA[j][1]-f)
        f+=1
    else:
        ans+=AA[j][0]*abs(l-AA[j][1])
        l-=1
print(ans)