#C7517. Minimum Contiguous Subarrays

    ID: 51397 Type: Default 1000ms 256MiB

Minimum Contiguous Subarrays

Minimum Contiguous Subarrays

You are given a sequence of weights and an integer limit. Your task is to determine the minimum number of contiguous subarrays such that the sum of the weights in each subarray does not exceed the limit.

Formally, given an array \(w = [w_1, w_2, \dots, w_n]\) and an integer limit \(l\), partition the array into the fewest possible contiguous segments (subarrays) so that for each segment \(S\), \(\sum_{w \in S} w \le l\).

The problem requires you to determine this minimum count. For example, if the weights are [1, 2, 3, 4, 5] and \(l = 5\), one valid partitioning is [1,2], [3], [4], [5] which gives 4 subarrays.

inputFormat

The input is given via stdin and consists of two lines:

  • The first line contains two integers \(n\) and \(l\) separated by a space, where \(n\) is the number of weights and \(l\) is the weight limit for each subarray.
  • If \(n > 0\), the second line contains \(n\) space-separated integers, representing the weights.

outputFormat

Output to stdout a single integer: the minimum number of contiguous subarrays such that the sum of the weights in each subarray does not exceed \(l\).

## sample
5 5
1 2 3 4 5
4