#P5745. Maximum Subarray Sum Under Limit
Maximum Subarray Sum Under Limit
Maximum Subarray Sum Under Limit
Given a sequence of (n) positive integers (a_1, a_2, \cdots, a_n) and an integer (m), find a contiguous subarray ([i, j]) such that the sum of its elements is maximized while not exceeding (m). In other words, find (i) and (j) (with (1 \le i \le j \le n)) so that (\sum_{k=i}^{j} a_k \le m) is as large as possible. If there are multiple subarrays yielding the same maximum sum, choose the one with the smallest starting index (i).
inputFormat
The first line contains two space-separated integers: \(n\) and \(m\).
The second line contains \(n\) space-separated positive integers representing \(a_1, a_2, \cdots, a_n\).
outputFormat
Output three space-separated integers: \(i\), \(j\), and the maximum subarray sum. Here \(i\) and \(j\) denote the starting and ending indices (1-indexed) of the chosen subarray, respectively.
sample
5 12
2 1 3 4 5
3 5 12