N = int(input())
A = list(map(int,input().split()))
A = sorted([(A[i],i) for i in range(N)],key=lambda x:x[0],reverse=True)
curl = 0
curr = N-1
cnt = 0
for i in range(N):
    a,ind = A[i]
    if abs(curr-ind)>=abs(curl-ind):
        cnt += a*abs(curr-ind)
        curr -= 1
    else:
        cnt += a*abs(curl-ind)
        curl += 1
print(cnt)        