Deforestation

아직 제출이 없습니다시간 제한2초메모리 제한1024 MB

문제

You want to remove a big tree from your property, but it's too big for you to carry all at once. How many pieces do you have to cut it into if the maximum weight you can carry is WW?

The tree has a single trunk connected to the ground and can split out into multiple branches. All of those branches can branch out further etc. So each segment of the tree is a continuous mass of wood, which may or may not split out into multiple branches.

You can make cuts at any point on the tree; start, end, or anywhere in the middle of any segment. You can consider branching as an arbitrarily small part of the tree, i.e. you can cut immediately before or after a branch splits off without increasing the weight of the base branch, but it will affect whether the child branches are cut off as a single piece or just one branch is cut off separately.

입력

The first line of the input will contain WW, your carrying capacity. The next line will continue with the description of the first tree segment; its trunk.

A tree segment description is defined recursively. The first line contains two numbers MM, weight of the segment, and NN, number of branches coming out of the segment at its end. This is followed by NN tree segment descriptions, describing each one of the branches.

출력

Output one number, the number of pieces you have to cut the tree into.

제한

  • 1W,M1091 \leq W, M \leq 10^9
  • 0N1050 \leq N \leq 10^5
  • Total weight of all tree segments will not exceed 10910^9.
  • Total number of segments will not exceed 10510^5.

힌트

Image shows some possible solutions of sample test cases.