import collections
import heapq
N = int(input())
A = list(map(int, input().split()))
B = [(A[i], i) for i in range(N)]
l = 0
r = N-1
ans = 0
for i in range(N):
    C = sorted(B, key=lambda x: x[0]*abs(x[1]-l))
    D = sorted(B, key=lambda x: x[0]*abs(x[1]-r))
    a, b = C.pop()
    c, d = D.pop()
    if a*abs(b-l) >= c*abs(d-r):
        ans += a*abs(b-l)
        B = list(C)
        l += 1
    else:
        ans += c*abs(d-r)
        B = list(D)
        r -= 1

print(ans)
