Packing Books into Boxes
InterviewTime limit2sMemory limit128 MB
Given book weights in order and a per-box weight limit, compute the minimum number of boxes needed by greedily filling each box in sequence.
- Level
Easy2 of 10
- Topics
- Greedy, Array, Simulation
- Solved
- No attempts yet
Problem
A pile of books is stacked like a tower, and you want to pack them into boxes to ship. Because the books are stacked in order, you can only take them one at a time from the top and place them into boxes in that exact order.
Each book has a weight, and each box can hold books whose total weight is at most M. Packing the books in their stacked order, write a program that finds the minimum number of boxes needed.
Input
The first line contains the number of books N and the maximum weight M a single box can hold. N is an integer with 0 ≤ N ≤ 50, and M is a natural number with M ≤ 1000. If N is greater than 0, the second line contains the weights of the books in stacking order, separated by spaces. Each book's weight is a natural number not greater than M.
Output
Print the minimum number of boxes needed on the first line.