G - ΣШX
Problem Statement
You are given a length-N sequence of non-negative integers A=(A1,…,AN).
Find the sum of mex(Al,⋯,Ar) over all pairs of integers (l,r) satisfying 1≤l≤r≤N.
Here, mex(Al,⋯,Ar) denotes the smallest non-negative integer not contained in Al,⋯,Ar.
Constraints
- 1≤N≤3×105
- 0≤Ai≤N
- All input values are integers.
The input is given from Standard Input in the following format:
$N$
$A_1$ $A_2$ $\cdots$ $A_N$
Output
Output the answer.
3
1 2 0
5
The answer is 5, the sum of the following 6 values.
- mex(A1)=mex(1)=0
- mex(A1,A2)=mex(1,2)=0
- $\mathrm{mex}({A_1,A_2,A_3}) = \mathrm{mex}({1,2,0}) = 3$
- mex(A2)=mex(2)=0
- mex(A2,A3)=mex(2,0)=1
- mex(A3)=mex(0)=1
6
2 1 0 2 1 4
31
The answer is 31, the sum of the following 21 values.
- mex(A1)=mex(2)=0
- mex(A1,A2)=mex(2,1)=0
- $\mathrm{mex}({A_1,A_2,A_3}) = \mathrm{mex}({2,1,0}) = 3$
- $\mathrm{mex}({A_1,A_2,A_3,A_4}) = \mathrm{mex}({2,1,0,2}) = 3$
- $\mathrm{mex}({A_1,A_2,A_3,A_4,A_5}) = \mathrm{mex}({2,1,0,2,1}) = 3$
- $\mathrm{mex}({A_1,A_2,A_3,A_4,A_5,A_6}) = \mathrm{mex}({2,1,0,2,1,4}) = 3$
- mex(A2)=mex(1)=0
- mex(A2,A3)=mex(1,0)=2
- $\mathrm{mex}({A_2,A_3,A_4}) = \mathrm{mex}({1,0,2}) = 3$
- $\mathrm{mex}({A_2,A_3,A_4,A_5}) = \mathrm{mex}({1,0,2,1}) = 3$
- $\mathrm{mex}({A_2,A_3,A_4,A_5,A_6}) = \mathrm{mex}({1,0,2,1,4}) = 3$
- mex(A3)=mex(0)=1
- mex(A3,A4)=mex(0,2)=1
- $\mathrm{mex}({A_3,A_4,A_5}) = \mathrm{mex}({0,2,1}) = 3$
- $\mathrm{mex}({A_3,A_4,A_5,A_6}) = \mathrm{mex}({0,2,1,4}) = 3$
- mex(A4)=mex(2)=0
- mex(A4,A5)=mex(2,1)=0
- $\mathrm{mex}({A_4,A_5,A_6}) = \mathrm{mex}({2,1,4}) = 0$
- mex(A5)=mex(1)=0
- mex(A5,A6)=mex(1,4)=0
- mex(A6)=mex(4)=0
25
2 2 1 2 2 0 2 2 2 1 2 2 2 0 2 2 2 2 1 2 2 2 2 0 2
624