n = int(input())
a = list(map(int, input().split()))

a = [[a[i], i] for i in range(n)]
a.sort(reverse=True)

l = 0
r = n-1
ans = 0
f = 0

for ai, i in a:
    pl = abs(l - i)
    pr = abs(r - i)
    if pl > pr:
        ans += pl * ai
        l += 1
        if f:
            r -= 1
            f = 0
    elif pl < pr:
        ans += pr * ai
        r -= 1
        if f:
            l += 1
            f = 0
    else:
        ans += pr * ai
        f = 1
print(ans)
