N = int(input())
As = [int(s) for s in input().split()]
 
l = 0
r = N - 1
 
S = 0
for k in range(N):
    m, mp, il, R = 0, 0, False, 0
    for p in range(N):
        v = As[p]
 
        tm = v * (p - l)
        if m < tm or m == tm and R < p - l:
            m = tm
            mp = p
            il = True
            R = p - l
 
        tm = v * (r - p)
        if m < tm or m == tm and R < r - p:
            m = tm
            mp = p
            il = False
            R = r - p
 
    S += m
    As[mp] = 0
    if il:
        l += 1
    else:
        r -= 1
 
print(S)
