Problems > Fewest Bounded Pieces

Fewest Bounded Pieces

Difficulty: 1700 5000 ms 128 MB

greedy

Cut the array into the minimum number of contiguous pieces so each piece's sum is at most K. Print the minimum count, or −1 if some single element exceeds K.

Input: the first line has N and K; the second line has N positive integers.

Output: the minimum number of pieces, or −1.

Constraints:

  • 1 ≤ N ≤ 105
  • 1 ≤ K ≤ 109
  • 1 ≤ ai ≤ 1000

Sample Tests

Input 1
5 6
1 2 3 4 5
Output 1
3
Input 2
3 2
1 5 1
Output 2
-1

Read the Editorial

Discussion (opens after you solve — no spoilers before)

No comments yet.

Submit Your Solution

Log in or create a free account to run and submit code.