import sys
input = sys.stdin.readline

n = int(input())
a = list(map(int, input().split()))
a = [(a_i, i+1) for i, a_i in enumerate(a)]
a.sort(reverse=True)


l, r = 1, n
cnt = 0
for p in range(n):
    a_i, i = a[p]
    if p == n-1:
        cnt += abs(l - i)
        continue
    if abs(l - i) == abs(r - i):
        cnt += a_i * abs(l - i)
        a_i2, i2 = a[p + 1]
        if abs(l - i2) < abs(r - i2):
            l += 1
        else:
            r -= 1

    elif abs(l - i) > abs(r - i):
        cnt += a_i * abs(l - i)
        l += 1

    elif abs(l - i) < abs(r - i):
        cnt += a_i * abs(r - i)
        r -= 1

print(cnt)
