2135A.md (2072B)
1 Source: https://codeforces.com/problemset/problem/2135/A 2 3 A. Against the Difference 4 5 time limit per test 6 7 2 seconds 8 9 memory limit per test 10 11 256 megabytes 12 13 input 14 15 standard input 16 17 output 18 19 standard output 20 21 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. 22 23 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. 24 25 You are given an array $a$ consisting of $n$ integers. Find the length of its longest neat subsequence$^{\text{∗}}$. 26 27 $^{\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. 28 29 Input 30 31 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. 32 33 The first line of each test case contains an integer $n$ ($1 \le n \le 2\cdot 10^5$) — the length of $a$. 34 35 The second line contains $n$ integers $a_1,a_2,\ldots,a_n$ ($1 \le a_i \le n$) — the elements of $a$. 36 37 It is guaranteed that the sum of $n$ over all test cases does not exceed $2\cdot 10^5$. 38 39 Output 40 41 For each test case, output a single integer — the length of the longest neat subsequence of $a$. 42 43 Example 44 45 Input 46 47 ``` 48 6 49 50 1 51 52 1 53 54 2 55 56 2 2 57 58 4 59 60 2 2 1 1 61 62 6 63 64 1 2 3 3 3 1 65 66 8 67 68 8 8 8 8 8 8 8 7 69 70 10 71 72 2 3 3 1 2 3 5 1 1 7 73 ``` 74 75 Output 76 77 ``` 78 1 79 2 80 4 81 5 82 0 83 5 84 ``` 85 86 Note 87 88 In the first test case, the whole array $[1]$ is neat because it is a block. 89 90 In the second test case, the whole array $[2, 2]$ is neat because it is a block. 91 92 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]$. 93 94 In the fourth test case, one of the longest neat subsequences of $a$ is $[1, 3, 3, 3, 1]$. 95 96 In the fifth test case, the longest neat subsequence of $a$ is the empty subsequence.