Deforestation
InterviewTime limit2sMemory limit1024 MB
Given a tree of segment weights and a carrying limit W, find the minimum number of pieces needed so that each piece weighs at most W.
Problem
You want to remove a big tree from your property, but it is too big to carry all at once. If the maximum weight you can carry is , how many pieces do you have to cut it into?
The tree has a single trunk connected to the ground, and the trunk can split into multiple branches. Each branch can branch out further, and so on. Each segment of the tree is a continuous mass of wood, which may or may not split into multiple branches.
You can make cuts at any point on the tree: at the start, at the end, or anywhere in the middle of any segment. Branching is treated as an arbitrarily small part of the tree. You can cut immediately before or after a branch splits off without increasing the weight of the base branch, but this choice decides whether the child branches are cut off as a single piece or whether only one branch is cut off separately.
Input
The first line contains , your carrying capacity. The next line describes the first tree segment, its trunk.
A tree segment is described recursively. The first line contains two numbers: , the weight of the segment, and , the number of branches coming out of the segment at its end. The tree segment descriptions follow, one for each branch.
Output
Print one number, the number of pieces you have to cut the tree into.
Constraints
- The total weight of all tree segments does not exceed .
- The total number of segments does not exceed .
Hints

The image shows some possible solutions for the sample test cases.