CF BUDDY
← Problems·

1842C · Tenzing and Balls

1500 · dp

Problem: Tenzing has nn balls arranged in a line. The color of the ii-th ball from the left is aia_i.

Tenzing can do the following operation any number of times:

  • select ii and jj such that 1i<ja1\leq i < j \leq |a| and ai=aja_i=a_j,
  • remove ai,ai+1,,aja_i,a_{i+1},\ldots,a_j from the array (and decrease the indices of all elements to the right of aja_j by ji+1j-i+1).

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

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

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

The second line contains nn integers a1,a2,,ana_1,a_2,\ldots,a_n (1ain1\leq a_i \leq n) — the color of the balls.

It is guaranteed that sum of nn of all test cases will not exceed 21052\cdot 10^5.

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

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

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

Sample Cases

Case 1

Input

2
5
1 2 2 3 3
4
1 2 1 2

Output

4
3

Similar problems

00:00:00
Loading editor…
Welcome! I'm your coding tutor for this problem. Use the chips below to reveal stored hints or get AI feedback on your code. I'll guide you step by step — never giving away the solution.

Sign in to unlock AI tutor feedback