Mex

Time Limit: 5000ms
Memory Limit: 65535KB
This problem will be judged on HDU. Original ID: 4747
64-bit integer IO format: %I64d      Java class name: Main

Description

Mex is a function on a set of integers, which is universally used for impartial game theorem. For a non-negative integer set S, mex(S) is defined as the least non-negative integer which is not appeared in S. Now our problem is about mex function on a sequence.

Consider a sequence of non-negative integers {ai}, we define mex(L,R) as the least non-negative integer which is not appeared in the continuous subsequence from aL to aR, inclusive. Now we want to calculate the sum of mex(L,R) for all 1 <= L <= R <= n.

Input

The input contains at most 20 test cases.
For each test case, the first line contains one integer n, denoting the length of sequence.
The next line contains n non-integers separated by space, denoting the sequence.
(1 <= n <= 200000, 0 <= ai <= 10^9)
The input ends with n = 0.

Output

For each test case, output one line containing a integer denoting the answer.

Sample Input

3
0 1 3
5
1 0 2 0 1
0

Sample Output

5
24

Source

Language: 
Theme: 
Share Code? 

Powered by NB231 | Current Style: .