import Foundation

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

var circle = [Int]()
var comfortableness = [Int]()
var preds = [Int]()

for friendliness in friendlinessList {
    if circle.isEmpty {
        comfortableness.append(0)
        circle.append(friendliness)
        preds.append(friendliness)
    } else if circle.count == 1 {
        comfortableness.append(circle[0])
        circle.append(friendliness)
        preds = [friendliness, friendliness]
    } else {
        let max = preds.max()!
        let index = preds.firstIndex(of: max)!
        comfortableness.insert(max, at: index)
        circle.insert(friendliness, at: index)
        preds[index] = friendliness
        preds.insert(friendliness, at: index)
    }
}

print(comfortableness.reduce(0, +))
