import random
maxa = 0

n = int(input())
arr = list(map(int,input().split()))
children = [[i, arr[i]] for i in range(n)]

for ii in range(12):
    buf = [i for i in range(n)]
    random.shuffle(buf)
    ans = 0

    for i,b in enumerate(buf):
        il  = children[b][0]
        ans += children[b][1]*abs(il-i)

    while True:
        dif = 0
        for i in range(n-1):
            l = buf[i]
            r = buf[i+1]
            il = children[l][0]
            ir = children[r][0]

            tp = -(children[l][1]*abs(il-i)+children[r][1]*abs(ir-(i+1))-children[l][1]*abs(il-(i+1))-children[r][1]*abs(ir-i))
            if tp > 0:
                buf[i],buf[i+1] = buf[i+1],buf[i]
                dif += tp
            else:
                continue
        if dif > 0:
            ans += dif
        else:
            break

    if ans > maxa:
        maxa = ans
print(maxa)