Best Choice (APCS 2024-06 Expert)
2.0s 256MYou are given a sequence of \(n\) positive integers. Select a prefix and a suffix that do not overlap; either may be empty. The selected numbers must contain equally many odd and even values, and their sum must not exceed \(k\). Find the maximum possible sum. Selecting nothing is allowed and gives \(0\).
Input Format
The first line contains \(n,k\). The second line contains the \(n\) values \(a_i\) in order.
Output Format
Print the maximum valid sum.
Constraints
\(1\le n\le300000\), \(1\le k\le10^9\), \(1\le a_i\le5000\). In every prefix, the absolute difference between the numbers of odd and even values is at most \(2000\).
Scoring
- 20 points: \(n=1000\).
- 80 points: No additional restrictions.
Each scored test is worth 5 points.
Sample Input 1
8 22
1 5 2 2 9 3 5 8
Sample Output 1
16
Sample Input 2
7 20
7 7 8 2 8 9 5
Sample Output 2
0
Source
APCS 2024-06 public archive version: o079。
Log in to write and submit code.
Log in