N = gets.to_i
pi = gets.chomp.split(" ").map(&:to_i)

ans = 0

for i in 0...(N-1) do
  max = [pi[i], pi[i+1]].max
  s_max = [pi[i], pi[i+1]].min
  for j in 0..i do
    #print s_max
    ans += s_max
    if max < pi[j] then
      s_max = max
      max = pi[j]
    elsif s_max < pi[j]
      s_max = pi[j]
    end
  end
  #print "\n"
end

p ans
