import sys,math,copy,queue,itertools,bisect
sys.setrecursionlimit(10**7)
LI = lambda : [int(x) for x in sys.stdin.readline().split()]
_LI = lambda : [int(x)-1 for x in sys.stdin.readline().split()]
NI = lambda : int(sys.stdin.readline())

N = NI()
A = LI()

ure = 0
top = 0
bot = N-1
for k in range(N):
    max_u = [0,0]
    max_u2 = [0,0]
    max_i = [-1,-1]
    max_i2 = [-1,-1]
    for i in range(N):
        u0 = abs(top-i) * A[i]
        u1 = abs(bot-i) * A[i]

        if u0 > max_u[0] or u0 == max_u[0] and A[i] > A[max_i[0]]:
            max_u2[0] = max_u[0]
            max_i2[0] = max_i[0]
            max_u[0] = u0
            max_i[0] = i
        else:
            if u0 > max_u2[0] or u0 == max_u2[0] and A[i] > A[max_i2[0]]:
                max_u2[0] = u0
                max_i2[0] = i

        if u1 > max_u[1]  or u1 == max_u[1] and A[i] > A[max_i[1]]:
            max_u2[1] = max_u[1]
            max_i2[1] = max_i[1]
            max_u[1] = u1
            max_i[1] = i
        else:
            if u1 > max_u2[1]  or u1 == max_u2[1] and A[i] > A[max_i2[1]]:
                max_u2[1] = u1
                max_i2[1] = i

    up_i = 0
    max_tb = 0
    if max_u[0] > max_u[1]:
        ure += max_u[0]
        up_i = max_i[0]
    elif max_u[1] > max_u[0]:
        ure += max_u[1]
        up_i = max_i[1]
        max_tb = 1
    else:
        if max_u2[0] > max_u2[1]:
            ure += max_u2[0]
            up_i = max_i2[0]
        elif max_u2[1] > max_u2[0]:
            ure += max_u2[1]
            up_i = max_i2[1]
            max_tb = 1
        else:
            ure += max_u[0]
            up_i = max_i[0]

    A[up_i] = 0
    if max_tb == 0:
        top += 1
    else:
        bot -= 1
print (ure)
