import heapq

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

que = []
for i in range(N):
    heapq.heappush(que, (-A[i], i))

l = 0; r = N-1
ans = 0
while que:
    e = heapq.heappop(que)
    x = e[1]
    if abs(r-x) >= abs(x-l):
        ans += abs(e[0]*(r-x))
        r -= 1
    else:
        ans += abs(e[0]*(x-l))
        l += 1
print(ans)