N = int(input())
A = list(map(int, input().split()))
B = [0] * N
for i in range(N):
    B[i] = (A[i] << 36) +i

B.sort(reverse = True)

l = 0
r = N-1
m = (1<< 36) -1
ans = 0
for i in range(N):
    b = B[i]
    s = (b&m)
    a = A[s]
    L = abs(l-s) 
    R = abs(s-r)
    if L >= R:    
        #print(a,s,l,L,ans)
        ans += L*a
        l += 1
    else:
        #print(a,s,r,R,ans)
        ans += R*a
        r -= 1

print(ans)