N = int(input())
A = list(map(int,input().split()))

l = 0
r = N-1
ans = 0
for i in range(N):
    c = -1
    cand = []
    for j in range(N):
        if A[j]==0:
            continue
        if j-l >= r-j:
            if c < A[j]*(j-l):
                c = A[j]*(j-l)
                cand = [j,l]
        else:
            if c < A[j]*(r-j):
                c = A[j]*(r-j)
                cand = [j,r]
    if cand[1]==l:
        l += 1
    if cand[1]==r:
        r -= 1
    A[cand[0]] = 0
    ans += c

print(ans)