Repovive
  • Dashboard
  • Ranking
  • Submissions
  • Discussion
  1. Contests
  2. Premier Round 2
  3. 3H

My submissions

/
/
/
/

Announcements

My submissions

/
/
/
/

Announcements

Solve
Author

3H. Max Mex

3500 pts·2s·256 MB

Input

Output

Samples

Input
t
2
Case 1
5
2 1 3 5 4
Case 2
10
4 1 7 2 10 5 3 9 6 8
Explanation
Case 1
Case 2
Output
Case 1
27
Case 2
100

The beauty of an array of natural numbers is defined as follows.

Choose a non-negative integer kkk and subtract kkk from every element of the array. If all elements remain non-negative, then this kkk is valid. For every valid kkk, compute the MEX of the resulting array. The beauty of the array is the maximum of these MEX values over all valid kkk.

You are given a permutation of length nnn. For every pair of indices 1≤l≤r≤n1 \le l \le r \le n1≤l≤r≤n, consider the subarray (al,al+1,…,ar)(a_l, a_{l+1}, \dots, a_r)(al​,al+1​,…,ar​) and its beauty. Output the sum of these beauties over all such pairs (l,r)(l, r)(l,r).

Constraints:

  • 1≤t≤1041 \le t \le 10^41≤t≤104
  • 1≤n≤5×1051 \le n \le 5 \times 10^51≤n≤5×105
  • a1,a2,…,ana_1, a_2, \ldots, a_na1​,a2​,…,an​ is a permutation of 1,2,…,n{1,2,\ldots, n}1,2,…,n
  • The sum of nnn over all test cases does not exceed 5×1055 \times 10^55×105.
tna1a2…an}×tt \\[0.5em] \left. \begin{array}{l} n \\[0.2em] a_1 \quad a_2 \quad \dots \quad a_n \end{array} \right\} \times ttna1​a2​…an​​}×t
ans}×t\left. \begin{array}{l} \text{ans} \end{array} \right\} \times tans​}×t

consider the subarray [1..3][1..3][1..3] which is [2,1,3][2,1,3][2,1,3]. Its minimum is 111. If we choose k=1k=1k=1 and subtract it from all elements, we get [1,0,2][1,0,2][1,0,2] (all non-negative). The MEX of [1,0,2][1,0,2][1,0,2] is 333, so the beauty of this subarray is at least 333 (and it is exactly 333).

There are 555555 subarrays and the answer is 100100100.