import sys

sys.setrecursionlimit(10 ** 6)
int1 = lambda x: int(x) - 1
p2D = lambda x: print(*x, sep="\n")
def II(): return int(sys.stdin.readline())
def MI(): return map(int, sys.stdin.readline().split())
def LI(): return list(map(int, sys.stdin.readline().split()))
def LLI(rows_number): return [LI() for _ in range(rows_number)]
def SI(): return sys.stdin.readline()[:-1]

def main():
    n=II()
    aa=LI()
    ii=list(range(n))
    for _ in range(2):
        for i in range(n):
            mx=0
            mxj=-1
            for j in range(n):
                i0=ii[i]
                i1=ii[j]
                cur=(abs(j-i0)-abs(i-i0))*aa[i]+(abs(i-i1)-abs(j-i1))*aa[j]
                if cur>mx:
                    mx=cur
                    mxj=j
            if mx:
                aa[i],aa[mxj]=aa[mxj],aa[i]
                ii[i],ii[mxj]=ii[mxj],ii[i]
    ans=0
    for i in range(n):
        ans+=abs(i-ii[i])*aa[i]
    print(ans)

main()
