Pro-Test Voting

Time limit1sMemory limit128 MB

Summary
Given a budget and precincts whose vote gain follows a concave spending curve, allocate dollars to maximize rounded total votes, breaking ties toward lower-numbered precincts.
Level

Medium5 of 10

Topics
Dynamic programming, Greedy
Solved
No attempts yet

Problem

Old Bob Test is running for Mayor of the Hamlet of Kerning. Kerning is divided into several precincts (numbered 0, 1, 2, ...). After extensive polling, Bob knows the current percentage of voters in each precinct who plan to vote for him. He would like to raise these percentages everywhere, but his funds are limited.

Based on past results, the effect of spending in a precinct follows the equation

Fp=Ip+(M10.1+M)ΔF_p = I_p + \left(\frac{M}{10.1 + M}\right)\Delta

where IpI_p is the current percentage of pro-Test voters, Δ\Delta is the maximum possible increase in this percentage, MM is the amount of money spent in the precinct as a non-negative integer number of dollars (multiples of $1), and FpF_p is the resulting expected percentage. Determine how Bob should spend his money to maximize the total number of votes he receives.

Input

The first line of each test case contains two integers mm and nn: the amount of money Bob has to spend (in dollars) and the number of precincts. Both are at most 100100. The next nn lines each have the form N Ip ΔN\ I_p\ \Delta, all positive integers, describing a precinct: NN is the precinct's population (less than 1000010000), and IpI_p and Δ\Delta are as described above. The first of these lines is precinct 0, the next is precinct 1, and so on.

A line containing 0 0 follows the last test case.

To find the number of pro-Test voters in a precinct, first compute FpF_p with the formula above using floating-point arithmetic, then multiply that percentage by the population NN (that is, Fp×N/100F_p \times N / 100) and round to the nearest integer, rounding halves up.

Output

For each test case, output two lines. The first line contains the case number followed by the maximum number of votes Bob can obtain through optimal spending. The second line lists, for every precinct, how much money Bob should spend there, each entry written as precinct:money and separated by a single blank.

Case X: votes
0:money0 1:money1 ...

If more than one way of spending the money yields the maximum number of votes, output the one that spends the most on precinct 0; if several tie on precinct 0, take the one that spends the most on precinct 1, and so on.

Examples1

  1. Example 1

    Input
    100 2
    3000 45 15
    2000 60 10
    100 3
    3000 45 15
    2000 60 10
    4000 20 8
    100 3
    3000 45 15
    2000 60 10
    4000 20 7
    100 3
    3000 45 15
    2000 60 10
    4000 20 6
    100 3
    3000 45 15
    2000 60 10
    4000 20 5
    0 0
    
    Expected output
    Case 1: 3095
    0:64 1:36
    Case 2: 4101
    0:42 1:24 2:34
    Case 3: 4070
    0:45 1:27 2:28
    Case 4: 4040
    0:46 1:27 2:27
    Case 5: 4011
    0:46 1:27 2:27