Codeforces Round 1046 (Div. 2)
C
Against the Difference
Codeforces
未评级
原题
← BD →
翻译中…

We define that a block is an array where all elements in it are equal to the length of the array. For example, $[3, 3, 3]$, $[1]$, and $[4, 4, 4, 4]$ are blocks, while $[1, 1, 1]$ and $[2, 3, 3]$ are not.

翻译中…

An array is called neat if it can be obtained by the concatenation of an arbitrary number of blocks (possibly zero). Note that an empty array is always neat.

翻译中…

You are given an array $a$ consisting of $n$ integers. Find the length of its longest neat subsequence$^{\text{∗}}$.

翻译中…

$^{\text{∗}}$A sequence $c$ is a subsequence of a sequence $a$ if $c$ can be obtained from $a$ by the deletion of several (possibly, zero or all) element from arbitrary positions.

翻译中…

Input

翻译中…

Each test contains multiple test cases. The first line contains the number of test cases $t$ ($1 \le t \le 10^4$). The description of the test cases follows.

翻译中…

The first line of each test case contains an integer $n$ ($1 \le n \le 2\cdot 10^5$) — the length of $a$.

翻译中…

The second line contains $n$ integers $a_1,a_2,\ldots,a_n$ ($1 \le a_i \le n$) — the elements of $a$.

翻译中…

It is guaranteed that the sum of $n$ over all test cases does not exceed $2\cdot 10^5$.

翻译中…

Output

翻译中…

For each test case, output a single integer — the length of the longest neat subsequence of $a$.

翻译中…

Note

翻译中…

In the first test case, the whole array $[1]$ is neat because it is a block.

翻译中…

In the second test case, the whole array $[2, 2]$ is neat because it is a block.

翻译中…

In the third test case, the whole array $[2,2,1,1]$ is neat because it is the concatenation of three blocks: $[2,2]$, $[1]$, and $[1]$.

翻译中…

In the fourth test case, one of the longest neat subsequences of $a$ is $[1, 3, 3, 3, 1]$.

翻译中…

In the fifth test case, the longest neat subsequence of $a$ is the empty subsequence.

翻译中…

样例

Input #1
6
1
1
2
2 2
4
2 2 1 1
6
1 2 3 3 3 1
8
8 8 8 8 8 8 8 7
10
2 3 3 1 2 3 5 1 1 7
Output #1
1
2
4
5
0
5
InputCopy OutputCopy
题目信息
题号
2136C
时间限制
2 s
内存限制
256 MB

题面与样例版权归 Codeforces 及出题人所有;中文翻译由 AI 生成(CI 管线),如有出入请以原文为准。