N = int(input())
A = [int(i) for i in input().split()]
s = 0
AP = []
for a in A:
    s += 1
    AP.append((a, s))

AP.sort(reverse=True)

lst = [int(i) for i in range(1, N + 1)]
ans = 0
for a in AP:
    if abs(a[1] - lst[0]) >= abs(a[1] - lst[-1]):
        ans += abs(a[1] - lst.pop(0)) * a[0]
    else:
        ans += abs(a[1] - lst.pop(-1)) * a[0]
print(ans)
