import numpy as np

def main():
  n = int( input() )
  a = list( map( int, input().split() ) )
  aa = []
  for i in range(n):
    aa += [[a[i],i]]
  aa.sort(reverse = True)
  dp = np.zeros(n+10)
  dppre = np.zeros(n+10)
  for i in range(n):
    for j in range(i+5):
      dppre[j] = dp[j]
      dp[j] = 0
    for j in range(i+1):
      dp[j+1] = max(dp[j+1], dppre[j] + aa[i][0] *  abs(aa[i][1]-j)   )
      dp[j]   = max(dp[j],   dppre[j] + aa[i][0] *  abs(aa[i][1]-(n-1-i+j))  )
  ans = int(max(dp))
  print(ans)
  

main()