#K41487. Maximum Subarray Sum Under Constraint
Maximum Subarray Sum Under Constraint
Maximum Subarray Sum Under Constraint
You are given an array of n integers and an integer S. Your task is to find the maximum possible sum of a non-empty contiguous subarray such that the sum is less than or equal to S.
If no such subarray exists, output 0
.
Note: The subarray must be contiguous and must contain at least one element.
Example:
Input: 3 5 10 1 2 3 4 5 4 5 2 -1 2 3 6 15 1 -2 3 10 -4 7</p>Output: 10 4 15
inputFormat
The first line contains an integer t
denoting the number of test cases.
For each test case, the first line contains two integers n
and S
where n
is the number of elements in the array and S
is the maximum allowed sum.
The second line contains n
space-separated integers, the elements of the array.
outputFormat
For each test case, output a single line containing the maximum possible sum of a contiguous subarray whose sum is less than or equal to S
. If no such subarray exists, output 0
.
3
5 10
1 2 3 4 5
4 5
2 -1 2 3
6 15
1 -2 3 10 -4 7
10
4
15
</p>