#1行目
hoge = gets.chomp.to_i - 1

#2行目読み込み
b = gets.chomp.split(" ").map!{|i| i.to_i}

a = []
unchi = 0

for j in 0..hoge do
  
for i in j..(hoge-1) do
a[i] = b.first(i+2).max(2)[1]
end 

unchi = unchi + a.inject(:+)
a[j] = 0
b[j] = 0
end
  
print unchi