#P92. [CSP-S 2024] Red and blue array

[CSP-S 2024] Red and blue array

Person in Charge

Problem Description

Given a positive integer array AA of length nn, where all numbers are arranged in a row from left to right.

You need to color each number in AA either red or blue, and then calculate the final score as follows:

Let CC be an integer array of length nn. For each number AiA_i in AA (1in1 \le i \le n):

  • If there is no number to the left of AiA_i with the same color, then Ci=0C_i = 0.
  • Otherwise, let AjA_j be the closest number to the left of AiA_i with the same color. If Ai=AjA_i = A_j, then Ci=AiC_i = A_i; otherwise, Ci=0C_i = 0.

Your final score is the sum of all integers in CC, i.e., i=1nCi\sum \limits_{i=1}^n C_i. You need to maximize the final score. Please determine the maximum possible final score.

Input Format

There are multiple test cases.

The first line of input contains a positive integer TT, indicating the number of test cases.

This is followed by TT test cases, each formatted as follows:

The first line contains a positive integer nn, indicating the length of the array.

The second line contains nn positive integers A1,A2,,AnA_1, A_2, \dots, A_n, representing the elements of array AA.

Output Format

For each test case: Output a line containing a non-negative integer, representing the maximum possible final score.

Samples

3
3
1 2 1
4
1 2 3 4
8
3 5 2 5 1 2 1 4
1
0
8

Sample 1 Explanation

For the first test case, here are three possible coloring schemes:

  1. Color A1,A2A_1, A_2 red and A3A_3 blue (121\red{1}\red{2}\blue{1}). The score calculation is as follows:
    • For A1A_1, since there is no red number to its left, C1=0C_1 = 0.
    • For A2A_2, the closest red number to its left is A1A_1. Since A1A2A_1 \neq A_2, C2=0C_2 = 0.
    • For A3A_3, since there is no blue number to its left, C3=0C_3 = 0.
      The final score for this scheme is C1+C2+C3=0C_1 + C_2 + C_3 = 0.
  2. Color A1,A2,A3A_1, A_2, A_3 all red (121\red{121}). The score calculation is as follows:
    • For A1A_1, since there is no red number to its left, C1=0C_1 = 0.
    • For A2A_2, the closest red number to its left is A1A_1. Since A1A2A_1 \neq A_2, C2=0C_2 = 0.
    • For A3A_3, the closest red number to its left is A2A_2. Since A2A3A_2 \neq A_3, C3=0C_3 = 0.
      The final score for this scheme is C1+C2+C3=0C_1 + C_2 + C_3 = 0.
  3. Color A1,A3A_1, A_3 red and A2A_2 blue (121\red{1}\blue{2}\red{1}). The score calculation is as follows:
    • For A1A_1, since there is no red number to its left, C1=0C_1 = 0.
    • For A2A_2, since there is no blue number to its left, C2=0C_2 = 0.
    • For A3A_3, the closest red number to its left is A1A_1. Since A1=A3A_1 = A_3, C3=A3=1C_3 = A_3 = 1.
      The final score for this scheme is C1+C2+C3=1C_1 + C_2 + C_3 = 1.

It can be proven that no coloring scheme yields a final score greater than 11.

For the second test case, it can be proven that any coloring scheme results in a final score of 00.

For the third test case, an optimal coloring scheme is to color A1,A2,A4,A5,A7A_1, A_2, A_4, A_5, A_7 red and A3,A6,A8A_3, A_6, A_8 blue (35251214\red{35}\blue{2}\red{51}\blue{2}\red{1}\blue{4}). The corresponding C=[0,0,0,5,0,1,2,0]C = [0, 0, 0, 5, 0, 1, 2, 0], and the final score is 88.

Sample 2

See the files color/color2.in and color/color2.ans in the contestant directory.

Data Range

For all test data, it is guaranteed that: 1T101\le T\le 10, 2n2×1052\le n\le 2\times 10^5, 1Ai1061\le A_i\le 10^6.

Test Case nn AiA_i
141\sim 4 15\le 15
575\sim 7 102\le 10^2
8108\sim 10 2000\le 2000
11,1211,12 2×104\le 2\times 10^4 106\le 10^6
131513\sim 15 2×105\le 2\times 10^5 10\le 10
162016\sim 20 106\le 10^6