N = int(eval(input()))

A = list(map(int, input().split()))

table = []

 

for i, a in enumerate(A):

    table.append([a,i])

table.sort()

 

# DP[i][j] 左端に i 個, 右端に j 個貪欲に詰めたときのうれしさ

DP = [[0 for i in range(N+1)] for j in range(N+1)]

for i in range(1,N+1):

    # i 人目 table[i-1] の幼児を移動させる

    baby, pos = table.pop()

    

    DP[i][0] = DP[i-1][0] + baby * abs(pos-i+1)

    DP[0][i] = DP[0][i-1] + baby * abs(pos-(N-i))

    for x in range(1,i):

        y = i - x

 

        #左端に置く場合と右端に置く場合でmax

        DP[x][y] = max(DP[x-1][y] + baby * abs(pos-x+1),\

                     DP[x][y-1] + baby * abs(pos-(N-y)))

 

ans = 0

for i in range(N+1):

    ans = max(ans, DP[i][N-i])

print(ans)
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 = [[0 for i in range(N+1)] for j in range(N+1)]

# print(dp)

for left in range(0, N):

    for right in range(0, N - left):

        # print(f'left, righte: { left, right }')

        # print(A_list[left + right - 1][0])

        activity = A_list[left + right][0]

        distance_from_left = A_list[left + right][1] - left

        # print(f'distance_from_left: { distance_from_left }')

        distance_from_right = (N - 1 - right) - A_list[left + right][1]

        # print(f'distance_from_right: { distance_from_right }')

        dp[left + 1][right]\

            = max(dp[left + 1][right],

                  dp[left][right] + activity * distance_from_left)

        dp[left][right + 1]\

            = max(dp[left][right + 1],

                  dp[left][right] + activity * distance_from_right)

        # print('dp---')

        # print(dp)

        # print('---dp')

# print(dp)

dp_list = [flatten for inner in dp for flatten in inner]

print(max(dp_list))

N = int(eval(input()))
A = list(map(int, input().split()))
table = []
for i, a in enumerate(A):
    table.append([a, i])
table.sort()
# DP[i][j] 左端に i 個, 右端に j 個貪欲に詰めたときのうれしさ
DP = [[0 for i in range(N + 1)] for j in range(N + 1)]
for i in range(1, N + 1):
    # i 人目 table[i-1] の幼児を移動させる
    baby, pos = table.pop()
    DP[i][0] = DP[i - 1][0] + baby * abs(pos - i + 1)
    DP[0][i] = DP[0][i - 1] + baby * abs(pos - (N - i))
    for x in range(1, i):
        y = i - x
        # 左端に置く場合と右端に置く場合でmax
        DP[x][y] = max(
            DP[x - 1][y] + baby * abs(pos - x + 1),
            DP[x][y - 1] + baby * abs(pos - (N - y)),
        )
ans = 0
for i in range(N + 1):
    ans = max(ans, DP[i][N - i])
print(ans)

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 = [[0 for i in range(N + 1)] for j in range(N + 1)]
# print(dp)
for left in range(0, N):
    for right in range(0, N - left):
        # print(f'left, righte: { left, right }')
        # print(A_list[left + right - 1][0])
        activity = A_list[left + right][0]
        distance_from_left = A_list[left + right][1] - left
        # print(f'distance_from_left: { distance_from_left }')
        distance_from_right = (N - 1 - right) - A_list[left + right][1]
        # print(f'distance_from_right: { distance_from_right }')
        dp[left + 1][right] = max(
            dp[left + 1][right], dp[left][right] + activity * distance_from_left
        )
        dp[left][right + 1] = max(
            dp[left][right + 1], dp[left][right] + activity * distance_from_right
        )
        # print('dp---')
        # print(dp)
        # print('---dp')
# print(dp)
dp_list = [flatten for inner in dp for flatten in inner]
print(max(dp_list))

-N = int(eval(input())) -A = list(map(int, input().split())) -table = [] -for i, a in enumerate(A): -    table.append([a, i]) -table.sort() -# DP[i][j] 左端に i 個, 右端に j 個貪欲に詰めたときのうれしさ -DP = [[0 for i in range(N + 1)] for j in range(N + 1)] -for i in range(1, N + 1): -    # i 人目 table[i-1] の幼児を移動させる -    baby, pos = table.pop() -    DP[i][0] = DP[i - 1][0] + baby * abs(pos - i + 1) -    DP[0][i] = DP[0][i - 1] + baby * abs(pos - (N - i)) -    for x in range(1, i): -        y = i - x -        # 左端に置く場合と右端に置く場合でmax -        DP[x][y] = max( -            DP[x - 1][y] + baby * abs(pos - x + 1), -            DP[x][y - 1] + baby * abs(pos - (N - y)), +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 = [[0 for i in range(N + 1)] for j in range(N + 1)] +# print(dp) +for left in range(0, N): +    for right in range(0, N - left): +        # print(f'left, righte: { left, right }') +        # print(A_list[left + right - 1][0]) +        activity = A_list[left + right][0] +        distance_from_left = A_list[left + right][1] - left +        # print(f'distance_from_left: { distance_from_left }') +        distance_from_right = (N - 1 - right) - A_list[left + right][1] +        # print(f'distance_from_right: { distance_from_right }') +        dp[left + 1][right] = max( +            dp[left + 1][right], dp[left][right] + activity * distance_from_left -ans = 0 -for i in range(N + 1): -    ans = max(ans, DP[i][N - i]) -print(ans) +        dp[left][right + 1] = max( +            dp[left][right + 1], dp[left][right] + activity * distance_from_right +        ) +        # print(dp) +# print(dp) +dp_list = [flatten for inner in dp for flatten in inner] +print(max(dp_list))