from collections import defaultdict

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

l = 0; r = N-1
ans = 0
used = [False for _ in range(N)]
for _ in range(N):
    ltmp = defaultdict(list)
    lmaxV = -1
    rtmp = defaultdict(list)
    rmaxV= -1
    for i in range(N):
        if used[i]:
            continue
        ltmp[A[i]*abs(l-i)].append([i, abs(l-i), "l"])
        lmaxV = max(lmaxV, A[i]*abs(l-i))
        rtmp[A[i]*abs(r-i)].append([i, abs(r-i), "r"])
        rmaxV = max(rmaxV, A[i]*abs(r-i))
    if lmaxV > rmaxV:
        t = ltmp[lmaxV]
        t.sort(key=lambda x:x[1])
        ti = t[-1][0]
        used[ti] = True
        l += 1
        ans += lmaxV
    elif lmaxV < rmaxV:
        t = rtmp[rmaxV]
        t.sort(key=lambda x:x[1])
        ti = t[-1][0]
        used[ti] = True
        r -= 1
        ans += rmaxV
    else:
        t = ltmp[lmaxV]+rtmp[rmaxV]
        t.sort(key=lambda x:x[1])
        ti = t[-1][0]
        used[ti] = True
        if t[-1][2] == "l":
            l += 1
        else:
            r -= 1
        ans += lmaxV
print(ans)