import numpy as np
N = int(input())
a= list(map(int,input().split()))
score=np.array(a)
b=np.sort(score)
c=b[-1::-1]
#print(c)
x=np.argsort(a)
y=x[-1::-1]
#print(y)

#c:score、y:index
cand=list(range(N))

count=0
for i in range(N):
    if (y[i]-cand[0])>(cand[-1]-y[i]):
        count+=c[i]*abs(y[i]-cand[0])
        cand.pop(0)
    elif (y[i]-cand[0])<(cand[-1]-y[i]):
        count+=c[i]*abs(cand[-1]-y[i])
        cand.pop(-1)
    else:
        if y[i+1]>y[i]:
            count+=c[i]*abs(y[i]-cand[0])
            cand.pop(0)
        else:
            count+=c[i]*abs(cand[-1]-y[i])
            cand.pop(-1) 

print(count)
