def max(a, b)
  a > b ? a : b
end

gets
ais = read_line.split.map(&.to_i64)
vals = ais.map_with_index {|a, i| {i.to_i64, a}}.sort_by! {|(i, a)| a}
dp = Array(Array(Int64)).new(n + 1) { Array(Int64).new(n, 0_i64) }
vals.each_with_index do |(i, a), i2|
  remains = vals.size - i2
  dp[i2].each_with_index do |v, j|
    next if j + remains > n
    left = j
    right = j + remains - 1
    dp[i2 + 1][j] = max(dp[i2 + 1][j], v + (left - j).abs)
    dp[i2 + 1][j + 1] = max(dp[i2 + 1][j + 1], v + (right - j).abs)
  end
end
p dp[vals.size][0]
