import numpy as np
import sys
sys.setrecursionlimit(10**9)

n = int(input())
a = list(map(int, input().split()))

a2 = []
for i in range(n):
    a2.append((a[i], i))
a2.sort(reverse=True)

dp = np.full((n, n, n), -1)


def solve(i2, s, e):
    if dp[i2][s][e] != -1:
        return dp[i2][s][e]

    a, i = a2[i2]

    if i2 == n-1:
        dp[i2][s][e] = a * abs(s-i)
        return dp[i2][s][e]

    dp[i2][s][e] = max(solve(i2+1, s+1, e) + a * abs(s - i),
                       solve(i2+1, s, e-1) + a * abs(e - i))

    return dp[i2][s][e]


print(solve(0, 0, n-1))
