N = int(input())
A = list(map(int, input().split()))
Ab = []
for i in range(N):
  Ab.append([A[i], i])
  
Ab.sort(reverse=True)
#dp[i][j] Aの大きいじゅんにi個とったとき、左寄せj個、右寄せi-j個としたときのMAX
dp = [[0 for _ in range(N+5)] for _ in range(N+5)]
dp[0][0] = 0

for i in range(0,N):
  a, p = Ab[i]
  for j in range(0,i+1):
    r = i - j
    dp[i+1][j+1] = max(dp[i+1][j+1], dp[i][j] + a * (p-j))
    dp[i+1][j]  = max(dp[i+1][j], dp[i][j] + a * (N-r-p-1))
                      
ans = 0
for i in range(N):
  ans = max(ans, dp[N][i])
  
print(ans)
#for a in dp:
#  print(a)
                       