#K55712. Maximum Subarray Sum Under a Threshold

    ID: 30037 Type: Default 1000ms 256MiB

Maximum Subarray Sum Under a Threshold

Maximum Subarray Sum Under a Threshold

Given an array of integers and a threshold T, your task is to find the largest sum of any contiguous subarray that does not exceed T. If there is no valid subarray (i.e. every subarray's sum is greater than T), output 0.

Recall that a subarray is a contiguous portion of an array. The optimal solution should have a time complexity that handles large input sizes efficiently. You may use a sliding window technique to solve this problem.

Note: All formulas are in LaTeX format. For example, the condition in the problem is: \(sum \le T\).

inputFormat

The input is given via standard input (stdin) with the following format:

  • The first line contains two integers: n (the number of elements in the array) and T (the threshold).
  • The second line contains n space-separated integers denoting the elements of the array.

outputFormat

Output a single integer—the maximum sum of a contiguous subarray that is less than or equal to T. If no such subarray exists, output 0.

The output should be printed via standard output (stdout).

## sample
5 9
1 2 3 4 5
9

</p>