#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 of length , where all numbers are arranged in a row from left to right.
You need to color each number in either red or blue, and then calculate the final score as follows:
Let be an integer array of length . For each number in ():
- If there is no number to the left of with the same color, then .
- Otherwise, let be the closest number to the left of with the same color. If , then ; otherwise, .
Your final score is the sum of all integers in , i.e., . 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 , indicating the number of test cases.
This is followed by test cases, each formatted as follows:
The first line contains a positive integer , indicating the length of the array.
The second line contains positive integers , representing the elements of array .
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:
- Color red and blue (). The score calculation is as follows:
- For , since there is no red number to its left, .
- For , the closest red number to its left is . Since , .
- For , since there is no blue number to its left, .
The final score for this scheme is .
- Color all red (). The score calculation is as follows:
- For , since there is no red number to its left, .
- For , the closest red number to its left is . Since , .
- For , the closest red number to its left is . Since , .
The final score for this scheme is .
- Color red and blue (). The score calculation is as follows:
- For , since there is no red number to its left, .
- For , since there is no blue number to its left, .
- For , the closest red number to its left is . Since , .
The final score for this scheme is .
It can be proven that no coloring scheme yields a final score greater than .
For the second test case, it can be proven that any coloring scheme results in a final score of .
For the third test case, an optimal coloring scheme is to color red and blue (). The corresponding , and the final score is .
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: , , .
| Test Case | ||
|---|---|---|