CF2135A Against the Difference
Description
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 Format
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 Format
For each test case, output a single integer — the length of the longest neat subsequence of $$$a$$$.
Explanation/Hint
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.