AtCoder Beginner Contest 163 - Active Infants

There are N children standing in a line from left to right. The activeness of the i-th child from the left is A_i.
You can rearrange these children just one time in any order you like.
When a child who originally occupies the x-th position from the left in the line moves to the y-th position from the left, that child earns A_x \times |x-y| happiness points.
Find the maximum total happiness points the children can earn.

-----Constraints-----
 - 2 \leq N \leq 2000
 - 1 \leq A_i \leq 10^9
 - All values in input are integers.

-----Input-----
Input is given from Standard Input in the following format:
N
A_1 A_2 ... A_N

-----Output-----
Print the maximum total happiness points the children can earn.

-----Sample Input-----
4
1 3 4 2

-----Sample Output-----
20

If we move the 1-st child from the left to the 3-rd position from the left, the 2-nd child to the 4-th position, the 3-rd child to the 1-st position, and the 4-th child to the 2-nd position, the children earns 1 \times |1-3|+3 \times |2-4|+4 \times |3-1|+2 \times |4-2|=20 happiness points in total.

Difficulty: MEDIUM

Starter code:

Solutions:
N = int(input())
A = [int(v) for v in input().split()]
P = sorted([(v, i) for (i, v) in enumerate(A)], key=lambda x: x[0], reverse=True)
dp = [[0 for j in range(2005)] for i in range(2005)]
for i in range(N):
	pi = P[i][1]
	for l in range(i + 1):
		r = i - l
		dp[i + 1][l + 1] = max(dp[i + 1][l + 1], dp[i][l] + (pi - l) * A[pi])
		dp[i + 1][l] = max(dp[i + 1][l], dp[i][l] + (N - r - 1 - pi) * A[pi])
ans = 0
for i in range(N + 1):
	ans = max(ans, dp[N][i])
print(ans)


def main():
	N = int(input())
	INF = float('inf')
	A = [(i + 1, a) for (i, a) in enumerate(map(int, input().split()))]
	A = sorted(A, key=lambda x: x[1], reverse=True)
	dp = [[-INF] * (N + 1) for _ in range(N + 1)]
	dp[0][0] = 0
	for s in range(1, N + 1):
		for l in range(s + 1):
			r = s - l
			dp[l][r] = max(dp[l - 1][r] + A[s - 1][1] * abs(A[s - 1][0] - l), dp[l][r - 1] + A[s - 1][1] * abs(N - r + 1 - A[s - 1][0]))
	ans = 0
	for m in range(N):
		if dp[m][N - m] > ans:
			ans = dp[m][N - m]
	print(ans)
main()


import math

def abs(num):
	if num < 0:
		return -num
	else:
		return num

def abc163e(n, a):
	hap_dict = dict()
	hap_list = []
	for elem in range(n):
		if a[elem] in hap_dict:
			hap_dict[a[elem]].append(elem)
		else:
			tmp_list = [elem]
			hap_dict[a[elem]] = tmp_list
			hap_list.append(a[elem])
	hap_list.sort(reverse=True)
	left = 0
	right = n - 1
	num = 0
	dp = [[0 for re in range(n + 1)] for le in range(n + 1)]
	for hap in hap_list:
		pos_list = hap_dict[hap]
		while len(pos_list) != 0:
			tgt = pos_list.pop()
			num += 1
			for left in range(num + 1):
				right = num - left
				if left == num:
					dp[left][right] = dp[left - 1][right] + hap * abs(tgt - left + 1)
				elif left == 0:
					dp[left][right] = dp[left][right - 1] + hap * abs(n - right - tgt)
				else:
					left_dp = dp[left - 1][right] + hap * abs(tgt - left + 1)
					right_dp = dp[left][right - 1] + hap * abs(n - right - tgt)
					dp[left][right] = max(left_dp, right_dp)
	ans_list = []
	for i in range(num + 1):
		ans_list.append(dp[i][num - i])
	return max(ans_list)
n = int(input())
a = list(map(int, input().split(' ')))
ans = abc163e(n, a)
print(ans)


n = int(input())
a = [(_a, i) for (i, _a) in enumerate(map(int, input().split()))]
a.sort(reverse=True)
dp = [[0] * (n + 1) for _ in range(n + 1)]
for (i, (v, p)) in enumerate(a):
	dp[i + 1][0] = dp[i][0] + v * abs(n - 1 - i - p)
	for j in range(1, i + 1):
		dp[i + 1][j] = max(dp[i][j] + v * abs(n - 1 - (i - j) - p), dp[i][j - 1] + v * abs(j - 1 - p))
	dp[i + 1][i + 1] = dp[i][i] + v * abs(i - p)
print(max(dp[n]))


n = int(input())
a = list(map(int, input().split()))
l = []
for (i, x) in enumerate(a):
	l.append([x, i])
l.sort(reverse=True)
cnt = 1
dp = [[0] * (n + 1) for _ in range(n + 1)]
for (i, j) in l:
	for x in range(cnt + 1):
		if x == 0:
			dp[x][cnt - x] = dp[x][cnt - x - 1] + i * (n - 1 - (cnt - x - 1) - j)
		elif x == cnt:
			dp[x][cnt - x] = dp[x - 1][cnt - x] + i * (j - x + 1)
		else:
			dp[x][cnt - x] = max(dp[x - 1][cnt - x] + i * (j - x + 1), dp[x][cnt - x - 1] + i * (n - 1 - (cnt - x - 1) - j))
	cnt += 1
ans = 0
for i in range(n + 1):
	ans = max(ans, dp[i][n - i])
print(ans)


from sys import stdin

def main():
	n = int(stdin.readline())
	a = list(map(int, stdin.readline().split()))
	a2 = [[a[i], i] for i in range(n)]
	a2.sort(reverse=True)
	dp = [[0 for _ in range(n + 1)] for _ in range(n + 1)]
	ans = 0
	for i in range(n + 1):
		for j in range(n + 1 - i):
			s1 = s2 = 0
			if i > 0:
				s1 = dp[i - 1][j] + a2[i + j - 1][0] * (a2[i + j - 1][1] - (i - 1))
			if j > 0:
				s2 = dp[i][j - 1] + a2[i + j - 1][0] * (n - j - a2[i + j - 1][1])
			dp[i][j] = max(s1, s2)
		ans = max(ans, dp[i][n - i])
	print(ans)

def __starting_point():
	main()
__starting_point()


n = int(input())
a = list(map(int, input().split()))
a = list(zip(range(n), a))
a.sort(key=lambda x: x[1])
dp = [[0 for i in range(n + 1)] for j in range(n + 1)]
ans = 0
for k in range(n):
	(i, ai) = a.pop()
	dp[k + 1][0] = dp[k][0] + ai * (i - k)
	dp[0][k + 1] = dp[0][k] + ai * (n - k - 1 - i)
	for l in range(k):
		dp[k - l][l + 1] = max(dp[k - l - 1][l + 1] + ai * (i - k + l + 1), dp[k - l][l] + ai * (n - l - 1 - i))
for k in range(n + 1):
	ans = max(ans, dp[k][n - k])
print(ans)


N = int(input())
A = [int(s) for s in input().split(' ')]
ABI = sorted(((a, i) for (i, a) in enumerate(A, 1)), reverse=True)
prev = [0]
for (k, (a, i)) in enumerate(ABI):
	curr = [0] * (k + 2)
	for l in range(k + 1):
		curr[l] = max(curr[l], prev[l] + abs(N - i - k + l) * a)
		curr[l + 1] = prev[l] + abs(i - l - 1) * a
	prev = curr
print(max(prev))


n = int(input())
a = sorted(([x, i] for (i, x) in enumerate(list(map(int, input().split())))))
f = [[0 for j in range(n)] for i in range(n)]
for i in range(n - 1, -1, -1):
	f[i][i] = a[0][0] * abs(a[0][1] - i)
	for j in range(i + 1, n):
		f[i][j] = max(f[i + 1][j] + a[j - i][0] * abs(a[j - i][1] - i), f[i][j - 1] + a[j - i][0] * abs(a[j - i][1] - j))
print(f[0][n - 1])


n = int(input())
(*A,) = map(int, input().split())
A = [(a, i) for (i, a) in enumerate(A)]
A.sort(reverse=True)
DP = [[0 for r in range(n + 1)] for l in range(n + 1)]
for l in range(n):
	for r in range(n - l):
		(a, i) = A[l + r]
		DP[l + 1][r] = max(DP[l + 1][r], DP[l][r] + a * abs(i - l))
		DP[l][r + 1] = max(DP[l][r + 1], DP[l][r] + a * abs(i - (n - 1 - r)))
print(max([DP[l][n - l] for l in range(n)]))


N = int(input())
A = [int(i) for i in input().split(' ')]
AA = []
for (i, a) in enumerate(A):
	AA.append(tuple([a, i]))
AA = sorted(AA, reverse=True)
DP = []
for _ in range(N + 1):
	DP.append([0] * (N + 1))
for (i, a) in enumerate(AA):
	for x in range(i + 1):
		y = i - x
		if x + 1 <= N:
			DP[x + 1][y] = max(DP[x + 1][y], DP[x][y] + a[0] * abs(x - a[1]))
		if y + 1 <= N:
			DP[x][y + 1] = max(DP[x][y + 1], DP[x][y] + a[0] * abs(N - 1 - y - a[1]))
MAX = 0
for x in range(N):
	y = N - x
	MAX = max(DP[x][y], MAX)
print(MAX)


n = int(input())
a = [[int(_), 0] for _ in input().split()]
for i in range(n):
	a[i][1] = i
a.sort(reverse=True)
dp = [[0 for _ in range(n)] for __ in range(n)]
for l in range(n):
	for r in range(n):
		if l + r < n:
			if l < n - 1:
				dp[l + 1][r] = max(dp[l + 1][r], dp[l][r] + a[l + r][0] * abs(a[l + r][1] - l))
			if r < n - 1:
				dp[l][r + 1] = max(dp[l][r + 1], dp[l][r] + a[l + r][0] * abs(a[l + r][1] - (n - r - 1)))
print(max([dp[i][n - i] for i in range(1, n)]))


def solve():
	N = int(input())
	A = [[a, i] for (a, i) in zip(map(int, input().split()), range(N))]
	A.sort(key=lambda a: a[0], reverse=True)
	dp = [[0] * (N + 1) for _ in range(N + 1)]
	for i in range(0, N):
		(a, l) = A[i]
		for x in range(0, i + 1):
			y = i - x
			dp[x + 1][y] = max(dp[x + 1][y], dp[x][y] + a * abs(l - x))
			dp[x][y + 1] = max(dp[x][y + 1], dp[x][y] + a * abs(l - (N - y - 1)))
	print(max((dp[N - i][i] for i in range(N + 1))))
solve()


N = int(input())
A = list(map(int, input().split()))
A = [[A[i], i] for i in range(N)]
A.sort()
dp = [[0] * (N + 1) for i in range(N + 1)]
ans = 0
for i in range(N + 1):
	for j in range(N + 1):
		if i + j > N:
			continue
		if i + j == 0:
			continue
		(a, k) = A[-i - j]
		if i == 0:
			dp[i][j] = dp[i][j - 1] + a * (N - j - k)
		elif j == 0:
			dp[i][j] = dp[i - 1][j] + a * (k - i + 1)
		else:
			dp[i][j] = max(dp[i - 1][j] + a * (k - i + 1), dp[i][j - 1] + a * (N - j - k))
		ans = max(ans, dp[i][j])
print(ans)


def main():
	import numpy as np
	n = int(input())
	a = list(enumerate(map(int, input().split())))
	a.sort(key=lambda x: -x[1])
	dp = [np.zeros(i + 1, dtype=np.int64) for i in range(n + 1)]
	dp[0] = np.zeros(1, dtype=np.int64)
	r = np.arange(n + 1, dtype=np.int64)
	for (time, ix) in enumerate(a):
		(i, x) = ix
		dp[time + 1][1:] = dp[time][:time + 1] + (i - r[:time + 1]) * x
		np.maximum(dp[time + 1][:-1], dp[time] + (n - 1 - (time - r[:time + 1]) - i) * x, out=dp[time + 1][:-1])
	print(np.max(dp[n]))
main()


def main():
	N = int(input())
	A = list(map(int, input().split()))
	A = sorted(enumerate(A), key=lambda x: x[1])[::-1]
	dp = [[0] * (N + 1 - i) for i in range(N + 1)]
	m = 0
	for i in range(1, len(A) + 1):
		(index, a) = A[i - 1]
		for j in range(i + 1):
			if (i - j, j) == (0, 0):
				dp[i - j][j] = 0
			elif i - j == 0:
				dp[i - j][j] = dp[i - j][j - 1] + (N - j - index) * a
			elif j == 0:
				dp[i - j][j] = dp[i - j - 1][j] + abs(index - (i - j - 1)) * a
			else:
				dp[i - j][j] = max(dp[i - j][j - 1] + (N - j - index) * a, dp[i - j - 1][j] + abs(index - (i - j - 1)) * a)
			if i == len(A):
				m = max(m, dp[i - j][j])
	print(m)
main()


N = int(input())
A = sorted([(a, i) for (i, a) in enumerate(map(int, input().split()))], reverse=True)
dp = [[0] * (N + 1) for _ in [0] * (N + 1)]
for (j, (a, i)) in enumerate(A):
	for l in range(j + 1):
		r = N - 1 - (j - l)
		dp[j + 1][l] = max(dp[j + 1][l], dp[j][l] + a * (r - i))
		dp[j + 1][l + 1] = max(dp[j + 1][l + 1], dp[j][l] + a * (i - l))
print(max(dp[N]))


n = int(input())
a = list(map(int, input().split()))
cost = []
for (i, g) in enumerate(a):
	cost.append([g, i])
cost.sort(reverse=True)
dp = [[0] * (n + 1) for _ in range(n + 1)]
for i in range(n):
	for j in range(i + 1):
		dp[i + 1][j + 1] = max(dp[i + 1][j + 1], dp[i][j] + cost[i][0] * abs(cost[i][1] - j))
		dp[i + 1][j] = max(dp[i + 1][j], dp[i][j] + cost[i][0] * abs(n - 1 - (i - j) - cost[i][1]))
print(max(dp[n]))


N = int(input())

def left(a, i, prev):
	yield 0
	for (l, s) in enumerate(prev, 1 - i):
		yield (s + abs(l) * a)

def right(a, i, prev):
	for (r, s) in enumerate(prev, N - len(prev) - i + 1):
		yield (s + abs(r) * a)
	yield 0
pd = [0]
A = map(int, input().split())
for (a, i) in sorted(((a, i) for (i, a) in enumerate(A, 1)), reverse=True):
	pd = [*map(max, zip(left(a, i, pd), right(a, i, pd)))]
print(max(pd))


import sys
input = sys.stdin.readline
N = int(input())
A = list(map(int, input().split()))
A2 = [(num, ind) for (ind, num) in enumerate(A)]
A2.sort(reverse=True)
L = [0] * N
for i in range(N):
	(num, ind) = A2[i]
	NL = [0] * N
	for j in range(i + 1):
		right = N - 1 - (i - j)
		NL[j] = max(NL[j], abs(right - ind) * num + L[j])
		if j != N - 1:
			NL[j + 1] = max(NL[j + 1], abs(j - ind) * num + L[j])
	L = NL
print(max(L))


N = int(input())
A = list(map(int, input().split()))
A = sorted([(a, p) for (p, a) in enumerate(A)])[::-1]
dp = [[0] * (N + 1) for _ in range(N + 1)]
for (i, (a, p)) in enumerate(A):
	for j in range(i + 1):
		dp[i + 1][j + 1] = max(dp[i + 1][j + 1], dp[i][j] + (p - j) * a)
		dp[i + 1][j] = max(dp[i + 1][j], dp[i][j] + (N - 1 - (i - j) - p) * a)
print(max(dp[-1]))


n = int(input())
a = list(map(int, input().split()))
dp = [[0] * (n + 1) for _ in range(n + 1)]
p = []
for i in range(n):
	p.append((a[i], i))
p.sort(key=lambda x: -x[0])
for i in range(n):
	pi = p[i][1]
	for l in range(i + 1):
		r = i - l
		dp[i + 1][l + 1] = max(dp[i + 1][l + 1], dp[i][l] + (pi - l) * a[pi])
		dp[i + 1][l] = max(dp[i + 1][l], dp[i][l] + (n - r - 1 - pi) * a[pi])
ans = 0
for i in range(n + 1):
	ans = max(ans, dp[n][i])
print(ans)


N = int(input())
A = [(a, i) for (i, a) in enumerate(map(int, input().split()), start=1)]
A.sort(reverse=True)
dp = [[0] * (N + 1) for _ in range(N + 1)]
for (s, (a, i)) in enumerate(A):
	for l in range(s + 1):
		r = s - l
		L = dp[l][r] + a * (i - (l + 1))
		R = dp[l][r] + a * (N - r - i)
		if dp[l + 1][r] < L:
			dp[l + 1][r] = L
		if dp[l][r + 1] < R:
			dp[l][r + 1] = R
ans = 0
for i in range(N + 1):
	ans = max(ans, dp[i][N - i])
print(ans)


import sys
input = sys.stdin.readline
sys.setrecursionlimit(10 ** 7)
n = int(input())
A = list(map(int, input().split()))
L = [(a, i) for (i, a) in enumerate(A)]
L.sort(reverse=True)
dp = [[-1] * n for _ in range(n)]

def dfs(l, r):
	i = n - 1 - r + l
	(a, idx) = L[i]
	if i == n - 1:
		res = max(idx - l, r - idx) * a
		return res
	if dp[l][r] != -1:
		return dp[l][r]
	res = max((idx - l) * a + dfs(l + 1, r), (r - idx) * a + dfs(l, r - 1))
	dp[l][r] = res
	return res
ans = dfs(0, n - 1)
print(ans)


import sys

def solve(N: int, A: 'List[int]'):
	A = sorted(enumerate(A), reverse=True, key=lambda x: x[1])
	dp = [[0 for i in range(N + 1)] for j in range(N + 1)]
	for i in range(N):
		for j in range(0, N - i):
			(idx, a) = A[i + j]
			dp[i + 1][j] = max(dp[i + 1][j], dp[i][j] + a * abs(idx - i))
			dp[i][j + 1] = max(dp[i][j + 1], dp[i][j] + a * abs(N - 1 - j - idx))
	result = -1
	for i in range(N):
		tmp = dp[i][N - i]
		if tmp > result:
			result = tmp
	print(result)
	return

def main():

	def iterate_tokens():
		for line in sys.stdin:
			for word in line.split():
				yield word
	tokens = iterate_tokens()
	N = int(next(tokens))
	A = [int(next(tokens)) for _ in range(N)]
	solve(N, A)

def __starting_point():
	main()
__starting_point()


import sys
input = sys.stdin.readline
N = int(input())
A = list(map(int, input().split()))
iA = [(i, A[i]) for i in range(N)]
iA.sort(key=lambda t: t[1], reverse=True)
dp = [[0] * (N + 1) for _ in range(N + 1)]
for i in range(N):
	for j in range(N):
		if i + j < N:
			(idx, v) = iA[i + j]
			dp[i + 1][j] = max(dp[i + 1][j], dp[i][j] + (idx - i) * v)
			dp[i][j + 1] = max(dp[i][j + 1], dp[i][j] + (N - 1 - j - idx) * v)
ans = 0
for i in range(N + 1):
	ans = max(ans, dp[i][N - i])
print(ans)


def main():
	import sys
	input = sys.stdin.readline
	N = int(input())
	A = list(map(int, input().split()))
	B = [(A[i], i) for i in range(N)]
	B.sort(key=lambda x: x[0], reverse=True)
	DP = [[0] * (N + 1) for _ in range(N + 1)]
	for (i, b) in enumerate(B):
		a = b[0]
		tmp = b[1]
		for x in range(i + 1):
			DP[x + 1][i - x] = max(DP[x + 1][i - x], DP[x][i - x] + a * abs(tmp - x))
			DP[x][i - x + 1] = max(DP[x][i - x + 1], DP[x][i - x] + a * abs(N - tmp - 1 - (i - x)))
	ans = 0
	for i in range(N + 1):
		ans = max(ans, DP[i][N - i])
	print(ans)

def __starting_point():
	main()
__starting_point()


n = int(input())
a = list(map(int, input().split()))
b = sorted(a, reverse=True)
dp = [[0] * (n + 1) for _ in range(n + 1)]
ans = 0

def f(s, e):
	return abs(e - s) * a[s]
for i in range(n):
	d = a.index(b[i])
	for l in range(0, i + 1):
		r = i - l
		dp[l + 1][r] = max(dp[l + 1][r], dp[l][r] + f(d, l))
		dp[l][r + 1] = max(dp[l][r + 1], dp[l][r] + f(d, n - r - 1))
	a[d] = 0
for i in range(n + 1):
	ans = max(ans, dp[i][n - i])
print(ans)


from heapq import heappop, heappush
from collections import defaultdict
n = int(input())
a = list(map(int, input().split()))
hp = []
for (i, e) in enumerate(a, 1):
	heappush(hp, [-e, i])
dp = defaultdict(int)
dp[0, n + 1] = 0
while hp:
	(e, i) = heappop(hp)
	e = -e
	dp2 = defaultdict(int)
	for (k, v) in dp.items():
		(l, r) = k
		dp2[l + 1, r] = max(dp2[l + 1, r], v + abs(l + 1 - i) * e)
		dp2[l, r - 1] = max(dp2[l, r - 1], v + abs(r - 1 - i) * e)
	dp = dp2
ans = max(dp.values())
print(ans)


import sys
input = sys.stdin.buffer.readline

def main():
	n = int(input())
	A = list(map(int, input().split()))
	L = [(a, i + 1) for (i, a) in enumerate(A)]
	L.sort(reverse=True)
	dp = [[-1] * (n + 1) for _ in range(n + 1)]
	dp[0][0] = 0
	for i in range(n + 1):
		for j in range(n + 1 - i):
			(a, idx) = L[i + j - 1]
			if i:
				dp[i][j] = max(dp[i][j], dp[i - 1][j] + a * (idx - i))
			if j:
				dp[i][j] = max(dp[i][j], dp[i][j - 1] + a * (n + 1 - j - idx))
	ans = 0
	for i in range(n + 1):
		ans = max(ans, dp[i][n - i])
	print(ans)
main()


n = int(input())
a = [(int(x), i + 1) for (i, x) in enumerate(input().split())]
a = sorted(a, reverse=True)
dp = [[0] * (n + 1) for __ in range(n + 1)]
for k in range(1, n + 1):
	child = a[k - 1][0]
	ind = a[k - 1][1]
	for i in range(k + 1):
		if i != 0:
			dp[i][k - i] = dp[i - 1][k - i] + child * (ind - i)
		if k - i != 0:
			dp[i][k - i] = max(dp[i][k - i], dp[i][k - i - 1] + child * (n + 1 - (k - i) - ind))
ans = 0
for i in range(n + 1):
	ans = max(ans, dp[i][n - i])
print(ans)


n = int(input())
(*a,) = map(int, input().split())
dp = [0]
aii = [(ai, i) for (i, ai) in enumerate(a)]
aii.sort(key=lambda x: -x[0])
for (j, (ai, i)) in enumerate(aii):
	dp2 = [0] * (j + 2)
	dp2[0] = dp[0] + ai * abs(n - j - 1 - i)
	dp2[-1] = dp[-1] + ai * abs(j - i)
	for k in range(j):
		dp2[k + 1] = max(dp[k] + ai * abs(k - i), dp[k + 1] + ai * abs(n - (j - k - 1) - 1 - i))
	dp = dp2
print(max(dp))


N = int(input())
A = list(map(int, input().split()))
infants = []
for (i, a) in enumerate(A):
	infants.append((a, i))
infants.sort(reverse=True)
dp = [[0 for _ in range(N + 1)] for _ in range(N + 1)]
for i in range(N):
	for j in range(i + 1):
		dp[j + 1][i - j] = max(dp[j + 1][i - j], dp[j][i - j] + infants[i][0] * abs(infants[i][1] - j))
		dp[i - j][j + 1] = max(dp[i - j][j + 1], dp[i - j][j] + infants[i][0] * abs(infants[i][1] - (N - 1 - j)))
ans = 0
for i in range(N + 1):
	ans = max(ans, dp[i][N - i])
print(ans)


n = int(input())
a = sorted([(v, i) for (i, v) in enumerate(list(map(int, input().split())))])[::-1]
dp = [[0 for _ in range(n + 1)] for _ in range(n + 1)]
ans = 0
for i in range(n):
	for j in range(n - i):
		(v, p) = a[i + j]
		dp[i + 1][j] = max(dp[i + 1][j], dp[i][j] + v * abs(n - 1 - i - p))
		dp[i][j + 1] = max(dp[i][j + 1], dp[i][j] + v * abs(j - p))
		ans = max(ans, dp[i + 1][j], dp[i][j + 1])
print(ans)


n = int(input())
a = list(map(int, input().split()))
b = [(i, a[i]) for i in range(n)]
b = sorted(b, key=lambda x: x[1])[::-1]
dp = [[0] * (n + 1) for _ in range(n + 1)]
ans = 0
for l in range(0, n + 1):
	for r in range(0, n + 1):
		if l + r == n:
			ans = max(ans, dp[l][r])
			break
		(idx, activeness) = b[l + r]
		dp[l + 1][r] = max(dp[l + 1][r], dp[l][r] + abs(idx - l) * activeness)
		dp[l][r + 1] = max(dp[l][r + 1], dp[l][r] + (n - 1 - r - idx) * activeness)
print(ans)


N = int(input())
A = [int(i) for i in input().split()]
ap = [(v, k) for (k, v) in enumerate(A)]
ap.sort(reverse=True)
dp = [[-float('inf')] * (N + 1) for _ in range(N + 1)]
dp[0][0] = 0
for (i, (a, p)) in enumerate(ap):
	for l in range(i + 1):
		r = i - l
		if l + 1 <= N:
			dp[l + 1][r] = max(dp[l + 1][r], dp[l][r] + a * (p - l))
		if r + 1 <= N:
			dp[l][r + 1] = max(dp[l][r + 1], dp[l][r] + a * (N - 1 - r - p))
print(max((dp[i][N - i] for i in range(N + 1))))


def solve(N, A):
	a = [(i, ai) for (i, ai) in enumerate(A)]
	a.sort(key=lambda x: x[1], reverse=True)
	dp = [[0] * (N + 1) for _ in range(N + 1)]
	dp[0][0] = 0
	for (i, ai) in enumerate(a):
		for l in range(i + 1):
			r = i - l
			dp[i + 1][l] = max(dp[i + 1][l], dp[i][l] + ai[1] * (N - 1 - r - ai[0]))
			dp[i + 1][l + 1] = max(dp[i + 1][l + 1], dp[i][l] + ai[1] * (ai[0] - l))
	print(max(dp[N]))
N = int(input())
A = list(map(int, input().split()))
solve(N, A)


N = int(input())
S = list(map(int, input().split()))
data = [0] * N
for (i, item) in enumerate(S):
	data[i] = [item, i]
data.sort(reverse=True)
dp = [[0] * (N + 1) for _ in range(N + 1)]
for i in range(N):
	for j in range(N):
		if i + j >= N:
			break
		dp[i + 1][j] = max(dp[i + 1][j], dp[i][j] + data[i + j][0] * (data[i + j][1] - i))
		dp[i][j + 1] = max(dp[i][j + 1], dp[i][j] + data[i + j][0] * (N - 1 - j - data[i + j][1]))
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 = sorted([(a, i) for (i, a) in enumerate(A)], reverse=True)
dp = [0] * (N + 1)
for i in range(N):
	dp2 = dp[:]
	for l in range(i + 1):
		(a, pos) = A[i]
		dp2[l + 1] = max(dp2[l + 1], dp[l] + abs(pos - l) * a)
		r = i - l + 1
		dp2[l] = max(dp2[l], dp[l] + abs(N - r - pos) * a)
	dp = dp2
print(max(dp))


N = int(input())
A = map(int, input().split())
AI = sorted(((a, i) for (i, a) in enumerate(A, 1)), reverse=True)

def solve(a, i, prev):
	(pl, pr, ps) = (i, 0, 0)
	for (l, r, s) in prev:
		yield (l, r - 1, max(s + abs(r - i) * a, ps + abs(i - pl) * a))
		(pl, pr, ps) = (l, r, s)
	yield (pl + 1, pr, ps + abs(i - pl) * a)
prev = [(1, N, 0)]
for (a, i) in AI:
	prev = [*solve(a, i, prev)]
print(max((s for (l, r, s) in prev)))


def main():
	(N, *A) = map(int, open(0).read().split())
	A = sorted(enumerate(A), reverse=True, key=lambda x: x[1])
	dp = [[0] * r for r in reversed(range(1, N + 2))]
	for l in range(N):
		for r in range(N - l):
			(p, a) = A[l + r]
			dp[l + 1][r] = max(dp[l + 1][r], a * (p - l) + dp[l][r])
			dp[l][r + 1] = max(dp[l][r + 1], a * (N - 1 - r - p) + dp[l][r])
	print(max((dp[l][N - l] for l in range(N))))
main()


N = int(input())
_A = sorted(enumerate(map(int, input().split()), 1), key=lambda x: x[1], reverse=True)
dp = [[0] * (N + 1) for i in range(N + 1)]
for i in range(1, N + 1):
	(k, Ak) = _A[i - 1]
	if N - i - k < 0:
		break
	dp[0][i] = dp[0][i - 1] + (N - i + 1 - k) * Ak
for i in range(1, N + 1):
	(k, Ak) = _A[i - 1]
	if k - i < 0:
		break
	dp[i][0] = dp[i - 1][0] + (k - i) * Ak
for x in range(1, N + 1):
	for y in range(1, N - x + 1):
		(k, val) = _A[x + y - 1]
		dp[x][y] = max(dp[x - 1][y] + abs(k - x) * val, dp[x][y - 1] + abs(N - y - k + 1) * val)
print(int(max((dp[i][N - i] for i in range(N + 1)))))


def main():
	N = int(input())
	A = list(map(int, input().split()))
	dp = [[0] * (N + 1 - i) for i in range(N + 1)]
	A = list(enumerate(A))
	A.sort(key=lambda x: x[1])
	ans = 0
	for z in range(1, N + 1):
		tmp = A.pop()
		i = tmp[0] + 1
		a = tmp[1]
		for x in range(z + 1):
			y = z - x
			if x == 0:
				dp[x][y] = max(dp[x][y - 1] + a * abs(i - (N - y + 1)), dp[x][y])
			elif y == 0:
				dp[x][y] = max(dp[x - 1][y] + a * abs(i - x), dp[x][y])
			else:
				dp[x][y] = max(dp[x][y - 1] + a * abs(i - (N - y + 1)), dp[x][y], dp[x - 1][y] + a * abs(i - x))
			ans = max(ans, dp[x][y])
	print(ans)

def __starting_point():
	main()
__starting_point()


N = int(input())
_L = list(map(int, input().split()))
L = [(v, i) for (i, v) in enumerate(_L)]
L = sorted(L, key=lambda x: -x[0])
dp = [[0] * (N + 1) for _ in range(N + 1)]
for i in range(N):
	for k in range(N):
		if k > i:
			continue
		(val, ind) = L[i]
		dp[i + 1][k + 1] = max(dp[i + 1][k + 1], dp[i][k] + val * abs(ind - k))
		dp[i + 1][k] = max(dp[i + 1][k], dp[i][k] + val * abs(N - 1 - (i - k) - ind))
print(max(dp[-1]))


N = int(input())
A = list(map(int, input().split()))
d = {}
for (i, j) in enumerate(A):
	d[i] = j
d = sorted(d.items(), key=lambda x: x[1], reverse=True)
dp = [[0] * (N + 1) for _ in range(N + 1)]
c = 0
for (k, v) in d:
	for i in range(c + 1):
		dp[i + 1][c - i] = max(dp[i + 1][c - i], dp[i][c - i] + abs(v * (k - i)))
		dp[i][c - i + 1] = max(dp[i][c - i + 1], dp[i][c - i] + abs(v * (N - (c - i) - k - 1)))
	c += 1
ans = 0
for i in range(N):
	ans = max(ans, dp[i][N - i])
print(ans)


def main():
	from operator import itemgetter
	import sys
	sys.setrecursionlimit(10 ** 9)
	input = sys.stdin.readline
	N = int(input())
	A = list(map(int, input().split()))
	AP = []
	for i in range(N):
		AP.append((A[i], i))
	AP.sort(key=itemgetter(0), reverse=True)
	dp = [[0] * (N + 1) for _ in range(N + 1)]
	for i in range(N):
		(ai, pi) = AP[i]
		for l in range(i + 1):
			r = i - l
			if dp[i + 1][l + 1] < dp[i][l] + (pi - l) * ai:
				dp[i + 1][l + 1] = dp[i][l] + (pi - l) * ai
			if dp[i + 1][l] < dp[i][l] + (N - r - 1 - pi) * ai:
				dp[i + 1][l] = dp[i][l] + (N - r - 1 - pi) * ai
	print(max(dp[N]))
main()


n = int(input())
a = list(map(int, input().split()))
a = [(num, i) for (i, num) in enumerate(a)]
a.sort(reverse=True)
dp = [[0] * (n + 1) for _ in range(n + 1)]
for i in range(n + 1):
	for j in range(n - i + 1):
		if i > 0:
			dp[i][j] = max(dp[i][j], dp[i - 1][j] + a[i + j - 1][0] * abs(a[i + j - 1][1] - (i - 1)))
		if j > 0:
			dp[i][j] = max(dp[i][j], dp[i][j - 1] + a[i + j - 1][0] * abs(a[i + j - 1][1] - (n - j)))
ans = max((dp[i][n - i] for i in range(n + 1)))
print(ans)


n = int(input())
d = [0] + [-10 ** 18] * n
for (j, (a, i)) in enumerate(sorted(((-a, i) for (i, a) in enumerate(map(int, input().split()))))):
	d = [max(t - a * abs(~i - j + k + n), d[k - 1] - a * abs(~i + k)) for (k, t) in enumerate(d)]
print(max(d))


(n, *a) = map(int, open(0).read().split())
a = sorted([(j, i) for (i, j) in enumerate(a)])[::-1]
dp = [[-1] * (n + 1) for _ in range(n + 1)]
dp[0][0] = 0
for (m, (a_k, k)) in enumerate(a):
	for i in range(m + 1):
		j = m - i
		dp[i + 1][j] = max(dp[i + 1][j], dp[i][j] + a_k * abs(k - i))
		dp[i][j + 1] = max(dp[i][j + 1], dp[i][j] + a_k * abs(k - (n - 1 - j)))
ans = 0
for i in range(n + 1):
	ans = max(ans, dp[i][n - i])
print(ans)


e = enumerate
(n, a) = open(0)
n = int(n)
d = [0] + [-9e+18] * n
for (j, (a, i)) in e(sorted(((int(a), i) for (i, a) in e(a.split())))[::-1]):
	d = [max(t + a * abs(~i - j + k + n), d[k - 1] + a * abs(~i + k) if k else 0) for (k, t) in e(d)]
print(max(d))


import sys, math, copy, queue, itertools, bisect
LI = lambda : [int(x) for x in sys.stdin.readline().split()]
NI = lambda : int(sys.stdin.readline())
N = NI()
data = [[a, i] for (i, a) in enumerate(LI())]
data.sort(reverse=True)
dp = [[0 for _ in range(N + 1)] for _ in range(N + 1)]
for i in range(N):
	(a, p) = data[i]
	for j in range(i + 1):
		dp[i + 1][j + 1] = max(dp[i + 1][j + 1], dp[i][j] + abs(N - 1 - j - p) * a)
		dp[i + 1][j] = max(dp[i + 1][j], dp[i][j] + abs(i - j - p) * a)
print(max(dp[-1]))


N = int(input())
A = list(map(int, input().split()))
B = [(A[i], i) for i in range(N)]
B.sort(key=lambda x: x[0], reverse=True)
DP = [[0] * (N + 1) for _ in range(N + 1)]
for i in range(N):
	a = B[i][0]
	tmp = B[i][1]
	for x in range(i + 1):
		DP[x + 1][i - x] = max(DP[x + 1][i - x], DP[x][i - x] + a * abs(tmp - x))
		DP[x][i - x + 1] = max(DP[x][i - x + 1], DP[x][i - x] + a * abs(N - tmp - 1 - (i - x)))
ans = 0
for i in range(N + 1):
	ans = max(ans, DP[i][N - i])
print(ans)


n = int(input())
l = sorted([(v, i + 1) for (i, v) in enumerate(list(map(int, input().split())))], reverse=1)
dp = [[0] * (n + 1) for i in range(n + 1)]
for ko in range(1, n + 1):
	for x in range(ko + 1):
		ret = 0
		(v, i) = l[ko - 1]
		y = ko - x
		if y >= 1:
			ret = max(ret, dp[x][y - 1] + abs(n - y + 1 - i) * v)
		if x >= 1:
			ret = max(ret, dp[x - 1][y] + abs(x - i) * v)
		dp[x][y] = ret
print(max((dp[a][n - a] for a in range(n + 1))))


import sys
input = sys.stdin.readline
n = int(input())
a = [int(i) for i in input().split()]
b = [int(i) for i in range(n)]
ab = [(x, y) for (x, y) in zip(a, b)]
from operator import itemgetter
ab = tuple(sorted(ab, key=itemgetter(0), reverse=True))
dp = [[-10 ** 14] * (n + 1) for i in range(n + 1)]
dp[0][0] = 0
for i in range(n):
	for j in range(i + 2):
		dp[i + 1][j] = max(dp[i][j] + ab[i][0] * abs(ab[i][1] - (n - 1 - (i - j))), dp[i][j - 1] + ab[i][0] * abs(ab[i][1] + 1 - j))
print(max(dp[n]))


n = int(input())
a = list(map(int, input().split()))
b = list(((x, i) for (i, x) in enumerate(a)))
b.sort(reverse=True)
dp = [[0] * (n + 1 - i) for i in range(n + 1)]
for i in range(n):
	(x, idx) = b[i]
	for j in range(i + 1):
		k = i - j
		if dp[j + 1][k] < dp[j][k] + x * (idx - j):
			dp[j + 1][k] = dp[j][k] + x * (idx - j)
		if dp[j][k + 1] < dp[j][k] + x * (n - 1 - k - idx):
			dp[j][k + 1] = dp[j][k] + x * (n - 1 - k - idx)
ans = 0
for i in range(n + 1):
	if ans < dp[i][n - i]:
		ans = dp[i][n - i]
print(ans)


def solve(n, aaa):
	dp = [0]
	aaa_with_idx = list(zip(aaa, range(n)))
	aaa_with_idx.sort(reverse=True)
	for k in range(1, n + 1):
		ndp = [0] * (k + 1)
		(a, i) = aaa_with_idx[k - 1]
		for l in range(k):
			ndp[l + 1] = max(ndp[l + 1], dp[l] + a * abs(i - l))
			r = n - (k - l)
			ndp[l] = max(ndp[l], dp[l] + a * abs(i - r))
		dp = ndp
	return max(dp)
n = int(input())
aaa = list(map(int, input().split()))
print(solve(n, aaa))


N = int(input())
A = list(map(int, input().split()))
infants = [[i, A[i]] for i in range(N)]
infants.sort(key=lambda x: x[1], reverse=True)
dp = [[0 for _ in range(N + 1)] for _ in range(N + 1)]
for x in range(N):
	for y in range(N - x):
		(pos, infant) = infants[x + y]
		dp[x][y + 1] = max(dp[x][y + 1], dp[x][y] + infant * abs(N - 1 - y - pos))
		dp[x + 1][y] = max(dp[x + 1][y], dp[x][y] + infant * abs(pos - x))
ans = 0
for i in range(N):
	ans = max(ans, dp[i][N - i])
print(ans)


N = int(input())
A = list(map(int, input().split()))
p = list(range(N))
p.sort(key=lambda i: A[i], reverse=True)
dp = [[0] * (N + 1) for _ in range(N + 1)]
for i in range(N):
	for j in range(i + 1):
		pi = p[i]
		dp[i + 1][j] = max(dp[i + 1][j], dp[i][j] + A[pi] * (N - i + j - 1 - pi))
		dp[i + 1][j + 1] = dp[i][j] + A[pi] * (pi - j)
print(max(dp[N]))


n = int(input())
A = list(map(int, input().split()))
a = []
for i in range(len(A)):
	a.append([A[i], i])
a.sort(key=lambda x: x[0], reverse=True)
dp = [[0 for i in range(n + 1)] for j in range(n + 1)]
for i in range(n):
	for l in range(i + 1):
		dp[i + 1][l] = max(dp[i + 1][l], dp[i][l] + a[i][0] * (n - 1 - (i - l) - a[i][1]))
		dp[i + 1][l + 1] = max(dp[i + 1][l + 1], dp[i][l] + a[i][0] * (a[i][1] - l))
ans = 0
for now in dp[n]:
	ans = max(now, ans)
print(ans)


n = int(input())
a = sorted([(v, k) for (k, v) in enumerate(list(map(int, input().split())))], reverse=True)
dp = [[-10 ** 18] * (n + 1) for i in range(n + 1)]
dp[0][0] = 0
for (i, (v, k)) in enumerate(a):
	for j in range(i + 1):
		left = j
		right = n - 1 - (i - j)
		dp[i + 1][j] = max(dp[i + 1][j], dp[i][j] + v * abs(k - right))
		dp[i + 1][j + 1] = max(dp[i + 1][j + 1], dp[i][j] + v * abs(k - left))
print(max(dp[i + 1]))


import sys
input = sys.stdin.readline
n = int(input())
A = list(map(int, input().split()))
B = [[A[i], i] for i in range(n)]
B.sort(reverse=True)
DP = [[0] * (n + 1) for i in range(n)]
DP[0][0] = B[0][0] * B[0][1]
DP[0][1] = B[0][0] * (n - 1 - B[0][1])
for i in range(1, n):
	ind = B[i][1]
	for j in range(i + 2):
		if j - 1 >= 0:
			DP[i][j] = DP[i - 1][j - 1] + B[i][0] * abs(n - 1 - (j - 1) - ind)
		DP[i][j] = max(DP[i][j], DP[i - 1][j] + B[i][0] * abs(ind - (i - 1 + 1 - j)))
print(max(DP[-1]))


e = enumerate
(n, a) = open(0)
n = int(n)
d = [0]
for (j, (a, i)) in e(sorted(((int(a), i) for (i, a) in e(a.split())))[::-1]):
	d = [d[0] + a * (~i - j + n)] + [max(d[k + 1] + a * (n + k - i - j), t + a * abs(i - k)) for (k, t) in e(d[:j])] + [d[j] + a * abs(i - j)]
print(max(d))


import sys
input = sys.stdin.readline
n = int(input())
A = list(map(int, input().split()))
cnt = [0, n - 1]
As = sorted(list(zip(A, range(n))), reverse=True)
dp = [[0] * (n + 1) for _ in range(n + 1)]
for i in range(n):
	for j in range(i + 1):
		(a, p) = As[i]
		dp[i + 1][j] = max(dp[i + 1][j], dp[i][j] + a * (n - 1 - p - i + j))
		dp[i + 1][j + 1] = max(dp[i + 1][j + 1], dp[i][j] + a * (p - j))
print(max(dp[n]))


def main():
	N = int(input())
	A = list(map(int, input().split()))
	AA = [(v, i) for (i, v) in enumerate(A)]
	AA.sort(reverse=True)
	dp = [[0] * (N + 1) for _ in range(N + 1)]
	for len in range(1, N + 1):
		(val, index) = AA[len - 1]
		for x in range(0, len + 1):
			y = len - x
			if x == 0:
				dp[x][y] = dp[x][y - 1] + val * abs(N - y - index)
			elif y == 0:
				dp[x][y] = dp[x - 1][y] + val * abs(index - x + 1)
			else:
				dp[x][y] = max(dp[x][y - 1] + val * abs(N - y - index), dp[x - 1][y] + val * abs(index - x + 1))
	maxes = []
	for i in dp:
		maxes.append(max(i))
	return max(maxes)
print(main())


N = int(input())
D = []
for (i, a) in enumerate(map(int, input().split())):
	D.append((a << 11) + i)
D.sort(reverse=True)
dp = [0] * (N + 1)
for (i, d) in enumerate(D, start=1):
	(x, a) = (d % (1 << 11), d >> 11)
	for j in reversed(range(0, i + 1)):
		dp[j] = max((dp[j - 1] + a * abs(x - (j - 1))) * (j - 1 >= 0), (dp[j] + a * abs(N - (i - j) - x)) * (j <= i - 1))
print(max(dp))


n = int(input())
a = list(map(int, input().split()))
a = list(enumerate(a))
a.sort(key=lambda x: x[1])
dp = [[] for _ in range(n + 1)]
dp[0].append(0)
for i in range(1, n + 1):
	(ind, act) = a.pop(-1)
	dp[i].append(dp[i - 1][0] + act * (n - i - ind))
	for j in range(1, i):
		ar = dp[i - 1][j] + act * (n - i + j - ind)
		al = dp[i - 1][j - 1] + act * (ind - j + 1)
		dp[i].append(max([ar, al]))
	dp[i].append(dp[i - 1][i - 1] + act * (ind - i + 1))
print(max(dp[n]))


(N, *A) = map(int, open(0).read().split())
A = sorted(enumerate(A), reverse=True, key=lambda x: x[1])
dp = [[0] * (N + 1) for _ in range(N + 1)]
for (i, (p, a)) in enumerate(A):
	for j in range(i + 1):
		dp[i + 1][j + 1] = max(dp[i + 1][j + 1], dp[i][j] + a * (p - j))
		dp[i + 1][j] = max(dp[i + 1][j], dp[i][j] + a * (N - (i - j) - 1 - p))
print(max(dp[N]))


def main():
	n = int(input())
	tmp = list(map(int, input().split()))
	a = [[v, i] for (i, v) in enumerate(tmp)]
	a.sort(reverse=True)
	dp = [[0] * (n + 1) for _ in range(n + 1)]
	for i in range(n):
		(v, idx) = a[i]
		for j in range(i + 1):
			dp[i + 1][j + 1] = max(dp[i + 1][j + 1], dp[i][j] + v * abs(idx - j))
			dp[i + 1][j] = max(dp[i + 1][j], dp[i][j] + v * abs(idx - (n - 1 - i + j)))
	print(max(dp[n]))
main()


n = int(input())
al = list(map(int, input().split()))
a_val_ind_l = []
for (i, a) in enumerate(al):
	a_val_ind_l.append((a, i))
a_val_ind_l.sort(reverse=True)
dp = [[0] * (n + 1) for _ in range(n + 1)]
for i in range(n):
	for j in range(0, n - i):
		dp[i + 1][j] = max(dp[i][j] + a_val_ind_l[i + j][0] * abs(a_val_ind_l[i + j][1] - i), dp[i + 1][j])
		dp[i][j + 1] = max(dp[i][j] + a_val_ind_l[i + j][0] * abs(n - 1 - a_val_ind_l[i + j][1] - j), dp[i][j + 1])
ans = 0
for row in dp:
	row_max = max(row)
	ans = max(row_max, ans)
print(ans)


import pprint
N = int(input())
A = list(map(int, input().split()))

def student_sorting(N, A):
	student_list = [0] * N
	for (initial_pos, activity) in enumerate(A):
		student_list[initial_pos] = (activity, initial_pos)
	return sorted(student_list, reverse=True)

def DP(N, student_list):
	DP_map = list((list((0 for row in range(N + 1 - column))) for column in range(N + 1)))
	ans_list = []
	ans = 0
	for left in range(N):
		activity = student_list[left][0]
		distance_l = abs(left - student_list[left][1])
		DP_map[left + 1][0] = DP_map[left][0] + activity * distance_l
	for right in range(N):
		activity = student_list[right][0]
		distance_r = abs(N - 1 - right - student_list[right][1])
		DP_map[0][right + 1] = DP_map[0][right] + activity * distance_r
	for left in range(1, N + 1):
		for right in range(1, N - left + 1):
			activity = student_list[left + right - 1][0]
			distance_l = abs(left - 1 - student_list[left + right - 1][1])
			distance_r = abs(N - right - student_list[left + right - 1][1])
			score_if_appended_to_left = DP_map[left - 1][right] + activity * distance_l
			score_if_appended_to_right = DP_map[left][right - 1] + activity * distance_r
			DP_map[left][right] = max(score_if_appended_to_left, score_if_appended_to_right)
	for left in range(N + 1):
		row = N - left
		column = left
		ans_list.append(DP_map[column][row])
	ans = max(ans_list)
	return ans
student_list = student_sorting(N, A)
ans = DP(N, student_list)
print(ans)


N = int(input())
A = list(map(int, input().split()))
dp = [[0 for _ in range(N + 1)] for _ in range(N + 1)]
p = [(a, i) for (i, a) in enumerate(A)]
p.sort(reverse=True)
for i in range(N):
	pi = p[i][1]
	for l in range(i + 1):
		dp[i + 1][l + 1] = max(dp[i + 1][l + 1], dp[i][l] + (pi - l) * A[pi])
		r = i - l
		dp[i + 1][l] = max(dp[i + 1][l], dp[i][l] + (N - r - 1 - pi) * A[pi])
print(max(dp[N]))


import heapq
import time
st = time.time()
n = int(input())
a = list(map(int, input().split()))
hq = []
for (i, ai) in enumerate(a):
	heapq.heappush(hq, (-ai, i))
dp = [[0 for i in range(j + 1)] for j in range(n + 1)]
dp[0][0] = 0
(ai, hi) = heapq.heappop(hq)
dp[1][0] = -ai * (n - 1 - hi)
dp[1][1] = -ai * hi
for i in range(2, n + 1):
	(ai, hi) = heapq.heappop(hq)
	dp[i][0] = dp[i - 1][0] - ai * abs(n - i - hi)
	dp[i][i] = dp[i - 1][i - 1] - ai * abs(hi - i + 1)
	for j in range(1, i):
		dp[i][j] = max(dp[i - 1][j - 1] - ai * abs(hi - j + 1), dp[i - 1][j] - ai * abs(n - i + j - hi))
print(max(dp[n]))


import sys
input = sys.stdin.readline
N = int(input())
a = list(map(int, input().split()))
sa = [(a[i], i) for i in range(N)]
sa.sort(key=lambda x: (-x[0], -abs(x[1] - (N - 1) / 2)))
dp = [[0] * (N + 1) for _ in range(N + 1)]
for k in range(N):
	(x, i) = sa[k]
	for l in range(k + 1):
		r = k - l
		nr = N - 1 - r
		dp[l + 1][r] = max(dp[l + 1][r], dp[l][r] + abs(l - i) * x)
		dp[l][r + 1] = max(dp[l][r + 1], dp[l][r] + abs(nr - i) * x)
res = 0
for l in range(N):
	r = N - l
	res = max(res, dp[l][r])
print(res)


N = int(input())
(*A,) = map(int, input().split())
B = [(i + 1, a) for (i, a) in enumerate(A)]
from operator import itemgetter
B.sort(reverse=True, key=itemgetter(1))
dp = [[-1] * (N + 1) for _ in range(N + 1)]
dp[0][0] = 0
for i in range(1, N + 1):
	(idx, act) = B[i - 1]
	for j in range(i + 1):
		k = i - j
		if 0 < j:
			dp[j][k] = max(dp[j][k], dp[j - 1][k] + act * abs(idx - j))
		if 0 < k:
			dp[j][k] = max(dp[j][k], dp[j][k - 1] + act * abs(idx - (N - k + 1)))
ans = 0
for j in range(N + 1):
	ans = max(ans, dp[j][N - j])
print(ans)


n = int(input())
a = list(map(int, input().split()))
assert len(a) == n
p = list(range(n))
p.sort(key=lambda i: a[i])
dp = [0] * (n + 1)
for i in range(n):
	for j in range(n - i):
		dp[j] = max(dp[j] + a[p[i]] * abs(p[i] - (j + i)), dp[j + 1] + a[p[i]] * abs(p[i] - j))
print(dp[0])


n = int(input())
a = map(int, input().split())
a = sorted(((ax, x) for (x, ax) in enumerate(a)), reverse=True)
dp = [[-1] * (n + 1) for _ in range(n + 1)]
dp[0][0] = 0
for (i, (ax, x)) in enumerate(a):
	for j in range(i + 1):
		dp[j + 1][i - j] = max(dp[j + 1][i - j], dp[j][i - j] + ax * abs(x - j))
		dp[j][i - j + 1] = max(dp[j][i - j + 1], dp[j][i - j] + ax * abs(n - 1 - x - i + j))
print(max((dp[i][n - i] for i in range(n + 1))))


(n, *a) = map(int, open(0).read().split())
a = sorted([(j, i) for (i, j) in enumerate(a)], reverse=True)
dp = [0]
for (i, (j, k)) in enumerate(a):
	ldp = [abs(k - i + a) * j + b for (a, b) in enumerate(dp)]
	rdp = [abs(n - 1 - k - a) * j + b for (a, b) in enumerate(dp)]
	dp = [max(a, b) for (a, b) in zip(ldp + [0], [0] + rdp)]
print(max(dp))


import sys
input = sys.stdin.readline
N = int(input())
A = list(map(int, input().split()))
dp = [[0] * (N + 1) for _ in range(N + 1)]
B = [(a, i) for (i, a) in enumerate(A)]
B.sort(reverse=True)
for (l, (a, i)) in enumerate(B):
	length = N - l
	for left in range(l + 1):
		dp[left + 1][left + length] = max(dp[left + 1][left + length], dp[left][left + length] + a * abs(i - left))
		dp[left][left + length - 1] = max(dp[left][left + length - 1], dp[left][left + length] + a * abs(left + length - 1 - i))
ans = 0
for n in range(N + 1):
	ans = max(ans, dp[n][n])
print(ans)


import sys
sys.setrecursionlimit(2000 * 2)
n = int(input())
a = list(map(int, input().split()))
a = [(i, v) for (i, v) in enumerate(a)]
a.sort(key=lambda x: x[1], reverse=True)
dp = [[-1] * (m + 1) for m in range(n + 1)]

def f(m, l):
	if m == n:
		return 0
	if dp[m][l] != -1:
		return dp[m][l]
	r = n - m + l - 1
	(i, v) = a[m]
	if i >= l:
		dp[m + 1][l + 1] = f(m + 1, l + 1)
	if i <= r:
		dp[m + 1][l] = f(m + 1, l)
	return max(dp[m + 1][l + 1] + v * (i - l), dp[m + 1][l] + v * (r - i))
print(f(0, 0))


n = int(input())
a = map(int, input().split())
babies = [(a, i) for (i, a) in enumerate(a)]
babies.sort(reverse=True)
dp = [[0] * (n + 1) for i in range(n + 1)]
ans = 0
for l in range(n):
	for r in range(n):
		if l + r == n:
			ans = max(ans, dp[l][r])
			break
		(activeness, ind) = babies[l + r]
		dp[l + 1][r] = max(dp[l + 1][r], dp[l][r] + abs(ind - l) * activeness)
		dp[l][r + 1] = max(dp[l][r + 1], dp[l][r] + abs(ind - (n - 1 - r)) * activeness)
print(ans)


n = int(input())
A = list(map(int, input().split()))
p = []
for (i, v) in enumerate(A):
	p.append((v, i))
p.sort(reverse=True)
dp = [[0] * (n + 1) for _ in range(n + 1)]
for i in range(n):
	k = p[i][0]
	r = p[i][1]
	for x in range(i + 1):
		y = i - x
		dp[x + 1][y] = max(dp[x + 1][y], dp[x][y] + abs(r - x) * k)
		dp[x][y + 1] = max(dp[x][y + 1], dp[x][y] + abs(r - (n - 1 - y)) * k)
ans = 0
for i in range(n):
	ans = max(ans, dp[i][n - i])
print(ans)


import sys
input = sys.stdin.buffer.readline
n = int(input())
a = list(map(int, input().split()))
b = []
for (i, v) in enumerate(a):
	b.append([v, i])
b.sort(reverse=True)
dp = [[0] * (n + 1) for _ in range(n + 1)]
ans = 0
for i in range(n + 1):
	for j in range(n + 1 - i):
		if i > 0:
			dp[i][j] = max(dp[i][j], dp[i - 1][j] + b[i + j - 1][0] * (b[i + j - 1][1] - (i - 1)))
		if j > 0:
			dp[i][j] = max(dp[i][j], dp[i][j - 1] + b[i + j - 1][0] * (n - j - b[i + j - 1][1]))
for i in range(n + 1):
	ans = max(ans, dp[i][n - i])
print(ans)


n = int(input())
al = list(map(int, input().split()))
al_id = [(a, idx) for (idx, a) in enumerate(al)]
al_id = sorted(al_id, reverse=True)
dp = [[-float('inf') for i in range(n + 1)] for j in range(n + 1)]
dp[0][0] = 0
for (count, (a, idx)) in enumerate(al_id):
	for i in range(count + 1):
		j = count - i
		dp[i + 1][j] = max(dp[i][j] + abs(idx - i) * a, dp[i + 1][j])
		dp[i][j + 1] = max(dp[i][j] + abs(n - j - idx - 1) * a, dp[i][j + 1])
ans = 0
for i in range(n + 1):
	j = n - i
	ans = max(dp[i][j], ans)
print(ans)


N = int(input())
A = list(map(int, input().split()))
dp = [[] for _ in range(N + 1)]
dp[0].append(0)
for i in range(N):
	A[i] = [A[i], i + 1]
A.sort(reverse=True)
for M in range(1, N + 1):
	(Ai, i) = A[M - 1]
	dp[M].append(dp[M - 1][0] + Ai * (N - (M - 1) - i))
	for x in range(1, M):
		dp[M].append(max(dp[M - 1][x - 1] + Ai * (i - x), dp[M - 1][x] + Ai * (N - (M - x - 1) - i)))
	dp[M].append(dp[M - 1][M - 1] + Ai * (i - M))
print(max(dp[N]))


def i1str():
	return input()

def istrs(n=None):
	if n is None:
		return input().split()
	a = []
	for _ in range(n):
		a.append(istrs())
	return a

def i1num():
	return int(input())

def inums(n=None):
	if n is None:
		return list(map(int, input().split()))
	a = []
	for _ in range(n):
		a.append(inums())
	return a

def ostrs(l, sp=' '):
	print(sp.join(l))

def onums(l, sp=' '):
	print(sp.join(map(str, l)))

def solve(n, aii):
	dp = [[0 for _ in range(n + 1)] for _ in range(n + 1)]
	for x in range(n + 1):
		for y in range(n + 1 - x):
			if x == y == 0:
				continue
			(ai, i) = aii[n - x - y]
			dp[x][y] = max(dp[x - 1][y] + ai * (i - x + 1) if x > 0 else 0, dp[x][y - 1] + ai * (n - y - i) if y > 0 else 0)
	res = 0
	for x in range(n + 1):
		if dp[x][n - x] > res:
			res = dp[x][n - x]
	return res

def __starting_point():
	n = i1num()
	a = inums()
	aii = sorted(((a[i], i) for i in range(n)))
	print(solve(n, aii))
__starting_point()


def resolve():
	N = int(input())
	A = list(map(int, input().split()))
	A = sorted([(a, i) for (i, a) in enumerate(A)], reverse=True)
	dp = [[0] * (N + 1) for _ in range(N + 1)]
	for (i, (a, p)) in enumerate(A):
		for j in range(i + 1):
			dp[i + 1][j + 1] = max(dp[i + 1][j + 1], dp[i][j] + (p - j) * a)
			dp[i + 1][j] = max(dp[i + 1][j], dp[i][j] + (N - (i - j) - 1 - p) * a)
	print(max(dp[-1]))

def __starting_point():
	resolve()
__starting_point()


from collections import *
from itertools import *
N = int(input())
A = list(map(int, input().split()))
SA = SB = 0
ABI = sorted((((b := (max(N - i, i - 1) * a)), (SA := (SA + a)), (SB := (SB + b))) and (a, b, i) for (i, a) in enumerate(A, 1)), reverse=True)
prev = {0: 0}
prev_max = 0
for (k, (a, b, i)) in enumerate(ABI):
	curr = defaultdict(int)
	curr_max = 0
	for (l, p) in list(prev.items()):
		r = k - l
		if p + SB - min(l, r) * SA < prev_max:
			continue
		curr[l] = max(curr[l], p + abs(N - i - r) * a)
		curr[l + 1] = p + abs(i - l - 1) * a
		curr_max = max(curr_max, curr[l], curr[l + 1])
	SA -= a
	SB -= b
	prev = curr
	prev_max = curr_max
print(curr_max)


n = int(input())
A = list(map(int, input().split()))
A = sorted([(a, i) for (i, a) in enumerate(A)], reverse=True)
dp = [[0] * (n + 1) for i in range(n + 1)]
for i in range(n):
	for j in range(n - i):
		(a, k) = A[i + j]
		dp[i + 1][j] = max(dp[i + 1][j], dp[i][j] + abs(k - i) * a)
		dp[i][j + 1] = max(dp[i][j + 1], dp[i][j] + abs(n - j - 1 - k) * a)
ans = 0
for i in range(n + 1):
	ans = max(ans, max(dp[i]))
print(ans)


N = int(input())
As = list(map(int, input().split()))
inds = sorted(range(len(As)), key=lambda k: As[k])[::-1]
ds = [0]
for xy in range(1, N + 1):
	i = inds[xy - 1]
	a = As[i]
	nds = [0] * (xy + 1)
	nds[-1] = ds[-1] + a * (i - (xy - 1))
	nds[0] = ds[0] + a * (N - xy - i)
	for x in range(1, xy):
		y = xy - x
		c1 = ds[x - 1] + a * abs(i - (x - 1))
		c2 = ds[x] + a * abs(N - y - i)
		nds[x] = max(c1, c2)
	ds = nds.copy()
print(max(ds))


import sys
readline = sys.stdin.readline
N = int(readline())
A = list(map(int, readline().split()))
L = list(range(N))
L.sort(key=lambda x: A[x], reverse=True)
dp = [[0] * (N + 1) for _ in range(N + 1)]
for s in range(N):
	l = L[s]
	a = A[l]
	for i in range(s + 1):
		j = s - i
		dp[i + 1][j] = max(dp[i + 1][j], dp[i][j] + a * abs(l - i))
		dp[i][j + 1] = max(dp[i][j + 1], dp[i][j] + a * abs(l - (N - s + i - 1)))
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()))
babies = [(a, i) for (i, a) in enumerate(A)]
babies.sort(reverse=True)
dp = [[0] * (N + 1) for _ in range(N + 1)]
ans = 0
for l in range(N):
	for r in range(N):
		if l + r == N:
			ans = max(ans, dp[l][r])
			break
		(active, index) = babies[l + r]
		dp[l][r + 1] = max(dp[l][r + 1], dp[l][r] + abs(index - (N - 1 - r)) * active)
		dp[l + 1][r] = max(dp[l + 1][r], dp[l][r] + abs(index - l) * active)
print(ans)


from heapq import heappush, heappop
n = int(input())
A = list(map(int, input().split()))
f_inf = float('inf')
dp = [[0 for _ in range(n + 1)] for _ in range(n + 1)]
dp[0][0] = 0
hq = []
for i in range(n):
	heappush(hq, (-A[i], i))
ans = 0
for i in range(n):
	(x, pi) = heappop(hq)
	for l in range(i + 1):
		r = i - l
		dp[i + 1][l + 1] = max(dp[i + 1][l + 1], dp[i][l] + (pi - l) * A[pi])
		dp[i + 1][l] = max(dp[i + 1][l], dp[i][l] + (n - r - 1 - pi) * A[pi])
ans = 0
for i in range(n + 1):
	ans = max(ans, dp[n][i])
print(ans)


N = int(input())
A = list(map(int, input().split()))
A = sorted([(val, pos) for (pos, val) in enumerate(A)], reverse=True)
dp = [0]
for (i, (val, pos)) in enumerate(A):
	ldp = [e + val * abs(pos - (i - r)) for (r, e) in enumerate(dp)]
	rdp = [e + val * abs(N - 1 - r - pos) for (r, e) in enumerate(dp)]
	dp = [max(L, R) for (L, R) in zip(ldp + [0], [0] + rdp)]
print(max(dp))


from collections import defaultdict
n = int(input())
a = list(map(int, input().split()))
infants = []
for i in range(n):
	infants.append((a[i], i))
infants.sort()
dp = [defaultdict(int) for _ in range(n + 1)]
dp[0][0] = 0
for i in range(n):
	(score, initial) = infants.pop()
	for right in dp[i].keys():
		dp[i + 1][right] = max(dp[i][right] + abs(initial - (i - right)) * score, dp[i + 1][right])
		dp[i + 1][right + 1] = max(dp[i][right] + abs(n - 1 - right - initial) * score, dp[i + 1][right + 1])
print(max(dp[n].values()))


def solve():
	N = int(input())
	A = [(a, i + 1) for (i, a) in enumerate(map(int, input().split()))]
	A.sort(reverse=True)
	INF = float('inf')
	dp = [[-INF] * (N + 1) for _ in range(N + 1)]
	dp[0][0] = 0
	for s in range(1, N + 1):
		for l in range(s + 1):
			r = s - l
			dp[l][r] = max(dp[l - 1][r] + A[s - 1][0] * abs(A[s - 1][1] - l), dp[l][r - 1] + A[s - 1][0] * abs(N - r + 1 - A[s - 1][1]))
	ans = 0
	for m in range(N):
		if dp[m][N - m] > ans:
			ans = dp[m][N - m]
	print(ans)
solve()


def main():
	N = int(input())
	A = list(map(int, input().split()))
	AA = [(a, i) for (i, a) in enumerate(A)]
	AA.sort(reverse=True)
	dp = [[0] * (N + 1) for _ in range(N + 1)]
	for i in range(N):
		(a, t) = AA[i]
		for x in range(i + 2):
			y = i + 1 - x
			dp[x][y] = max(dp[x - 1][y] + abs(t - (x - 1)) * a if x > 0 else 0, dp[x][y - 1] + abs(t - (N - y)) * a if y > 0 else 0)
	return max((dp[i][N - i] for i in range(N + 1)))
print(main())


INF = float('inf')
N = int(input())
arr = list(map(int, input().split()))
A = []
for i in range(N):
	A.append((arr[i], i))
A.sort(key=lambda x: x[0], reverse=True)
dp = [[-INF] * (N + 1) for i in range(N + 1)]
dp[0][0] = 0
for i in range(N):
	(a, w) = A[i]
	for x in range(N + 1):
		y = i - x + 1
		if x > 0:
			dp[i + 1][x] = max(dp[i + 1][x], dp[i][x - 1] + a * abs(w - (x - 1)))
		if y >= 0:
			dp[i + 1][x] = max(dp[i + 1][x], dp[i][x] + a * abs(w - (N - y)))
print(max(dp[N]))


n = int(input())
a = list(map(int, input().split()))
for i in range(n):
	a[i] = [a[i], i]
a.sort()
a.reverse()
dp = [[0 for i in range(n + 1)] for j in range(n + 1)]
for i in range(n):
	for j in range(n):
		if i + j <= n - 1:
			dp[i + 1][j] = max(dp[i + 1][j], dp[i][j] + a[i + j][0] * abs(a[i + j][1] - i))
			dp[i][j + 1] = max(dp[i][j + 1], dp[i][j] + a[i + j][0] * abs(a[i + j][1] - (n - j - 1)))
ans = []
for i in range(n):
	ans.append(dp[i][n - i])
print(max(ans))


N = int(input())
A = list(map(int, input().split()))
A = sorted([(a, p) for (p, a) in enumerate(A)], reverse=True)
dp = [[0] * (i + 1) for i in range(N + 1)]
for z in range(N):
	for y in range(z + 1):
		dp[z + 1][y] = max(A[z][0] * (A[z][1] - (z - y)) + dp[z][y], dp[z + 1][y])
		dp[z + 1][y + 1] = A[z][0] * (N - y - 1 - A[z][1]) + dp[z][y]
print(max(dp[-1]))


def main():
	a = int(input())
	nums = list(map(int, input().split()))
	vilist = []
	for i in range(a):
		vilist.append((nums[i], i))
	vilist = sorted(vilist, key=lambda vi: vi[0])
	dp = []
	for i in range(a):
		dp.append([0] * (a - i))
	for k in range(a - 1):
		vi = vilist.pop()
		v = vi[0]
		p = vi[1]
		vi_z = vilist[0]
		vz = vi_z[0]
		pz = vi_z[1]
		for i in range(k + 2):
			j = k - i + 1
			if k == a - 2:
				if j == 0:
					dp[i][0] = dp[i - 1][0] + v * abs(p - i + 1) + vz * abs(pz - i)
				elif i == 0:
					dp[0][j] = dp[0][j - 1] + v * abs(a - j - p) + vz * abs(a - j - 1 - pz)
				else:
					dp[i][j] = max(dp[i - 1][j] + v * abs(p - i + 1) + vz * abs(pz - i), dp[i][j - 1] + v * abs(a - j - p) + vz * abs(pz - i))
			elif j == 0:
				dp[i][0] = dp[i - 1][0] + v * abs(p - i + 1)
			elif i == 0:
				dp[0][j] = dp[0][j - 1] + v * abs(a - j - p)
			else:
				dp[i][j] = max(dp[i - 1][j] + v * abs(p - i + 1), dp[i][j - 1] + v * abs(a - j - p))
	print(max([max(x) for x in dp]))
main()


(N,) = map(int, input().split())
As = list(enumerate(map(int, input().split())))
As.sort(key=lambda x: -x[1])
dp = [[0] * (N + 1) for _ in range(N + 1)]
for (i, (pos, val)) in enumerate(As):
	for j in range(i + 1):
		k = i - j
		dp[j + 1][k] = max(dp[j + 1][k], dp[j][k] + abs(j - pos) * val)
		dp[j][k + 1] = max(dp[j][k + 1], dp[j][k] + abs(N - 1 - k - pos) * val)
print(max((dp[i][N - i] for i in range(N + 1))))


from sys import setrecursionlimit, exit
setrecursionlimit(1000000000)

def main():
	n = int(input())
	a = [(int(v), i) for (i, v) in enumerate(input().split())]
	a.sort(reverse=True)
	dp = [[0] * (n + 1) for _ in range(n + 1)]
	dp[0][0] = a[0][0] * (n - 1 - a[0][1])
	dp[0][1] = a[0][0] * a[0][1]
	for (i, v) in enumerate(a[1:]):
		dp[i + 1][0] = dp[i][0] + v[0] * abs(n - 1 - (i + 1) - v[1])
		dp[i + 1][i + 2] = dp[i][i + 1] + v[0] * abs(v[1] - (i + 1))
		for j in range(1, i + 2):
			dp[i + 1][j] = max(dp[i][j] + v[0] * abs(n - 1 - (i + 1) + j - v[1]), dp[i][j - 1] + v[0] * abs(v[1] - (j - 1)))
	ans = -float('inf')
	for i in dp[n - 1]:
		ans = max(ans, i)
	print(ans)
main()


n = int(input())
a = list(map(int, input().split()))
for i in range(n):
	a[i] = (a[i], i)
b = sorted(a, reverse=True)
dp = [[-10 ** 14 for i in range(n + 1)] for j in range(n + 1)]
dp[0][0] = 0
for i in range(1, n + 1):
	see = b[i - 1][0]
	prv = b[i - 1][1]
	for j in range(i + 1):
		dp[j][i - j] = max(dp[j - 1][i - j] + abs(prv - (j - 1)) * see, dp[j][i - j - 1] + abs(n - i + j - prv) * see)
ans = 0
for i in range(n + 1):
	ans = max(ans, dp[i][n - i])
print(ans)


e = enumerate
(n, *a) = map(int, open(0).read().split())
d = [0]
for (j, (a, i)) in e(sorted(((a, i) for (i, a) in e(a)))[::-1]):
	d = [d[0] + a * (~i - j + n)] + [max(d[k + 1] + a * (n + k - i - j), d[k] + a * abs(i - k)) for k in range(j)] + [d[j] + a * abs(i - j)]
print(max(d))


N = int(input())
A = list(map(int, input().split()))
A = [[A[i], i] for i in range(N)]
A.sort(reverse=True)
dp = [[0] * (N + 1) for _ in range(N + 1)]
dp[0][0] = 0
for x in range(1, N + 1):
	dp[x][0] = dp[x - 1][0] + A[x - 1][0] * (A[x - 1][1] - x + 1)
for y in range(1, N + 1):
	dp[0][y] = dp[0][y - 1] + A[y - 1][0] * (N - y - A[y - 1][1])
for x in range(1, N):
	for y in range(1, N + 1 - x):
		A0 = A[x + y - 1][0]
		A1 = A[x + y - 1][1]
		dp[x][y] = max(dp[x - 1][y] + A0 * (A1 + 1 - x), dp[x][y - 1] + A0 * (N - y - A1))
c = 0
for i in range(N):
	if c < dp[i][N - i]:
		c = dp[i][N - i]
print(c)


def main():
	n = int(input())
	a = list(map(int, input().split()))
	a = sorted([(j, i) for (i, j) in enumerate(a)])[::-1]
	dp = [0]
	for i in range(n):
		dp2 = [0] * (i + 2)
		(A, p) = a[i]
		for j in range(i + 1):
			right = n - i - 1 + j
			dp2[j] = max(dp2[j], dp[j] + abs(p - right) * A)
			dp2[j + 1] = max(dp2[j + 1], dp[j] + abs(p - j) * A)
		dp = dp2
	print(max(dp2))
main()


n = int(input())
al = list(map(int, input().split()))
l = 0
r = n - 1
data = [[i, a] for (i, a) in enumerate(al)]
data.sort(key=lambda x: x[1], reverse=True)
dp = [[0] * (n + 1) for _ in range(n + 1)]
for i in range(n):
	for j in range(0, i + 1):
		(p, a) = data[i]
		dp[i + 1][j + 1] = max(dp[i + 1][j + 1], dp[i][j] + abs(n - 1 - j - p) * a)
		dp[i + 1][j] = max(dp[i + 1][j], dp[i][j] + abs(i - j - p) * a)
print(max(dp[-1]))


import numpy as np
n = int(input())
A = list(map(int, input().split()))
babies = sorted([(activity, index) for (index, activity) in enumerate(A, 1)], reverse=True)
DP = np.zeros(n + 1, dtype=np.int64)
position = np.arange(1, n + 1, dtype=np.int64)
for (filled, (activity, index)) in enumerate(babies, 1):
	to_left = DP[:filled] + abs((position[:filled] - index) * activity)
	to_right = DP[:filled] + abs((position[-filled:] - index) * activity)
	DP[1:filled + 1] = np.maximum(DP[1:filled + 1], to_left)
	DP[:filled] = np.maximum(DP[:filled], to_right)
print(DP.max())


N = int(input())
A = list(map(int, input().split()))
A = [[a, i] for (a, i) in zip(A, range(1, N + 1))]
A = sorted(A, reverse=True)
INF = float('inf')
dp = [[-INF] * (N + 1) for _ in range(N + 1)]
dp[0][0] = 0
ans = 0
for x_y in range(1, N + 1):
	a = A[x_y - 1]
	for x in range(x_y + 1):
		y = x_y - x
		if x == 0:
			dp[x][y] = dp[x][y - 1] + a[0] * (N - y + 1 - a[1])
		elif y == 0:
			dp[x][y] = dp[x - 1][y] + a[0] * (a[1] - x)
		else:
			dp[x][y] = max(dp[x - 1][y] + a[0] * (a[1] - x), dp[x][y - 1] + a[0] * (N - y + 1 - a[1]))
		ans = max(ans, dp[x][y])
print(ans)


N = int(input())
A = sorted([(int(a), i) for (i, a) in enumerate(input().split())], key=lambda x: x[0])[::-1]
X = [0] + [-1 << 100] * (N + 5)
for (k, (a, i)) in enumerate(A):
	X = [max(X[j] + abs(N - (k - j) - 1 - i) * a, X[j - 1] + abs(j - 1 - i) * a if j else 0) for j in range(N + 5)]
print(max(X))


e = enumerate
(n, a) = open(0)
n = int(n)
d = [0] + [1e+18] * n
for (j, (a, i)) in e(sorted(((-int(a), i) for (i, a) in e(a.split())))):
	d = [min(t + a * abs(~i - j + k + n), d[k - 1] + a * abs(~i + k)) for (k, t) in e(d)]
print(-min(d))


from heapq import heappush, heappop
from math import inf
import numpy as np
n = int(input())
A = []
[heappush(A, (-v, i)) for (i, v) in enumerate(map(int, input().split()), 1)]
dp = np.full((n + 1, n + 1), -inf, dtype=int)
dp[0][0] = 0
for i in range(n):
	(v, j) = heappop(A)
	v = -v
	dp[i + 1, 1:] = np.maximum(dp[i, :-1] + v * np.abs(j - np.arange(1, n + 1)), dp[i + 1, 1:])
	dp[i + 1, :-1] = np.maximum(dp[i, :-1] + v * np.abs(j - np.arange(n - i, n + n - i)), dp[i + 1, :-1])
print(dp[-1,].max())


N = int(input())
A = sorted(((int(x), i) for (i, x) in enumerate(input().split())))
t = [0] * N ** 2
for w in range(N):
	(a, j) = A[w]
	for l in range(N - w):
		r = l + w
		p = a * abs(j - l)
		t[l * N + r] = max(p + t[(l + 1) * N + r], a * abs(j - r) + t[l * N + r - 1]) if w else p
print(t[N - 1])


n = int(input())
a = map(int, input().split())
p = [(x, i) for (i, x) in enumerate(a)]
p.sort(reverse=True)
ans = 0
dp = [[0] * (n + 1) for i in range(n + 1)]
for i in range(n + 1):
	for j in range(n + 1):
		if i + j == n:
			ans = max(ans, dp[i][j])
			break
		(act, ind) = p[i + j]
		dp[i + 1][j] = max(dp[i + 1][j], dp[i][j] + act * abs(ind - i))
		dp[i][j + 1] = max(dp[i][j + 1], dp[i][j] + act * abs(n - 1 - j - ind))
print(ans)


n = int(input())
A_l = [(A, i) for (i, A) in enumerate(map(int, input().split()))]
A_l.sort(reverse=True)
dp = []
for _ in range(n + 1):
	dp.append([0] * (n + 1))
for j in range(1, n + 1):
	(a, i) = A_l[j - 1]
	dp[j][0] = dp[j - 1][0] + a * (n - (j - 1) - (i + 1))
for k in range(1, n + 1):
	(a, i) = A_l[k - 1]
	dp[0][k] = dp[0][k - 1] + a * (i + 1 - (1 + (k - 1)))
for j in range(1, n + 1):
	for k in range(1, n - j + 1):
		(a, i) = A_l[j + k - 1]
		j_plus = a * (n - (j - 1) - (i + 1))
		k_plus = a * (i + 1 - (1 + (k - 1)))
		dp[j][k] = max(dp[j - 1][k] + j_plus, dp[j][k - 1] + k_plus)
result = 0
for j in range(0, n + 1):
	k = n - j
	result = max(result, dp[j][k])
print(result)


N = int(input())
A = sorted([(int(x[1]), x[0]) for x in enumerate(input().split())], reverse=True)
V = [0]
for i in range(N - 1):
	(a, p) = A[i]
	s = i + 1
	V2 = [None] * (s + 1)
	for t in range(s + 1):
		v = 0
		if t > 0:
			v = V[t - 1] + a * abs(p - (t - 1))
		if t < s:
			v = max(V[t] + a * abs(p - (N - s + t)), v)
		V2[t] = v
	V = V2
(a, p) = A[-1]
for i in range(N):
	V[i] += a * abs(p - i)
print(max(V))


def main():
	n = int(input())
	arr = map(int, input().split())
	arr = tuple(sorted(enumerate(arr), key=lambda tpl: -tpl[1]))
	dp = [[0] * (n + 1) for _ in range(n + 1)]
	for (i, (p, a)) in enumerate(arr):
		for l in range(i + 1):
			dp[i + 1][l] = max(dp[i + 1][l], dp[i][l] + a * abs(p - (n - (i - l) - 1)))
			dp[i + 1][l + 1] = max(dp[i + 1][l + 1], dp[i][l] + a * abs(p - l))
	print(max(dp[n]))
main()


import sys
input = sys.stdin.readline
n = int(input())
a = list(map(int, input().split()))
ch = []
for i in range(n):
	ch.append((a[i], i))
ch.sort(reverse=True)
dp = [[0] * (n + 1) for _ in range(n + 1)]
dp[1][1] = ch[0][0] * ch[0][1]
dp[1][0] = ch[0][0] * (n - 1 - ch[0][1])
for i in range(2, n + 1):
	for j in range(i + 1):
		if j >= 1:
			dp[i][j] = max(dp[i][j], dp[i - 1][j - 1] + ch[i - 1][0] * abs(ch[i - 1][1] - j + 1))
		if i - 1 >= j:
			dp[i][j] = max(dp[i][j], dp[i - 1][j] + ch[i - 1][0] * abs(n - i + j - ch[i - 1][1]))
print(max(dp[n]))


import sys
input = sys.stdin.buffer.readline
N = int(input())
A = sorted(zip(map(int, input().split()), range(N)), reverse=True)
dp = [[0] * (N + 1) for _ in range(N + 1)]
ans = 0
for i in range(N):
	for j in range(i + 1):
		dp[i + 1][j] = max(dp[i + 1][j], dp[i][j] + A[i][0] * abs(A[i][1] - (N - 1 - (i - j))))
		dp[i + 1][j + 1] = max(dp[i + 1][j + 1], dp[i][j] + A[i][0] * abs(A[i][1] - j))
for i in range(N):
	ans = max(ans, dp[N][i])
print(ans)


import sys
input = sys.stdin.readline
N = int(input())
A = list(zip(list(map(int, input().split())), range(N)))
A.sort(reverse=True)
dp = [[0] * (N + 1) for _ in range(N + 1)]
for i in range(1, N + 1):
	(a, k) = A[i - 1]
	dp[i][0] = dp[i - 1][0] + a * abs(N - i - k)
	for j in range(1, i):
		dp[i][j] = max(dp[i - 1][j] + a * abs(N - (i - j) - k), dp[i - 1][j - 1] + a * abs(j - 1 - k))
	dp[i][i] = dp[i - 1][i - 1] + a * abs(i - 1 - k)
print(max(dp[N]))


n = int(input())
a = list(map(int, input().split()))
a2 = []
for i in range(n):
	a2.append((a[i], i))
a2.sort(reverse=True)
dp = [[-1] * n for _ in range(n)]
for i2 in range(n - 1, -1, -1):
	(a, i) = a2[i2]
	for s in range(i2 + 1):
		if i2 == n - 1:
			dp[i2][s] = a * abs(s - i)
		else:
			dp[i2][s] = max(dp[i2 + 1][s + 1] + a * abs(s - i), dp[i2 + 1][s] + a * abs(n - 1 - i2 + s - i))
print(dp[0][0])


import functools
import heapq
import itertools
import logging
import math
import random
import string
import sys
from argparse import ArgumentParser
from collections import defaultdict, deque
from copy import deepcopy
from typing import Dict, List, Optional, Set, Tuple

def solve(values: List[int], nb: int) -> int:
	s_values = sorted(((v, i) for (i, v) in enumerate(values)), reverse=True)
	dp = [[0 for _ in range(nb + 1)] for _ in range(nb + 1)]
	for (i, (v, p)) in enumerate(s_values, start=1):
		for x in range(i + 1):
			y = i - x
			if y == 0:
				dp[x][y] = dp[x - 1][0] + abs(p - x + 1) * v
				continue
			if x == 0:
				dp[x][y] = dp[x][y - 1] + abs(nb - y - p) * v
				continue
			dp[x][y] = max(dp[x - 1][y] + abs(p - x + 1) * v, dp[x][y - 1] + abs(nb - y - p) * v)
	return max((dp[i][nb - i] for i in range(nb + 1)))

def do_job():
	LOG.debug('Start working')
	N = int(input())
	values = list(map(int, input().split()))
	result = solve(values, N)
	print(result)

def print_output(testcase: int, result) -> None:
	if result is None:
		result = 'IMPOSSIBLE'
	print('Case #{}: {}'.format(testcase + 1, result))

def configure_log(log_file: Optional[str]=None) -> None:
	log_formatter = logging.Formatter('%(asctime)s - %(filename)s:%(lineno)d - %(levelname)s - %(message)s')
	if log_file:
		handler = logging.FileHandler(filename=log_file)
	else:
		handler = logging.StreamHandler(sys.stdout)
	handler.setFormatter(log_formatter)
	LOG.addHandler(handler)
LOG = None
if not LOG:
	LOG = logging.getLogger('template')
	configure_log()

def main(argv=None):
	if argv is None:
		argv = sys.argv[1:]
	parser = ArgumentParser()
	parser.add_argument('-v', '--verbose', dest='verbose', action='store_true', default=False, help='run as verbose mode')
	args = parser.parse_args(argv)
	if args.verbose:
		LOG.setLevel(logging.DEBUG)
	do_job()
	return 0

def __starting_point():
	import doctest
	doctest.testmod()
	return main()

class memoized:

	def __init__(self, func):
		self.func = func
		self.cache = {}

	def __call__(self, *args):
		try:
			return self.cache[args]
		except KeyError:
			value = self.func(*args)
			self.cache[args] = value
			return value
		except TypeError:
			return self.func(*args)

	def __repr__(self):
		return self.func.__doc__

	def __get__(self, obj, objtype):
		return functools.partial(self.__call__, obj)
__starting_point()


N = int(input())

def solve(a, i, prev):
	r = N - len(prev) - i + 1
	p = -i * a
	for (j, s) in enumerate(prev):
		yield max(p + abs(j - i) * a, s + abs(j + r) * a)
		p = s
	yield (s + abs(len(prev) - i) * a)
pd = [0]
A = list(map(int, input().split()))
for (a, i) in sorted(((a, i) for (i, a) in enumerate(A, 1)), reverse=True):
	pd = [*solve(a, i, pd)]
print(max(pd))


n = int(input())
a = list(map(int, input().split()))
p = sorted([(x, i) for (i, x) in enumerate(a)], reverse=True)
dp = [[0 for _ in range(n + 1)] for _ in range(n + 1)]
for s in range(1, n + 1):
	for x in range(s + 1):
		y = s - x
		if x > 0:
			dp[x][y] = max(dp[x][y], dp[x - 1][y] + abs(p[s - 1][1] - x + 1) * p[s - 1][0])
		if y > 0:
			dp[x][y] = max(dp[x][y], dp[x][y - 1] + abs(n - y - p[s - 1][1]) * p[s - 1][0])
print(max((dp[x][n - x] for x in range(n + 1))))


N = int(input())
_L = list(map(int, input().split()))
L = [(v, i) for (i, v) in enumerate(_L)]
L = sorted(L, key=lambda x: -x[0])
dp = [[0] * (N + 1) for _ in range(N + 1)]
ans = 0
for x in range(N):
	for y in range(N):
		cur = x + y
		if cur == N:
			ans = max(ans, dp[x][y])
			break
		(val, ind) = L[cur]
		dp[x + 1][y] = max(dp[x + 1][y], dp[x][y] + val * abs(ind - x))
		dp[x][y + 1] = max(dp[x][y + 1], dp[x][y] + val * abs(N - y - 1 - ind))
print(ans)


import sys
import socket
hostnames = ['N551J', 'F551C', 'X553M']
input_file = 'e1.in'
if socket.gethostname() in hostnames:
	sys.stdin = open(input_file)

def read_int_list():
	return list(map(int, input().split()))

def read_int():
	return int(input())

def read_str_list():
	return input().split()

def read_str():
	return input()
inf = 10 ** 9
inf = 10 ** 3

def solve():
	n = read_int()
	a = read_int_list()
	b = [(a[i], i) for i in range(n)]
	b.sort()
	best = [[-inf] * (n + 1) for i in range(n + 1)]
	best[0][0] = 0
	for s in range(1, n + 1):
		for x in range(s + 1):
			y = s - x
			(ai, i) = b[-s]
			if x > 0:
				c = best[x - 1][y] + (n - x) * ai - i * ai
				if c > best[x][y]:
					best[x][y] = c
			if y > 0:
				c = best[x][y - 1] - (y - 1) * ai + i * ai
				if c > best[x][y]:
					best[x][y] = c
	res = -inf
	for x in range(n):
		y = n - x
		if res < best[x][y]:
			res = best[x][y]
	return res

def main():
	res = solve()
	print(res)

def __starting_point():
	main()
__starting_point()


N = int(input())
A = map(int, input().split())
A = sorted(enumerate(A), key=lambda x: x[1], reverse=True)
dp = [[0] * (N + 1) for _ in range(N + 1)]
for (n, (from_i, a)) in enumerate(A):
	for j in range(n + 1):
		dp[n + 1][j + 1] = max(dp[n + 1][j + 1], dp[n][j] + a * (from_i - j))
		dp[n + 1][j] = max(dp[n + 1][j], dp[n][j] + a * (N - (n - j) - 1 - from_i))
print(max(dp[N]))


n = int(input())
buf = list(map(int, input().split()))
a = []
for i in range(n):
	a.append([buf[i], i])
a = sorted(a, reverse=True)
dp = [[0] * (n + 1) for i in range(n + 1)]
for i in range(n):
	for j in range(n - i):
		cur = i + j
		temp1 = dp[i][j] + a[cur][0] * abs(n - 1 - a[cur][1] - j)
		temp2 = dp[i][j] + a[cur][0] * abs(a[cur][1] - i)
		dp[i + 1][j] = max(dp[i + 1][j], temp2)
		dp[i][j + 1] = max(dp[i][j + 1], temp1)
print(max([max(i) for i in dp]))


N = int(input())
A = list(map(int, input().split()))
table = []
for (i, a) in enumerate(A):
	table.append([a, i])
table.sort()
DP = [[0 for i in range(N + 1)] for j in range(N + 1)]
for i in range(1, N + 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
		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 = map(int, input().split())
b = [(a, i) for (i, a) in enumerate(a)]
b.sort(reverse=True)
dp = [[0] * (n + 1) for i in range(n + 1)]
ans = 0
for l in range(n):
	for r in range(n):
		if l + r == n:
			ans = max(ans, dp[l][r])
			break
		(act, i) = b[l + r]
		dp[l + 1][r] = max(dp[l + 1][r], dp[l][r] + act * abs(i - l))
		dp[l][r + 1] = max(dp[l][r + 1], dp[l][r] + act * abs(n - i - 1 - r))
print(ans)


N = int(input())
D = []
for (i, a) in enumerate(map(int, input().split())):
	D.append((a << 11) + i)
D.sort(reverse=True)
dp = [0] * (N + 1)
for (i, d) in enumerate(D, start=1):
	(x, a) = (d % (1 << 11), d >> 11)
	for j in reversed(range(0, i + 1)):
		res1 = dp[j - 1] + a * (x - (j - 1)) * (j - 1 >= 0)
		res2 = dp[j] + a * (N - (i - j) - x) * (j <= i - 1)
		if res1 >= res2:
			dp[j] = res1
		else:
			dp[j] = res2
print(max(dp))


def slove(n, a_list):
	dp = [[0] * (n + 1) for i in range(n + 1)]
	for i in range(n):
		for j in range(n - i):
			dp[i + 1][j] = max(dp[i + 1][j], dp[i][j] + a_list[i + j][0] * abs(a_list[i + j][1] - i))
			dp[i][j + 1] = max(dp[i][j + 1], dp[i][j] + a_list[i + j][0] * abs(n - 1 - j - a_list[i + j][1]))
	return max([max(i) for i in dp])
n = int(input())
k = sorted([(int(x), i) for (i, x) in enumerate(input().split())], reverse=True)
print(slove(n, k))


N = int(input())
A = list(map(int, input().split()))
B = sorted([(a, i) for (i, a) in enumerate(A)])
dp = [[0] * (N + 1) for _ in range(N + 1)]
for i in range(N):
	(a, j) = B.pop()
	for k in range(i + 1):
		dp[k][i - k + 1] = max(dp[k][i - k + 1], dp[k][i - k] + a * abs(j - (N - 1 - (i - k))))
		dp[k + 1][i - k] = max(dp[k + 1][i - k], dp[k][i - k] + a * abs(j - k))
print(max((dp[i][N - i] for i in range(N + 1))))


N = int(input())
As = list(map(int, input().split()))
arridx = [(i, a) for (i, a) in enumerate(As)]
arridx.sort(key=lambda x: x[1])
dp = {}
dp[0, N - 1] = 0
for i in range(N - 1, -1, -1):
	ndp = {}
	for l in range(N - i + 1):
		r = l + i - 1
		(idx, a) = arridx[i]
		lsc = rsc = 0
		if l > 0:
			lsc = dp[l - 1, r] + a * abs(l - 1 - idx)
		if r < N - 1:
			rsc = dp[l, r + 1] + a * abs(r + 1 - idx)
		ndp[l, r] = max(lsc, rsc)
	dp = ndp
print(max(dp.values()))


n = int(input())
a = list(map(int, input().split()))
b = []
for i in range(n):
	b.append([a[i], i])
b.sort(reverse=True)
now = [0]
for i in range(n):
	(aj, j) = b[i]
	last = now
	now = []
	for l in range(i + 1):
		r = l + n - 1 - i
		now.append(last[l] + aj * abs(r - j))
	for l in range(i):
		now[l + 1] = max(now[l + 1], last[l] + aj * abs(l - j))
	now.append(last[-1] + aj * abs(i - j))
print(max(now))


import sys
import numpy as np

def f(n, a_):
	a = []
	for (i, ai) in enumerate(a_):
		a.append([1 + i, ai])
	a.sort(key=lambda x: x[1], reverse=True)
	dp = np.zeros((n + 1, n + 1), np.int64)
	dp[1, 1] = a[0][1] * (n - a[0][0])
	dp[1, 0] = a[0][1] * (a[0][0] - 1)
	for i in range(2, n + 1):
		dp[i, 0] = dp[i - 1, 0] + a[i - 1][1] * abs(a[i - 1][0] - 1 - (i - 1))
		dp[i, i] = dp[i - 1, i - 1] + a[i - 1][1] * abs(n - a[i - 1][0] - (i - 1))
		l = dp[i - 1, 1:i] + [a[i - 1][1] * abs(a[i - 1][0] - 1 - j) for j in range(i - 2, -1, -1)]
		r = dp[i - 1, :i - 1] + [a[i - 1][1] * abs(n - a[i - 1][0] - j) for j in range(i - 1)]
		dp[i, 1:i] = np.maximum(l, r)
	return max(dp[n])

def __starting_point():
	input = sys.stdin.readline
	n = int(input())
	a_ = list(map(int, input().split()))
	print(f(n, a_))
__starting_point()


n = int(input())
a = list(map(int, input().split()))
b = []
for i in range(n):
	b.append((a[i], i))
b.sort(reverse=True)
dp = [[0 for _ in range(n + 1)] for _ in range(n + 1)]
for i in range(n):
	x = b[i][0]
	y = b[i][1]
	for l in range(i + 1):
		r = i - l
		dp[i + 1][l + 1] = max(dp[i + 1][l + 1], dp[i][l] + x * (y - l))
		dp[i + 1][l] = max(dp[i + 1][l], dp[i][l] + x * (n - 1 - r - y))
ans = 0
for i in range(n + 1):
	ans = max(dp[n][i], ans)
print(ans)


n = int(input())
xs = list(enumerate(map(int, input().split())))
xs.sort(key=lambda x: x[1])
xs.reverse()
dp = [[0 for _ in range(n + 1)] for _ in range(n + 1)]
for i in range(1, n + 1):
	(j, a) = xs[i - 1]
	for x in range(0, i + 1):
		y = i - x
		if x == 0:
			dp[x][y] = dp[x][y - 1] + a * (n - y - j)
		elif y == 0:
			dp[x][y] = dp[x - 1][y] + a * (j - x + 1)
		else:
			dp[x][y] = max(dp[x][y - 1] + a * (n - y - j), dp[x - 1][y] + a * (j - x + 1))
print(max([dp[i][n - i] for i in range(n + 1)]))


N = int(input())
A = list(map(int, input().split()))
A = [(A[i], i) for i in range(0, N)]
A.sort(reverse=True)
dp = [[0 for i in range(0, N)] for j in range(0, N)]
for i in range(0, N - 1):
	(a, index) = A[-1]
	dp[N - 1][i] = a * abs(index - i)
for _ in range(1, N):
	i = N - 1 - _
	for j in range(0, i + 1):
		left = j
		right = i - j
		(a, index) = A[i]
		ans = 0
		if index > left - 1:
			ans = max(ans, dp[i + 1][j + 1] + a * (index - left))
		if N - right > index:
			ans = max(ans, dp[i + 1][j] + a * (N - right - 1 - index))
		dp[i][j] = ans
print(dp[0][0])


n = int(input())
a = list(map(int, input().split()))
a = [[e, i] for (i, e) in enumerate(a, 1)]
a.sort(reverse=True)
dp = [[0] * (n + 1) for _ in range(n + 1)]
for (t, (e, idx)) in enumerate(a, 1):
	for i in range(1, t + 1):
		j = t - i
		dp[i][j] = max(dp[i][j], dp[i - 1][j] + e * abs(idx - i))
	for j in range(1, t + 1):
		i = t - j
		dp[i][j] = max(dp[i][j], dp[i][j - 1] + e * abs(idx - (n - j + 1)))
ans = 0
for i in range(1, n + 1):
	j = n - i
	ans = max(ans, dp[i][j])
print(ans)


n = int(input())
a = list(map(int, input().split()))
b = []
for (i, v) in enumerate(a):
	b.append([v, i])
b.sort(reverse=True)
dp = [[0] * (n + 1) for _ in range(n + 1)]
ans = 0
for i in range(n + 1):
	for j in range(n + 1 - i):
		if i > 0:
			dp[i][j] = max(dp[i][j], dp[i - 1][j] + b[i + j - 1][0] * (b[i + j - 1][1] - (i - 1)))
		if j > 0:
			dp[i][j] = max(dp[i][j], dp[i][j - 1] + b[i + j - 1][0] * (n - j - b[i + j - 1][1]))
for i in range(n + 1):
	ans = max(ans, dp[i][n - i])
print(ans)


from collections import defaultdict
n = int(input())
a_ = list(map(int, input().split()))
import numpy as np
a = []
for (i, ai) in enumerate(a_):
	a.append([1 + i, ai])
a.sort(key=lambda x: x[1], reverse=True)
dp = np.zeros((n + 1, n + 1), np.int64)
dp[1, 1] = a[0][1] * (n - a[0][0])
dp[1, 0] = a[0][1] * (a[0][0] - 1)
for i in range(2, n + 1):
	dp[i, 0] = dp[i - 1, 0] + a[i - 1][1] * abs(a[i - 1][0] - 1 - (i - 1))
	dp[i, i] = dp[i - 1, i - 1] + a[i - 1][1] * abs(n - a[i - 1][0] - (i - 1))
	l = dp[i - 1, 1:i] + [a[i - 1][1] * abs(a[i - 1][0] - 1 - j) for j in range(i - 2, -1, -1)]
	r = dp[i - 1, :i - 1] + [a[i - 1][1] * abs(n - a[i - 1][0] - j) for j in range(i - 1)]
	dp[i, 1:i] = np.maximum(l, r)
print(max(dp[n]))


iim = lambda : list(map(int, input().rstrip().split()))

def resolve():
	N = int(input())
	A = list(enumerate(iim()))
	A.sort(key=lambda x: (x[1], (2 * x[0] - N) ** 2), reverse=True)
	dp = [0] * (N + 1)
	dp0 = dp[:]
	for (i, (j, ai)) in enumerate(A):
		for k in range(i + 1):
			dp[k] = max(dp[k], dp0[k] + abs(N - i + k - 1 - j) * ai)
			dp[k + 1] = max(dp[k + 1], dp0[k] + abs(k - j) * ai)
		(dp, dp0) = (dp0, dp)
	print(max(dp0))

def __starting_point():
	resolve()
__starting_point()


n = int(input())
a = list(enumerate(map(int, input().split())))
a.sort(key=lambda x: -x[1])
dp = [[0] * (n + 1) for i in range(n + 1)]
for (idx, ix) in enumerate(a):
	(i, x) = ix
	for j in range(idx + 1):
		dp[j + 1][idx - j] = max(dp[j + 1][idx - j], dp[j][idx - j] + (i - j) * x)
		dp[j][idx - j + 1] = max(dp[j][idx - j + 1], dp[j][idx - j] + (n - 1 - (idx - j) - i) * x)
print(max([dp[i][n - i] for i in range(n + 1)]))


n = int(input())
a = list(map(int, input().split()))
b = []
for i in range(n):
	b.append([a[i], i])
b.sort()
b = b[::-1]
dp = [[0] * (n + 1) for i in range(n + 1)]
cnt = 0
for (i, j) in b:
	for k in range(cnt + 1):
		dp[cnt - k][k + 1] = max(dp[cnt - k][k] + abs(n - 1 - j - k) * i, dp[cnt - k][k + 1])
		dp[cnt - k + 1][k] = max(dp[cnt - k][k] + abs(j - (cnt - k)) * i, dp[cnt - k + 1][k])
	cnt += 1
ans = 0
for k in range(n):
	ans = max(ans, dp[n + 1 - k - 1][k])
print(ans)


from operator import itemgetter as get
N = int(input())
A = list(map(int, input().split()))
SB = 0
ABI = sorted((((b := (max(N - i, i - 1) * a)), (SB := (SB + b))) and (a, b, i) for (i, a) in enumerate(A, 1)), reverse=True)

def solve(a, i, prev, th):
	(pl, pr, ps) = (0, -1, 0)
	for (l, r, s) in prev:
		if s < th:
			continue
		if pr == r - 1:
			yield (l, r - 1, max(s + abs(r - i) * a, ps + abs(i - pl) * a))
		else:
			if pl:
				yield (pl + 1, pr, ps + abs(i - pl) * a)
			yield (l, r - 1, s + abs(r - i) * a)
		(pl, pr, ps) = (l, r, s)
	yield (pl + 1, pr, ps + abs(i - pl) * a)
prev = [(1, N, 0)]
pm = 0
for (j, (a, b, i)) in enumerate(ABI):
	M = 0
	prev = [((M := max(M, s)),) and (l, r, s) for (l, r, s) in solve(a, i, prev, pm - SB)]
	pm = M
	SB -= b
print(pm)


def main():
	(n, *a) = list(map(int, open(0).read().split()))
	dp = [[0] * (n + 1) for _ in range(n + 1)]
	for (t, (c, k)) in enumerate(sorted(((c, k) for (k, c) in enumerate(a)))[::-1]):
		for i in range(t + 1):
			j = t - i
			if dp[i + 1][j] <= (s := (dp[i][j] + c * abs(i - k))):
				dp[i + 1][j] = s
			if dp[i][j + 1] <= (s := (dp[i][j] + c * abs(n - 1 - k - j))):
				dp[i][j + 1] = s
	print(max((dp[i][~i] for i in range(n + 1))))

def __starting_point():
	main()
__starting_point()


from collections import defaultdict
n = int(input())
a = list(map(int, input().split()))
babies = [(q, i) for (i, q) in enumerate(a)]
babies.sort(reverse=True)
dp = defaultdict(int)
ans = 0
for l in range(n):
	for r in range(n):
		if l + r == n:
			ans = max(ans, dp[l, r])
			break
		(act, ind) = babies[l + r]
		dp[l + 1, r] = max(dp[l + 1, r], dp[l, r] + act * abs(ind - l))
		dp[l, r + 1] = max(dp[l, r + 1], dp[l, r] + act * abs(ind - (n - 1 - r)))
print(ans)


def max2(x, y):
	return x if x > y else y
N = int(input())
D = []
for (i, a) in enumerate(map(int, input().split())):
	D.append((a << 11) + i)
D.sort(reverse=True)
dp = [0] * (N + 1)
for (i, d) in enumerate(D, start=1):
	(x, a) = (d % (1 << 11), d >> 11)
	for j in reversed(range(0, i + 1)):
		dp[j] = max2((dp[j - 1] + a * (x - (j - 1))) * (j - 1 >= 0), (dp[j] + a * (N - (i - j) - x)) * (j <= i - 1))
print(max(dp))


N = int(input())
A = list(map(int, input().split()))
D = [[0] * 2 for _ in range(N)]
for i in range(N):
	D[i] = [i, A[i]]
D.sort(key=lambda x: x[1], reverse=True)
DP = [[0] * (N + 1) for _ in range(N + 1)]
for i in range(N):
	(n, x) = D[i]
	t = i + 1
	for j in range(t + 1):
		l = j
		r = t - j
		rr = N - 1 - r
		if l != 0:
			DP[l][r] = DP[l - 1][r] + abs(n - l + 1) * x
		if r != 0:
			DP[l][r] = max(DP[l][r], DP[l][r - 1] + abs(rr + 1 - n) * x)
ans = 0
for i in range(N + 1):
	ans = max(ans, DP[i][N - i])
print(ans)


(N,) = map(int, input().split())
X = [(i + 1, x) for (i, x) in enumerate(map(int, input().split()))]
dp = [[0] * (N + 1) for _ in range(N + 1)]
X = sorted(X, key=lambda x: x[1])[::-1]
for i in range(1, N + 1):
	(ai, a) = X[i - 1]
	dp[i][0] = dp[i - 1][0] + a * abs(ai - (N - i + 1))
	for j in range(1, i):
		dp[i][j] = max(dp[i - 1][j - 1] + a * abs(ai - j), dp[i - 1][j] + a * abs(ai - (N - (i - j) + 1)))
	dp[i][i] = dp[i - 1][i - 1] + a * abs(ai - i)
print(max(dp[-1]))


N = int(input())
A = sorted([(int(a), i) for (i, a) in enumerate(input().split())], key=lambda x: x[0])[::-1]
X = [0] + [-1 << 100] * (N + 1)
for (k, (a, i)) in enumerate(A):
	nX = [0] * (N + 2)
	for j in range(k + 1):
		nX[j] = max(nX[j], X[j] + abs(N - (k - j) - 1 - i) * a)
		nX[j + 1] = max(nX[j + 1], X[j] + abs(j - i) * a)
	X = nX
print(max(X))


def f(n, a_list):
	a_list = list(zip(list(range(n)), a_list))
	a_list.sort(key=lambda x: x[1])
	dp = [0]
	for k in range(1, n + 1):
		dp_ = []
		(i, a) = a_list.pop()
		dp_.append(dp[0] + a * (n - k - i))
		for x in range(1, k):
			dp_.append(max(dp[x - 1] + a * (i - x + 1), dp[x] + a * (n - k + x - i)))
		dp_.append(dp[k - 1] + a * (i - k + 1))
		dp = dp_
	return max(dp)

def __starting_point():
	n = int(input())
	a_list = list(map(int, input().split()))
	print(f(n, a_list))
__starting_point()


n = int(input())
a = list(map(int, input().split()))
a = list(enumerate(a))
a.sort(key=lambda x: x[1])
DP = [[0 for i in range(n + 1)] for j in range(n + 1)]
DP[0][0] = 0
for i in range(1, n + 1):
	(pos, val) = a.pop()
	pos = pos + 1
	DP[0][i] = DP[0][i - 1] + abs(val * (n - i + 1 - pos))
	DP[i][0] = DP[i - 1][0] + abs(val * (pos - i))
	for x in range(1, i):
		y = i - x
		DP[x][y] = max(DP[x - 1][y] + abs(val * (pos - x)), DP[x][y - 1] + abs(val * (n - y + 1 - pos)))
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()))
for i in range(n):
	a[i] = [a[i], i]
l1 = 0
r1 = n - 1
l2 = l1
r2 = r1
a.sort(reverse=1)
dp = [[0] * (n + 1) for i in range(n + 1)]
ans = 0
for i in range(1, n + 1):
	dp[i][0] = dp[i - 1][0] + a[i - 1][0] * abs(a[i - 1][1] - i + 1)
	dp[0][i] = dp[0][i - 1] + a[i - 1][0] * abs(n - a[i - 1][1] - i)
	for j in range(1, i):
		dp[j][i - j] = max(dp[j - 1][i - j] + a[i - 1][0] * abs(a[i - 1][1] - j + 1), dp[j][i - j - 1] + a[i - 1][0] * abs(n - a[i - 1][1] - (i - j)))
for i in dp:
	ans = max(ans, max(i))
print(ans)


import itertools
n = int(input())
l = list(map(int, input().split()))
li = [i for i in range(n)]
a = []
for i in range(n):
	a.append([l[i], i])
a.sort(reverse=True)
dp = [[-1] * (n + 1) for i in range(n + 1)]
dp[0][0] = 0
for i in range(n):
	for j in range(i + 1):
		le = j
		ri = i - le
		dp[le + 1][ri] = max(dp[le + 1][ri], dp[le][ri] + a[i][0] * abs(a[i][1] - le))
		dp[le][ri + 1] = max(dp[le][ri + 1], dp[le][ri] + a[i][0] * abs(n - 1 - a[i][1] - ri))
ans = 0
for i in range(n + 1):
	j = n - i
	ans = max(ans, dp[i][j])
print(ans)


n = int(input())
a = sorted(((int(x), k) for (k, x) in enumerate(input().split())))
opt = [[0 for j in range(n + 1)] for i in range(n + 1)]
for i in reversed(list(range(n))):
	for j in range(i, n):
		(val, k) = a[j - i]
		opt[i][j] = max(val * abs(k - i) + opt[i + 1][j], val * abs(k - j) + opt[i][j - 1])
print(opt[0][n - 1])


n = int(input())
a = list(map(int, input().split()))
for i in range(n):
	a[i] = [a[i], i]
a.sort(reverse=True)
dp = [[0] * (n + 1) for i in range(n + 1)]
for i in range(n):
	for j in range(i + 1):
		dp[i + 1][j + 1] = dp[i][j] + abs(a[i][1] - j) * a[i][0]
	for j in range(i + 1):
		dp[i + 1][j] = max(dp[i + 1][j], dp[i][j] + abs(a[i][1] - (n - 1 - (i - j))) * a[i][0])
print(max(dp[-1]))


import sys
sys.setrecursionlimit(2 * 10 ** 5)
N = int(input())
A = [int(x) for x in input().split()]
A = sorted(((a, i) for (i, a) in enumerate(A)))
cache = [[None for i in range(N + 1)] for j in range(N + 1)]

def dp(l, r):
	if l > r:
		return 0
	if cache[l][r] is None:
		(a, x) = A[r - l]
		cache[l][r] = max(a * abs(x - l) + dp(l + 1, r), a * abs(x - r) + dp(l, r - 1))
	return cache[l][r]
print(dp(0, N - 1))


(n, *a) = map(int, open(0).read().split())
dp = [[0] * (n + 1) for _ in range(n + 1)]
for (t, (c, k)) in enumerate(sorted(((c, k) for (k, c) in enumerate(a)))[::-1]):
	for i in range(t + 1):
		j = t - i
		if dp[i + 1][j] < dp[i][j] + c * abs(i - k):
			dp[i + 1][j] = dp[i][j] + c * abs(i - k)
		if dp[i][j + 1] < dp[i][j] + c * abs(n - 1 - k - j):
			dp[i][j + 1] = dp[i][j] + c * abs(n - 1 - k - j)
print(max((dp[i][~i] for i in range(n + 1))))


n = int(input())
a = [(int(x), i) for (i, x) in enumerate(input().split())]
a.sort(reverse=True)
(aval, ai) = zip(*a)
dp = [[0] * (n + 1) for _ in range(n + 1)]
for l in range(n):
	for r in range(n):
		if l + r >= n:
			break
		dp[l + 1][r] = max(dp[l + 1][r], dp[l][r] + aval[l + r] * abs(ai[l + r] - l))
		dp[l][r + 1] = max(dp[l][r + 1], dp[l][r] + aval[l + r] * abs(ai[l + r] - (n - r - 1)))
print(max([dp[i][n - i] for i in range(n + 1)]))


import sys
sys.setrecursionlimit(10 ** 6)
N = int(input())
A = list(map(int, input().split()))
acts = [(x, i) for (i, x) in enumerate(A)]
acts.sort()
dp = [[-1] * N for _ in range(N)]

def solve(l, r):
	if r < l:
		return 0
	if dp[l][r] >= 0:
		return dp[l][r]
	k = r - l
	(act, init_pos) = acts[k]
	gain_l = act * abs(l - init_pos)
	gain_r = act * abs(r - init_pos)
	res = dp[l][r] = max(gain_l + solve(l + 1, r), gain_r + solve(l, r - 1))
	return res
print(solve(0, N - 1))


n = int(input())
A = [(a, i) for (i, a) in enumerate(map(int, input().split()))]
A.sort(reverse=True)
dp = [[0] * (n + 1) for _ in range(n + 1)]
ans = 0
for L in range(n + 1):
	for R in range(n + 1):
		if n <= L + R - 1:
			continue
		(a, i) = A[L + R - 1]
		if 0 <= R - 1:
			dp[L][R] = max(dp[L][R], dp[L][R - 1] + abs(n - R - i) * a)
		if 0 <= L - 1:
			dp[L][R] = max(dp[L][R], dp[L - 1][R] + abs(i - (L - 1)) * a)
		ans = max(ans, dp[L][R])
print(ans)


N = int(input())
A = enumerate(map(int, input().split()))
A = sorted(A, reverse=True, key=lambda x: x[1])
DP = [[0] * (N + 1) for x in range(N + 1)]
for i in range(1, N + 1):
	(b, a) = A[i - 1]
	b += 1
	for j in range(i + 1):
		c = 0
		if i >= 1 and j >= 1:
			dst = j
			c = max(c, DP[i - 1][j - 1] + a * abs(b - dst))
		if i != j:
			dst = N - (i - j) + 1
			c = max(c, DP[i - 1][j] + a * abs(b - dst))
		DP[i][j] = c
print(max(DP[N]))


n = int(input())
a = [(int(j), i) for (i, j) in enumerate(input().split())]
a.sort(reverse=1)
dp = [[0] * (n + 1) for _ in range(n + 1)]
for i in range(1, n + 1):
	for j in range(i + 1):
		k = i - j
		if j != 0:
			dp[i][j] = dp[i - 1][j - 1] + a[i - 1][0] * abs(j - 1 - a[i - 1][1])
		if k != 0:
			dp[i][j] = max(dp[i][j], dp[i - 1][j] + a[i - 1][0] * abs(n - k - a[i - 1][1]))
print(max(dp[-1]))


n = int(input())
l = list(map(int, input().split()))
dp = [[0 for j in range(n + 1)] for i in range(n + 1)]
e = []
for i in range(n):
	e.append([l[i], i])
e.sort(reverse=True)
for i in range(1, n + 1):
	p = e[i - 1][0]
	q = e[i - 1][1]
	for j in range(i + 1):
		if j < i:
			a1 = dp[i - 1][j] + p * abs(n - (i - j) - q)
		else:
			a1 = 0
		if j > 0:
			a2 = dp[i - 1][j - 1] + p * abs(q - j + 1)
		else:
			a2 = 0
		dp[i][j] = max(a1, a2)
print(max(dp[-1]))


from operator import itemgetter

def main():
	n = int(input())
	a = sorted(enumerate(map(int, input().split())), key=itemgetter(1), reverse=True)
	dp = [[0] * (n + 1) for _ in range(n + 1)]
	for x in range(n):
		for y in range(n - x):
			(i, ai) = a[x + y]
			dp[x + 1][y] = max(ai * (i - x) + dp[x][y], dp[x + 1][y])
			dp[x][y + 1] = max(ai * (n - 1 - y - i) + dp[x][y], dp[x][y + 1])
	print(max((dp[i][n - i] for i in range(n))))
main()


def main():
	from random import sample
	from operator import itemgetter
	e = enumerate
	(n, a) = open(0)
	n = int(n)
	d = [0] + [-2 ** 64] * n
	for (j, (a, i)) in e(sorted(sample([(a, i) for (i, a) in e(map(int, a.split()))], n), key=itemgetter(0), reverse=1)):
		d = [max(t + a * (~i - j + k + n), d[k - 1] + a * abs(~i + k)) for (k, t) in e(d)]
	print(max(d))
main()


(n, *a) = map(int, open(0).read().split())
MX = 2005
dp = [0] * (MX * 10000 + MX + 100)
p = sorted([a[i] * MX + i for i in range(n)])[::-1]
for i in range(n):
	(ai, pi) = divmod(p[i], MX)
	for l in range(n):
		r = i - l
		if not 0 <= r <= n:
			break
		dp[(i + 1) * 10000 + (l + 1)] = max(dp[(i + 1) * 10000 + (l + 1)], dp[i * 10000 + l] + ai * (pi - l))
		dp[(i + 1) * 10000 + (l + 0)] = max(dp[(i + 1) * 10000 + (l + 0)], dp[i * 10000 + l] + ai * (n - r - 1 - pi))
ans = 0
for i in range(n + 1):
	ans = max(ans, dp[n * 10000 + i])
print(ans)


N = int(input())
As = list(map(int, input().split()))
dp = [[0] * (N - k + 1) for k in range(N + 1)]
s = 0
for (i, A) in sorted(enumerate(As), key=lambda t: t[1], reverse=True):
	s += 1
	for x in range(s + 1):
		y = s - x
		if x > 0 and y > 0:
			dp[x][y] = max(dp[x - 1][y] + A * (i - x + 1), dp[x][y - 1] + A * (N - y - i))
		elif x == 0:
			dp[x][y] = dp[x][y - 1] + A * (N - y - i)
		else:
			dp[x][y] = dp[x - 1][y] + A * (i - x + 1)
ans = max((dp[x][N - x] for x in range(N + 1)))
print(ans)


n = int(input())
arr = list(map(int, input().split()))
arr = [[arr[i], i] for i in range(n)]
arr = sorted(arr, reverse=True, key=lambda x: x[0])
dp = [[-1] * (n + 1) for _ in range(n + 1)]
dp[0][0] = 0
for i in range(n):
	for j in range(n):
		if dp[i][j] == -1:
			continue
		dp[i + 1][j + 1] = max(dp[i + 1][j + 1], dp[i][j] + arr[i][0] * abs(arr[i][1] - j))
		dp[i + 1][j] = max(dp[i + 1][j], dp[i][j] + arr[i][0] * abs(n - 1 - (i - j) - arr[i][1]))
print(max(dp[-1]))


def main():
	N = int(input())
	A = list(map(int, input().split()))
	AA = [(v, i) for (i, v) in enumerate(A)]
	AA.sort(reverse=True)
	dp = [[0] * (N + 1) for _ in range(N + 1)]
	for (i, (a, b)) in enumerate(AA, start=1):
		for x in range(i + 1):
			y = i - x
			if y == 0:
				dp[x][y] = dp[x - 1][y] + abs(b - x + 1) * a
			elif x == 0:
				dp[x][y] = dp[x][y - 1] + abs(N - y - b) * a
			else:
				dp[x][y] = max(dp[x - 1][y] + abs(b - x + 1) * a, dp[x][y - 1] + abs(N - y - b) * a)
	r = 0
	for i in range(N + 1):
		r = max(r, dp[i][N - i])
	return r
print(main())


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

def main():
	P = [(A[i], i + 1) for i in range(N)]
	P.sort(reverse=True)
	dp = [[0] * (N + 1) for _ in range(N + 1)]
	for (n, p) in zip(list(range(1, N + 1)), P):
		(x, y) = p
		for i in range(n):
			j = n - 1 - i
			left = dp[i][j] + x * abs(y - (i + 1))
			dp[i + 1][j] = max(dp[i + 1][j], left)
			right = dp[i][j] + x * abs(y - (N - j))
			dp[i][j + 1] = max(dp[i][j + 1], right)
	ans = max([dp[i][N - i] for i in range(N)])
	print(ans)

def __starting_point():
	main()
__starting_point()


N = int(input())
A = sorted([(i, int(a)) for (i, a) in enumerate(input().split())], key=lambda x: x[1])[::-1]
dp = [[0] * (N + 1) for _ in range(N + 1)]
cnt = 0
for (k, a) in A:
	for i in range(N):
		j = cnt - i
		if j < 0:
			continue
		dp[i + 1][j] = max(dp[i + 1][j], dp[i][j] + a * max(0, k - i))
		dp[i][j + 1] = max(dp[i][j + 1], dp[i][j] + a * max(0, N - 1 - j - k))
	cnt += 1
ans = 0
for i in range(N + 1):
	ans = max(ans, dp[N - i][i])
print(ans)


n = int(input())
a = list(map(int, input().split()))
a2 = [(a[i], i) for i in range(n)]
a2.sort(reverse=True)
dp = [[0] * (n + 1) for _ in range(n + 1)]
for (i, (aj, j)) in enumerate(a2):
	for k in range(i + 1):
		dp[i + 1][k + 1] = max(dp[i][k] + aj * abs(k - j), dp[i + 1][k + 1])
		dp[i + 1][k] = max(dp[i][k] + aj * abs(n - i + k - 1 - j), dp[i + 1][k])
ans = max(dp[n])
print(ans)


import sys
input = sys.stdin.buffer.readline
n = int(input())
a = list(map(int, input().split()))
dp = [[0] * (n + 1) for _ in range(n + 1)]
b = []
for i in range(n):
	b.append(a[i] * n + i)
b.sort(reverse=True)
for i in range(n + 1):
	for j in range(n + 1 - i):
		(val, idx) = divmod(b[i + j - 1], n)
		if i > 0:
			dp[i][j] = max(dp[i][j], dp[i - 1][j] + val * abs(idx - i + 1))
		if j > 0:
			dp[i][j] = max(dp[i][j], dp[i][j - 1] + val * abs(idx - n + j))
ans = 0
for i in range(n + 1):
	if ans < dp[i][n - i]:
		ans = dp[i][n - i]
print(ans)


import sys
sys.setrecursionlimit(5000)
N = int(input())
A = [(int(a), i) for (i, a) in enumerate(input().split())]
A = sorted(A, reverse=True)
memo = {}

def solve(i, l, r):
	if i == N:
		return 0
	key = l * N + r
	if key in memo:
		return memo[key]
	(a, loc) = A[i]
	score_l = a * abs(l - loc) + solve(i + 1, l + 1, r)
	score_r = a * abs(r - loc) + solve(i + 1, l, r - 1)
	retval = max(score_l, score_r)
	memo[key] = retval
	return retval
print(solve(0, 0, N - 1))


(n, *a) = map(int, open(0).read().split())

def dp_table(MXi, MXj):
	return [[0] * MXi for _ in range(MXj)]
MX = 2005
dp = dp_table(MX, MX)
p = sorted([a[i] * MX + i for i in range(n)])[::-1]
for i in range(n):
	(ai, pi) = divmod(p[i], MX)
	for l in range(n):
		r = i - l
		if not 0 <= r <= n:
			break
		dp[i + 1][l + 1] = max(dp[i + 1][l + 1], dp[i][l] + ai * (pi - l))
		dp[i + 1][l + 0] = max(dp[i + 1][l + 0], dp[i][l] + ai * (n - r - 1 - pi))
ans = 0
for i in range(n + 1):
	ans = max(ans, dp[n][i])
print(ans)


n = int(input())
a = list(map(int, input().split()))
b = [(a[i], i + 1) for i in range(n)]
b.sort(reverse=True)
cntf = 0
cntb = n - 1
dp = [[0 for i in range(n + 1)] for j in range(n + 1)]
dp[0][1] = -10 ** 18
for i in range(1, n + 1):
	(x, ind) = b[i - 1]
	dp[i][0] = dp[i - 1][0] + abs(n - i - ind + 1) * x
	for j in range(1, i + 1):
		dp[i][j] = max(dp[i - 1][j - 1] + abs(j - ind) * x, dp[i - 1][j] + abs(n - (i - j) + 1 - ind) * x)
print(max(dp[n]))


def main():
	N = int(input())
	A = list(map(int, input().split()))
	Awithidx = [[a, i] for (i, a) in enumerate(A)]
	sortedA = sorted(Awithidx, reverse=True)
	dp = [[0] * (N + 1) for _ in range(N + 1)]
	for i in range(N):
		a = sortedA[i][0]
		idx = sortedA[i][1]
		for l in range(i + 1):
			r = i - l
			dp[i + 1][l] = max(dp[i + 1][l], dp[i][l] + a * abs(N - r - 1 - idx))
			dp[i + 1][l + 1] = max(dp[i + 1][l + 1], dp[i][l] + a * abs(l - idx))
	print(max(dp[N]))
main()


import sys
sys.setrecursionlimit(10000)
n = int(input())
(*A,) = map(int, input().split())
A = [(a, i) for (i, a) in enumerate(A)]
A.sort(reverse=True)
DP = [[None for r in range(n + 1)] for l in range(n + 1)]

def dp(l, r):
	if l == r:
		DP[l][r] = 0
	if DP[l][r] != None:
		return DP[l][r]
	(a, i) = A[n - (r - l)]
	x = dp(l + 1, r) + a * abs(i - l)
	y = dp(l, r - 1) + a * abs(i - (r - 1))
	DP[l][r] = max(x, y)
	return DP[l][r]
print(dp(0, n))


import sys
import numpy as np
sys.setrecursionlimit(10 ** 9)
N = int(input())
A = list(map(int, input().split()))
B = []
for i in range(N):
	B.append([A[i], i])
B.sort(reverse=True)
dp = np.zeros(1, dtype=int)
zero = np.zeros(1, dtype=int)
for (act, i) in B:
	prev = dp.copy()
	dp = np.append(dp, [0])
	l = len(prev)
	right = np.arange((N - 1 - i) * act, (N - 1 - i) * act - act * l, -act)
	left = np.arange(i * act - act * (l - 1), (i + 1) * act, act)
	np.maximum(np.concatenate([prev + left, zero]), np.concatenate([zero, prev + right]), out=dp)
print(np.max(dp))


import sys
sys.setrecursionlimit(10 ** 9)
input()
arr = list(sorted([(v, idx) for (idx, v) in enumerate(map(int, input().split()))], reverse=True))
d = [[-1 for _ in range(len(arr))] for _ in range(len(arr))]

def dp(l, r, idx):
	if idx == len(arr):
		return 0
	if d[l][r] != -1:
		return d[l][r]
	left = abs(arr[idx][1] - l) * arr[idx][0] + dp(l + 1, r, idx + 1)
	right = abs(arr[idx][1] - r) * arr[idx][0] + dp(l, r - 1, idx + 1)
	d[l][r] = max(left, right)
	return max(left, right)
print(dp(0, len(arr) - 1, 0))


from bisect import bisect_left
N = int(input())
As = [(x, i) for (i, x) in enumerate(map(int, input().split()))]
up = sorted(As)
dp = [[0] * (N + 1) for _ in range(N + 1)]
for (i, (u, idx)) in enumerate(up[::-1], 1):
	for j in range(i):
		left = j
		right = N - (i - j)
		dp[i][j] = max(dp[i][j], dp[i - 1][j] + u * abs(right - idx))
		dp[i][j + 1] = max(dp[i][j + 1], dp[i - 1][j] + u * abs(left - idx))
print(max(dp[-1]))


n = int(input())
l = list(map(int, input().split()))
INF = 10 ** 16
dp = [-INF] * (n + 1)
dp[0] = 0
import heapq
q = []
for i in range(n):
	heapq.heappush(q, (-l[i], i))
for itera in range(n):
	(_, ind) = heapq.heappop(q)
	val = l[ind]
	dpnew = [-INF] * (n + 1)
	for i in range(n):
		left = dp[i] + abs(i - ind) * val
		right = dp[i] + abs(n - 1 - itera + i - ind) * val
		if dpnew[i] < right:
			dpnew[i] = right
		if dpnew[i + 1] < left:
			dpnew[i + 1] = left
	dp = dpnew
print(max(dp))


n = int(input())
a = list(map(int, input().split()))
num = []
for i in range(n):
	num.append([a[i], i])
num.sort(reverse=True)
dp = [[0] * (n + 1) for i in range(n + 1)]
ans = 0
for l in range(n):
	for r in range(n):
		if l + r == n:
			ans = max(ans, dp[l][r])
			break
		dp[l + 1][r] = max(dp[l + 1][r], dp[l][r] + num[l + r][0] * abs(num[l + r][1] - l))
		dp[l][r + 1] = max(dp[l][r + 1], dp[l][r] + num[l + r][0] * abs(num[l + r][1] - n + 1 + r))
print(ans)


def sep():
	return map(int, input().strip().split(' '))

def lis():
	return list(sep())
n = int(input())
ar = lis()
data = []
for i in range(n):
	data.append((ar[i], i))
data.sort(reverse=True)
dp = [[0] * (n + 1) for _ in range(n + 1)]
for i in range(n):
	(a, p) = data[i]
	for j in range(i + 1):
		dp[i + 1][j + 1] = max(dp[i + 1][j + 1], dp[i][j] + abs(n - 1 - j - p) * a)
		dp[i + 1][j] = max(dp[i + 1][j], dp[i][j] + abs(i - j - p) * a)
print(max(dp[-1]))


n = int(input())
a = list(map(int, input().split()))
ai = [[a_, i] for (i, a_) in enumerate(a)]
ai.sort(reverse=True)
dp = [[0] * (n + 1) for _ in range(n + 1)]
for i in range(n):
	for j in range(n - i):
		dp[i + 1][j] = max(dp[i + 1][j], dp[i][j] + ai[i + j][0] * (ai[i + j][1] - i))
		dp[i][j + 1] = max(dp[i][j + 1], dp[i][j] + ai[i + j][0] * (n - 1 - j - ai[i + j][1]))
ans = 0
for i in range(n):
	ans = max(ans, dp[i][n - i])
print(ans)