n = gets.to_i
a = gets.split.map(&:to_i)

hash = {0 => 0}

a.each do |w| 
    new_hash = Hash.new(Float::INFINITY)
    hash.each do |l, cost|
        new_hash[l] = cost + w if cost + w < new_hash[l]
        if l < w 
            new_hash[w] = cost if cost < new_hash[w]
        end 
    end 
    hash = new_hash
end
p hash.values.min