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

res = Array.new(n, -1)

n.times do |i|
    maxs_idx = [0, i - 1].max
    max_num = maxs[maxs_idx]
    next_max_num = maxs[maxs_idx + 1] || 10**10
    next_max_num = 10**10 if i == 0
    res[i] = [max_num, next_max_num].min
end

puts res.inject(:+)