n = gets.to_i
nums = gets.split.map(&:to_i)
second_maxs = []

l = 0
mul = 1
res = 0
ranks = []



0.upto(n - 2) do|l|
    (l + 1).upto(n - 1) do |r|
        sub_nums = nums[l..r]
        # p sub_nums
        second_max = sub_nums.sort.reverse[1]
        second_maxs << second_max
        res += second_max
        # res += mul * second_max
        # mul += 1
    end
end

puts res