printn = lambda x: print(x,end='')
inn = lambda : int(input())
inl   = lambda: list(map(int, input().split()))
inm   = lambda:      map(int, input().split())
ins = lambda : input().strip()
DBG = True  and False
BIG = 10**18
R = 10**9 + 7

def ddprint(x):
  if DBG:
    print(x)

n = inn()
a = inl()
done = [False]*n
ans = [-1]*n
l = 0
r = n-1
sm = 0
ldif = [a[i]*i for i in range(n)]
rdif = [a[i]*(n-1-i) for i in range(n)]
lmx = rmx = 0
for i in range(n):
    if ldif[i]>lmx:
        lmx = ldif[i]
        lmxi = i
    if rdif[i]>rmx:
        rmx = rdif[i]
        rmxi = i
while l<=r:
    ddprint(done)
    if lmx>rmx:
        ans[l] = lmxi
        ddprint(f"ans l {l} set to {lmxi}")
        done[lmxi] = True
        l += 1
        sm += lmx
        for i in range(n):
            ldif[i] -= a[i]
    else:
        ans[r] = rmxi
        ddprint(f"ans r {r} set to {rmxi}")
        done[rmxi] = True
        r -= 1
        sm += rmx
        for i in range(n):
            rdif[i] -= a[i]
    lmx = 0
    rmx = 0
    for i in range(n):
            if done[i]:
                continue
            if ldif[i]>lmx:
                lmx = ldif[i]
                lmxi = i
            if rdif[i]>rmx:
                rmx = rdif[i]
                rmxi = i
    ddprint(f"lmx {lmx} lmxi {lmxi} rmx {rmx} rmxi {rmxi}")
ddprint(ans)
print(sm)
