CF1905D.Cyclic MEX
普及/提高-
通过率:0%
AC君温馨提醒
该题目为【codeforces】题库的题目,您提交的代码将被提交至codeforces进行远程评测,并由ACGO抓取测评结果后进行展示。由于远程测评的测评机由其他平台提供,我们无法保证该服务的稳定性,若提交后无反应,请等待一段时间后再进行重试。
题目描述
For an array a , define its cost as ∑i=1nmex†([a1,a2,…,ai]) .
You are given a permutation ‡ p of the set {0,1,2,…,n−1} . Find the maximum cost across all cyclic shifts of p .
†mex([b1,b2,…,bm]) is the smallest non-negative integer x such that x does not occur among b1,b2,…,bm .
‡ A permutation of the set {0,1,2,...,n−1} is an array consisting of n distinct integers from 0 to n−1 in arbitrary order. For example, [1,2,0,4,3] is a permutation, but [0,1,1] is not a permutation ( 1 appears twice in the array), and [0,2,3] is also not a permutation ( n=3 but there is 3 in the array).
输入格式
Each test consists of multiple test cases. The first line contains a single integer t ( 1≤t≤105 ) — the number of test cases. The description of the test cases follows.
The first line of each test case contains a single integer n ( 1≤n≤106 ) — the length of the permutation p .
The second line of each test case contain n distinct integers p1,p2,…,pn ( 0≤pi<n ) — the elements of the permutation p .
It is guaranteed that sum of n over all test cases does not exceed 106 .
输出格式
For each test case, output a single integer — the maximum cost across all cyclic shifts of p .
输入输出样例
输入#1
4 6 5 4 3 2 1 0 3 2 1 0 8 2 3 6 7 0 1 4 5 1 0
输出#1
15 5 31 1
说明/提示
In the first test case, the cyclic shift that yields the maximum cost is [2,1,0,5,4,3] with cost 0+0+3+3+3+6=15 .
In the second test case, the cyclic shift that yields the maximum cost is [0,2,1] with cost 1+1+3=5 .