N = int(input())
As = list(map(int, input().split()))
preans = []
for n in range(N):
    preans.append([n, As[n]])
preans.append(preans.pop(0))
judge = 0
while judge == 0:
    for _ in range(N-1):
        for n in range(N-1):
            if abs(preans[n][0] - n) >= abs(preans[n][0] - (n+1)):
                ans1 = -preans[n][1]
            else:
                ans1 = preans[n][1]
            if abs(preans[n+1][0] - (n+1)) >= abs(preans[n+1][0] - n):
                ans2 = -preans[n+1][1]
            else:
                ans2 = preans[n+1][1]
            #print(ans1, ans2)
            if ans1 + ans2 >= 0:
                preans[n], preans[n+1] = preans[n+1], preans[n]
            else:
                judge = 1
                continue
ans = 0
#print(preans)
for n in range(N):
    num = abs(preans[n][0] - n)
    ans += num * preans[n][1]
print(ans)