CF1905D.Cyclic MEX

普及/提高-

通过率:0%

AC君温馨提醒

该题目为【codeforces】题库的题目,您提交的代码将被提交至codeforces进行远程评测,并由ACGO抓取测评结果后进行展示。由于远程测评的测评机由其他平台提供,我们无法保证该服务的稳定性,若提交后无反应,请等待一段时间后再进行重试。

题目描述

For an array aa , define its cost as i=1nmex([a1,a2,,ai])\sum_{i=1}^{n} \operatorname{mex} ^\dagger ([a_1,a_2,\ldots,a_i]) .

You are given a permutation ^\ddagger pp of the set {0,1,2,,n1}\{0,1,2,\ldots,n-1\} . Find the maximum cost across all cyclic shifts of pp .

mex([b1,b2,,bm])^\dagger\operatorname{mex}([b_1,b_2,\ldots,b_m]) is the smallest non-negative integer xx such that xx does not occur among b1,b2,,bmb_1,b_2,\ldots,b_m .

^\ddagger A permutation of the set {0,1,2,...,n1}\{0,1,2,...,n-1\} is an array consisting of nn distinct integers from 00 to n1n-1 in arbitrary order. For example, [1,2,0,4,3][1,2,0,4,3] is a permutation, but [0,1,1][0,1,1] is not a permutation ( 11 appears twice in the array), and [0,2,3][0,2,3] is also not a permutation ( n=3n=3 but there is 33 in the array).

输入格式

Each test consists of multiple test cases. The first line contains a single integer tt ( 1t1051 \le t \le 10^5 ) — the number of test cases. The description of the test cases follows.

The first line of each test case contains a single integer nn ( 1n1061 \le n \le 10^6 ) — the length of the permutation pp .

The second line of each test case contain nn distinct integers p1,p2,,pnp_1, p_2, \ldots, p_n ( 0pi<n0 \le p_i < n ) — the elements of the permutation pp .

It is guaranteed that sum of nn over all test cases does not exceed 10610^6 .

输出格式

For each test case, output a single integer — the maximum cost across all cyclic shifts of pp .

输入输出样例

  • 输入#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][2,1,0,5,4,3] with cost 0+0+3+3+3+6=150+0+3+3+3+6=15 .

In the second test case, the cyclic shift that yields the maximum cost is [0,2,1][0,2,1] with cost 1+1+3=51+1+3=5 .

首页