n=int(input())
a=list(map(int,input().split()))
#print(a)
'''
b=sorted(a,reverse=True)#降順ソート
'''
b=[]#sorting
c=[]#移動前
for i in range(n):
  b.append(max(a))
  c.append(a.index(max(a)))
  a[a.index(max(a))]=0
#print(b)
#print(c)



'''
for i in range(n):
  c.append(b.index(a[i]))
'''
dp=[[0]*n for i in range(n)]
dp[0][0]=0
x=0
y=0
for i in range(n):
  if dp[x][y]+((n-1-x)-c[i])*b[i]>dp[x][y]+(c[i]-y)*b[i]:
    #print('  from',c[i],'to',n-1-x,'の',b[i],'を足した値を')
    dp[x+1][y]=dp[x][y]+((n-1-x)-c[i])*b[i]
    x+=1
    #print('x:when dp',x,y,':',dp[x][y],'に入れる。')
  else:
    #print('  from',c[i],'to',y,'の',b[i],'を足した値を')
    dp[x][y+1]=dp[x][y]+(c[i]-y)*b[i]
    y+=1
    #print('y:when dp',x,y,':',dp[x][y],'に入れる。')
ans=[]
for i in range(n):
  #print(dp[i])
  for j in range(n):
    if i+j==n:
      ans.append(dp[i][j])
print(max(ans))