import Foundation

_ = readLine()
let friendlinessList = readLine()!
    .split(separator: " ")
    .compactMap { Int($0) }
    .sorted(by: >)

var circle = [(Int, Int, Int)]()

for friendliness in friendlinessList {
    if circle.isEmpty {
        circle = [(friendliness, 0, friendliness)]
    } else if circle.count == 1 {
        circle[0].2 = friendliness
        circle.insert((friendliness, circle[0].0, friendliness), at: 0)
    } else {
        let (index, max) = circle.lazy
            .map { $0.2 }
            .enumerated()
            .max { $0.1 < $1.1 }!
        circle[index].2 = friendliness
        circle.insert((friendliness, max, friendliness), at: index)
    }
}

print(circle.lazy.map { $0.1 }.reduce(0, +))
