import numpy as np

def main():
  n = int( input() )
  a = list( map( int, input().split() ) )
  aa = np.zeros((n,2))
  for i in range(n):
    aa[i][0] = a[i]
    aa[i][1] = i
  a2 = np.ndarray.argsort(aa, axis=0)
  for i in range(n):
    tmp = a2[i][0]
    a2[i][0] = aa[ tmp ][0]
    a2[i][1] = aa[ tmp ][1]
  for i in range(n):
    print("",end="")
    aa[i][0] = a2[ i ][0]
    aa[i][1] = a2[ i ][1]
  aa = aa[::-1]
  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()