import sys

input = sys.stdin.readline


def solve():
    N = int(input())
    A = list(map(int, input().split()))
    a_idx = [(a, -idx) for idx, a in enumerate(A)]
    a_idx.sort(reverse=True)
    a_idx = [(a, -idx) for a, idx in a_idx]

    dp = [[0 for _ in range(N + 2)] for _ in range(N + 2)]
    for i in range(N + 2):
        dp[-1][i] = -100
        dp[i][-1] = -100
    for num_go_left in range(0, N + 1):
        for num_go_right in range(0, N + 1 - num_go_left):
            if num_go_left == 0 and num_go_right == 0:
                continue
            current_baby_idx = num_go_right + num_go_left - 1
            active_vale, position = a_idx[current_baby_idx]
            left_idx = num_go_left - 1
            right_idx = N - num_go_right

            dp[num_go_left][num_go_right] = max(
                dp[num_go_left - 1][num_go_right] + active_vale * (position - left_idx),
                dp[num_go_left][num_go_right - 1] + active_vale * (right_idx - position)
            )

    return max([dp[num_go_left][N - num_go_left] for num_go_left in range(0, N + 1)])


if __name__ == '__main__':
    answer = solve()
    print(answer)
