import numpy as np

N = int(input())
A_list = map(int, input().split())

# [活発度, もとの番号] のリストを降順に並べる
A_list = [[A, index] for index, A in enumerate(A_list)]
A_list.sort(reverse=True)

# print(A_list)

dp = np.zeros((N+1, N+1), int)
# print(dp)

# right = 0
for left in range(N):
    activity = A_list[left][0]
    distance_from_left = abs(left - A_list[left][1])
    dp[left + 1][0] = dp[left][0] + activity * distance_from_left

# left = 0
for right in range(N):
    activity = A_list[right][0]
    distance_from_right = abs(N - 1 - right - A_list[right][1])
    dp[0][right + 1] = dp[0][right] + activity * distance_from_right

for left in range(1, N + 1):
    for right in range(1, N - left + 1):
        activity = A_list[left + right - 1][0]
        distance_from_left = abs((left - 1) - A_list[left + right - 1][1])
        distance_from_right = abs((N - right) - A_list[left + right - 1][1])
        score_if_appended_to_left\
            = dp[left - 1][right] + activity * distance_from_left
        score_if_appended_to_right\
            = dp[left][right - 1] + activity * distance_from_right
        dp[left][right] = max(score_if_appended_to_left,
                              score_if_appended_to_right)
        # print(dp)
print(np.max(dp))
