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

m = []
for x, c in enumerate(l):
    m.append((c, x))
m.sort(reverse=True)

dp = [[-1 for _ in range(n)] for __ in range(n)]
dp[0][n-1] = 0

result = 0
for i, (c, x) in enumerate(m):
    s = n-i-2
    for j in range(i+2):
        if j + s + 1 < n:
            dp[j][j + s] = max(dp[j][j + s], dp[j][j + s + 1] + c * abs(x - (j + s + 1)))
        if j > 0:
            # print(j, s)
            try:
                dp[j][j+s] = max(dp[j][j+s], dp[j-1][j+s] + c * abs(x - (j-1)))
            except Exception as e:
                continue
        result = max(result, dp[j][j+s])
        # print("res: ", result)
print(result)