def ee
    n = gets.chomp.to_i
    pn = gets.chomp.split(/\s/).map{|x| x.to_i}

    sum = 0
    for l in 0..(n-2)
        for r in (l+1)..(n-1)
            sum += pn[l..r].sort[-2]
        end
    end
        
    puts sum
end

ee
