#################################3
#################################3
#################################3
#################################3


def read(container_type, data_type):
    return container_type(data_type(x) for x in input().split())


n, = read(tuple, int)
arr = read(list, int)

i_sorted = sorted(range(n), key=lambda i: arr[i], reverse=True)

# import functools
# import sys
#
# sys.setrecursionlimit(1000000)
#
#
# @functools.lru_cache(None)
# def dp_func(left, right):
#     if left >= right:
#         return 0
#     i = i_sorted[n - right + left]
#     x = arr[i]
#     dl = abs(i - left)
#     dr = abs(i - right + 1)
#     # if dl > dr:
#     #     return x * dl + dp(left + 1, right)
#     # elif dl < dr:
#     #     return x * dr + dp(left, right - 1)
#     return max(x * dl + dp_func(left + 1, right), x * dr + dp_func(left, right - 1))


dp = [[0] * (n + 1) for _ in range(n + 1)]
for ll in range(1, n + 1):
    for left in range(n - ll + 1):
        right = left + ll
        i = i_sorted[n - right + left]
        x = arr[i]
        dl = abs(i - left)
        dr = abs(i - right + 1)
        dp[left][right] = max(x * dl + dp[left + 1][right], x * dr + dp[left][right - 1])

sol = dp[0][n]
print(sol)

#################################3
#################################3
#################################3
#################################3
