#C5098. Maximum Contiguous Subarray Sum Under K
Maximum Contiguous Subarray Sum Under K
Maximum Contiguous Subarray Sum Under K
This problem involves finding the maximum sum of a contiguous subarray such that the sum does not exceed a given integer K. Formally, for an array \(a_1, a_2, \dots, a_N\) and an integer \(K\), you need to compute
[ \max_{1 \leq l \leq r \leq N} \left{ S(l, r)=\sum_{i=l}^{r} a_i ;\middle|; S(l, r) \leq K \right} ]
If no contiguous subarray has a sum less than or equal to \(K\), output 0. The input will consist of multiple test cases. For each test case, the first line contains two integers \(N\) (the number of cards) and \(K\) (the maximum allowed sum for any contiguous subarray). The second line contains \(N\) integers representing the power values on the cards.
Your task is to process each test case and output the maximum contiguous subarray sum that does not exceed \(K\>.
inputFormat
The input is read from standard input and follows the format below:
T N₁ K₁ a₁ a₂ ... aₙ N₂ K₂ a₁ a₂ ... aₙ ... (T test cases in total)
Where:
T
is the number of test cases.- For each test case, the first line contains two integers:
N
(number of cards) andK
(maximum allowed sum). - The next line contains
N
integers representing the power values of the cards.
outputFormat
For each test case, output a single integer on a new line which represents the maximum sum of a contiguous subarray whose sum does not exceed K
.
1
1 10
10
10
</p>