#C8530. Maximum Consecutive Water Fetching
Maximum Consecutive Water Fetching
Maximum Consecutive Water Fetching
You are given n villagers standing in a line. Each villager wants to fetch a certain amount of water, represented by an array A
of n positive integers. The villagers are connected by a rope that can carry at most c units of water. Your task is to determine the maximum total amount of water that can be fetched by a consecutive group of villagers such that the sum does not exceed the rope's capacity.
Formally, you need to find the maximum sum of a contiguous subarray A[i...j] such that:
\[ \sum_{k=i}^{j} A[k] \leq c \]
If no contiguous segment satisfies this condition, output 0.
inputFormat
The first line contains two integers, n and c, where n is the number of villagers and c is the maximum load capacity of the rope.
The second line contains n space-separated integers representing the array A
, where each integer denotes the amount of water each villager wants to fetch.
\(1 \leq n \leq 10^5\) (assumed) and all water amounts are positive integers.
outputFormat
Output a single integer representing the maximum total water that can be fetched consecutively without exceeding the rope's capacity c.
## sample5 10
2 3 5 4 6
10