let main: () = {
    let N = nextInt()
    let A = listOfInt().enumerated().map { ($0.1, $0.0) }.sorted { $0.0 > $1.0 }
    var dp = Array(N + 2) { _ in Array(N + 2) { _ in -1001001009 } }
    dp[0][0] = 0
    repeate(N) { t in
        let (cost, n) = A[t]
        repeate(t + 1) { l in
            if dp[t][l] >= 0 {
                chmax(&dp[t + 1][l + 1], dp[t][l] + cost * abs(n - l))
                chmax(&dp[t + 1][l], dp[t][l] + cost * abs(N - 1 - (t - l) - n))
            }
        }
    }
    var ans = 0
    repeate(N + 1) { n in chmax(&ans, dp[N][n]) }
    print(ans)
}()

@inlinable func next() -> String { readLine()! }
@inlinable func nextInt() -> Int { Int(next())! }
@inlinable func listOfString() -> [String] { next().split(separator: " ").map(String.init) }
@inlinable func listOfInt() -> [Int] { listOfString().map { Int($0)! } }
extension Array {
    init(_ size: Int, _ initValue: ((Int) -> Element)) {
        self = [Element]()
        for index in 0 ..< size {
            let value = initValue(index)
            self.append(value)
        }
    }
}
extension Array where Element == Int {
    @inlinable func sum() -> Element { self.reduce(0, +) }
    init(_ size: Int) {
        self = [Element]()
        for _ in 0 ..< size {
            self.append(0)
        }
    }
}
prefix operator *
prefix func * <T> (a: [T]) -> (T, T) { (a[0], a[1]) }

@inlinable func repeate(_ times1: Int, _ action: (Int) -> Void) {
    for index1 in 0 ..< times1 {
        action(index1)
    }
}

@inlinable func chmax<T: Comparable>(_ m: inout T, _ q: T) { if (m < q) { m = q } }
@inlinable func chmin<T: Comparable>(_ m: inout T, _ q: T) { if (q < m) { m = q } }
