def dp(x, y, curr):
	if len(l) <= x + y:
		return curr
	v, pos = l[x + y]
	return max([
		dp(x + 1, y, curr + v * (pos - (x + 1) )),
		dp(x, y + 1, curr +  v * (len(l) - y - pos))])
  
n = int(input())
l = [ (y, x + 1)  for  x, y in  enumerate(map(int, input().split(" "))) ]
l = sorted(l, reverse=True)

print(dp(0, 0, 0))