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))
    #print(aj,j,l,r,abs(r-j))
  #print(now,'#',aj,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(now)
print(max(now))