import heapq
import math
import random
import sys
from bisect import bisect_left, bisect_right
from collections import Counter, defaultdict, deque, namedtuple, UserDict
from functools import cmp_to_key, lru_cache, reduce
from itertools import (chain, combinations, combinations_with_replacement,
                       permutations, product)

sys.setrecursionlimit(10**6+1)
write = sys.stdout.write
# input = sys.stdin.buffer.readline

MOD = 10**9 + 7


if __name__ == "__main__":
    n = int(input())
    nums = [int(c) for c in input().split()]
    arr = [(num, i) for i, num in enumerate(nums)]
    arr.sort(key=lambda x: -x[0])
    dp = [[0] * (n+1) for _ in range(n+1)]    

    for i in range(n):
        a, idx = arr[i]
        for j in range(i+1):
            dp[j+1][i-j] = max(dp[j+1][i-j], dp[j][i-j] + a*(idx - j))
            dp[j][i-j+1] = max(dp[j][i-j+1], dp[j][i-j] + a*(n-1-(i-j)-idx))

    print(max(dp[i][n-i] for i in range(n+1)))