#K15366. Taco: Maximum Sum Segment with Threshold

    ID: 24341 Type: Default 1000ms 256MiB

Taco: Maximum Sum Segment with Threshold

Taco: Maximum Sum Segment with Threshold

You are given T test cases. For each test case, you are provided a sequence of N integers and an integer threshold M. Your task is to compute the sum of a contiguous segment of the sequence such that every element in the segment is less than or equal to M, and the segment sum is maximized.

More formally, for a contiguous subarray S, if every element s in S satisfies s \le M, define its sum as \[ \sum_{s \in S} s \] Your goal is to choose a contiguous subarray with the maximum possible sum under the given constraint. If no valid segment exists, output 0.

Note: When processing the sequence, if an element is greater than M, the current segment is terminated and a new segment begins immediately after that element. Also, if the computed sum becomes negative, it is reset to 0.

Example:

Input:
2
5 10
1 2 -1 4 5
7 3
-1 -2 4 3 2 1 5

Output: 11 6

</p>

inputFormat

The input is read from stdin and has the following format:

  • The first line contains an integer T, representing the number of test cases.
  • Each test case starts with a line containing two integers, N (the length of the sequence) and M (the threshold).
  • The next line contains N integers separated by spaces, representing the sequence.

See the sample input for clarification.

outputFormat

For each test case, output the sum of the maximum contiguous segment that satisfies the condition (every element in the segment is \( \le M \)). If there is no valid segment, output 0. Each result should be printed on its own line to stdout.

## sample
2
5 10
1 2 -1 4 5
7 3
-1 -2 4 3 2 1 5
11

6

</p>