from math import *
from collections import *
import sys
sys.setrecursionlimit(10**9)
mod = 10**9 + 7
test = 1
#test = int(input())
d = dict()
def mem(i,l,r):
	if(l > r):
		return 0
	if (l,r) in d:
		return d[(l,r)]
	ans1 = a[i][0]*abs(a[i][1]-l)+mem(i+1,l+1,r)
	ans2 = a[i][0]*abs(r-a[i][1])+mem(i+1,l,r-1)
	d[(l,r)] = max(ans1,ans2)
	return d[(l,r)]
for __ in range(test):
	n = int(input())
	a = list(map(int,input().split()))
	for i in range(n):
		a[i] = [a[i],i]
	a.sort(reverse = True)
	l,r = 0,n-1
	print(mem(0,l,r))