CodeTON Round 5 (Div. 1 + Div. 2, Rated, Prizes!)
C
Tenzing and Balls
Codeforces
1500
原题
← BD →
翻译中…

Enjoy erasing Tenzing, identified as Accepted!

翻译中…

Tenzing has $n$ balls arranged in a line. The color of the $i$-th ball from the left is $a_i$.

翻译中…

Tenzing can do the following operation any number of times:

翻译中…
  • select $i$ and $j$ such that $1\leq i \lt j \leq |a|$ and $a_i=a_j$,
  • remove $a_i,a_{i+1},\ldots,a_j$ from the array (and decrease the indices of all elements to the right of $a_j$ by $j-i+1$).
翻译中…

Tenzing wants to know the maximum number of balls he can remove.

翻译中…

Input

翻译中…

Each test contains multiple test cases. The first line of input contains a single integer $t$ ($1\leq t\leq 10^3$) — the number of test cases. The description of test cases follows.

翻译中…

The first line contains a single integer $n$ ($1\leq n\leq 2\cdot 10^5$) — the number of balls.

翻译中…

The second line contains $n$ integers $a_1,a_2,\ldots,a_n$ ($1\leq a_i \leq n$) — the color of the balls.

翻译中…

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

翻译中…

Output

翻译中…

For each test case, output the maximum number of balls Tenzing can remove.

翻译中…

Note

翻译中…

In the first example, Tenzing will choose $i=2$ and $j=3$ in the first operation so that $a=[1,3,3]$. Then Tenzing will choose $i=2$ and $j=3$ again in the second operation so that $a=[1]$. So Tenzing can remove $4$ balls in total.

翻译中…

In the second example, Tenzing will choose $i=1$ and $j=3$ in the first and only operation so that $a=[2]$. So Tenzing can remove $3$ balls in total.

翻译中…

样例

Input #1
2
5
1 2 2 3 3
4
1 2 1 2
Output #1
4
3
InputCopy OutputCopy
题目信息
题号
1842C
时间限制
1 s
内存限制
256 MB
标签
动态规划

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