#P1181. 数列分段 Section I

数列分段 Section I

Description

Given a sequence AiA_i of length NN consisting of non-negative integers, split it into several consecutive segments such that the sum of each segment does not exceed MM (it can be equal to MM). Find the minimum number of segments needed to meet the requirement.

Input Format

The first line contains two positive integers N,MN, M, representing the length of the sequence AiA_i and the maximum allowed sum of each segment. The second line contains NN space-separated non-negative integers AiA_i, as described.

Output Format

Output a single integer: the minimum number of segments.

5 6
4 2 4 5 1
3

Hint

For 20%20\% of the testdata, there is N10N≤10.

For 40%40\% of the testdata, there is N1000N≤1000.

For 100%100\% of the testdata, there are N100000,M109N≤100000, M≤10^9, MM is greater than the maximum element, and the sum of AiA_i does not exceed 10910^9.

Split the sequence as follows:

[4][24][51][4][2 4][5 1]

The sum of the first segment is 44, the sum of the 22nd segment is 66, and the sum of the 33rd segment is 66, all not exceeding M=6M=6. It can be proven that 33 is the minimum number of segments.

Translated by ChatGPT 5