N = gets.to_i
P = gets.split.map(&:to_i)
sum = 0
remain = (1..N).to_a
for l in 0..N-2
    max1 = P[l]
    max2 = -1
    for r in l+1..N-1
        _, max2, max1 = [max1, max2, P[r]].sort
        if max2 == remain[-1] - 1
            sum += max2 * (N - 1 - r + 1)
            break
        else
            sum += max2
        end
    end
    remain.delete(P[l])
end
p sum