n = int(input())
A_l = [(A,i) for i, A in enumerate(map(int,input().split()))]
A_l.sort()
A_l.reverse()

dp = []
for _ in range(n+1):
    dp.append([0] * (n+1))
for j in range(1,n+1): # all go bigger
    yb = n - (j-1)
    x = A_l[j-1][1] + 1
#    if yb-i <0:
#        dp[j][0] = -float('inf')
#    else:
    dp[j][0] = dp[j-1][0] + A_l[j-1][0]*(yb-x)
for k in range(1,n+1): # all go smaller
    ys = 1 + (k-1)
    x = A_l[k-1][1] + 1
#    if i-ys <0:
#        dp[0][k] = -float('inf')
#    else:
    dp[0][k] = dp[0][k-1] + A_l[k-1][0]*(x-ys)

for j in range(1,n+1):
    for k in range(1,n-j+1):
        yb = n - (j-1)
        ys = 1 + (k-1)
        x = A_l[j+k-1][1] + 1
        j_plus = A_l[j+k-1][0]*(yb-x)
        k_plus = A_l[j+k-1][0]*(x-ys)
#        if j_plus <0:
#            j_plus = -float('inf')
#        elif k_plus <0:
#            k_plus = -float('inf')
        dp[j][k] = max(dp[j-1][k]+j_plus, dp[j][k-1]+k_plus)

result = 0
for j in range(0,n+1):
    k = n-j
    result = max(result, dp[j][k])
print(result)
#for l in range(n+1):
#    print(dp[l])


