let n = Int(readLine()!)!
let a = readLine()!.split(separator: " ").map { Int($0)! }

let sorted = Array(zip(a, Array(0..<n))).sorted(by: { $0.0 > $1.0 })

var dp: [[Int]] = Array(
  repeating: Array(
    repeating: 0,
    count: n + 1
  ),
  count: n + 1
)
dp[0][0] = 0

var answer = 0
for x in 0...n {
  for y in 0...n where 0 < x + y && x + y <= n {
    if x == 0 {
      dp[x][y] = dp[x][y-1] - sorted[x + y - 1].0 * (y - 1 - sorted[x + y - 1].1)
      continue
    }
    if y == 0 {
      dp[x][y] = dp[x-1][y] + sorted[x + y - 1].0 * (n - x - sorted[x + y - 1].1)
      continue
    }
    dp[x][y] = max(
      dp[x-1][y] + sorted[x + y - 1].0 * (n - x - sorted[x + y - 1].1),
      dp[x][y-1] - sorted[x + y - 1].0 * (y - 1 - sorted[x + y - 1].1)
    )
    if x + y == n && dp[x][y] > answer { answer = dp[x][y] }
  }
}

print(answer)
