#C12377. Maximum Continuous Subarray Sum Under Budget
Maximum Continuous Subarray Sum Under Budget
Maximum Continuous Subarray Sum Under Budget
You are given a list of integers representing prices of items and an integer representing your budget. Your task is to find the maximum total price of any continuous subarray such that its sum does not exceed the budget.
The problem can be formally described as follows: Given an array \(A = [a_1, a_2, \dots, a_n]\) and an integer \(B\), find the maximum sum \(S\) of a contiguous segment \(A[i \dots j]\) for which \(S \le B\). If no such subarray exists, output 0. This should be solved using an efficient algorithm (e.g., the two-pointer or sliding window technique).
Example:
Input: prices = [100, 200, 300, 400, 500], budget = 800 Output: 700
inputFormat
The input is given via standard input (stdin) and consists of two lines.
The first line contains two integers \(n\) and \(B\) where \(n\) is the number of items and \(B\) is the budget.
The second line contains \(n\) space-separated integers representing the prices of the items.
Example: 5 800
on the first line and 100 200 300 400 500
on the second line.
outputFormat
The output should be printed to standard output (stdout) and consist of a single integer representing the maximum total price of a continuous subarray that does not exceed the given budget.
Example: 700
5 800
100 200 300 400 500
700
</p>