Gimli's Gullet
InterviewTime limit1sMemory limit256 MB
Pack unlimited servings of M foods into capacity C for the most calories, breaking ties by the lexicographically smallest serving counts.
- Level
Medium5 of 10
- Topics
- Dynamic programming
- Solved
- No attempts yet
Problem
Legolas and Gimli are packing rations before they leave the Woodland Realm, and the kitchen has badly underestimated the belly of a dwarf. Gimli refuses to take a step until his pack holds as many calories as it can.
The kitchen has kinds of food. Each kind has a volume per serving and a calorie count per serving. Gimli can order any number of servings of a kind, including none. Choose how many servings of each food to order so that the total volume fits in the space left in the pack and the total calorie count is as large as possible.
Several orders can reach the same largest calorie count. In that case the answer is the one whose sequence of quantities comes first in lexicographic order: order as few servings as possible of the first food, then, under that condition, as few servings as possible of the second food, and so on.
Input
The input holds several test cases. The first line of a test case has one integer , the space left in Gimli's pack in cubic centimeters. The second line has one integer , the number of foods on offer. Each of the next lines has two integers, the volume of one serving in cubic centimeters and its calorie count . These lines are given in non-decreasing order of volume.
A test case whose first line is 0 marks the end of the input, and it produces no output.
, , , , and . At most 10 test cases come before the terminating 0.
Output
For each test case, print one line with integers separated by single spaces. The -th integer is the number of servings of the -th food that Gimli should order.
When several orders reach the largest calorie count, print the sequence of quantities that comes first in lexicographic order.