n = int(input())
a = list(map(int, input().split()))
b = []
for i, v in enumerate(a):
    b.append((v, i))
b.sort()
l = 0
r = n - 1
ans = 0
while len(b) > 0:
    v, i = b.pop()
    if abs(i - l) > abs(i - r):
        ans += abs(i - l) * v
        l += 1
    else:
        ans += abs(i - r) * v
        r -= 1
print(ans)
