N = int(input())
A = list(map(int,input().split()))
K = []
used = [0]*N

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

ans = 0
mi = 0
ma = N-1
for i in K:
    n = i[1]
    if n - mi >= ma - n:
        ans += i[0]*(n-mi)
        mi += 1
    else:
        ans += i[0]*(ma-n)
        ma -= 1

print(ans)