
N = int(input())

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

ans = 0
use = [True] * N

l = 0
r = N-1

for i in range(N):

    nmax = 0
    maxind = 0
    maxdir = 0

    for i in range(N):

        if use[i]:
            if nmax < abs(i - l)*A[i]:
                nmax = abs(i - l)*A[i]
                maxind = i
                maxdir = 0
            if nmax < abs(i - r)*A[i]:
                nmax = abs(i-r)*A[i]
                maxind = i
                maxdir = 1

    ans += nmax
    if maxdir == 0:
        l += 1
    if maxdir == 1:
        r -= 1
    use[maxind] = False

print (ans)