Maximize the Acceleration

Interview

Time limit1sMemory limit128 MB

Summary
Given N parts that each add force and mass, choose the subset maximizing total force divided by total mass, breaking ties by smaller mass.
Level

Medium4 of 10

Topics
Brute force, Bit manipulation, Greedy, Math
Solved
No attempts yet

Problem

Bessie is tuning her race car for an upcoming Grand Prix. Right now the car has mass MM and pushes itself forward with force FF. A performance shop sells NN upgrade parts numbered 11 through NN. The shop stocks at most one of each part, so Bessie may install any subset of the parts (possibly none).

Installing part ii adds force FiF_i and mass MiM_i to the car. By Newton's second law F=M⋅AF = M \cdot A, the car's acceleration equals its total force divided by its total mass. If Bessie installs a set SS of parts, the acceleration becomes

A=F+∑i∈SFiM+∑i∈SMi.A = \frac{F + \sum_{i \in S} F_i}{M + \sum_{i \in S} M_i}.

For example, adding a single part with force 150150 and mass 99 to a car with F=1500F = 1500 and M=100M = 100 raises the acceleration from 1515 to (1500+150)/(100+9)≈15.14(1500 + 150) / (100 + 9) \approx 15.14.

Bessie wants to pick the set of parts that maximizes the acceleration. If several different sets reach the same maximum acceleration, she prefers the set with the smallest total mass. Under these two rules the best set of parts is unique.

Constraints

  • 1≤M≤10001 \le M \le 1000
  • 1≤F≤1,000,0001 \le F \le 1{,}000{,}000
  • 1≤N≤201 \le N \le 20
  • 1≤Fi≤1,000,0001 \le F_i \le 1{,}000{,}000
  • 1≤Mi≤10001 \le M_i \le 1000

Input

The first line contains three space-separated integers FF, MM, and NN.

Each of the next NN lines contains two space-separated integers FiF_i and MiM_i: the force and mass added by part ii.

Output

Print the indices of the parts Bessie should install, one per line, in increasing order. If she should install no parts at all, print NONE.

Examples3

  1. Example 1

    Input
    1500 100 4
    250 25
    150 9
    120 5
    200 8
    
    Expected output
    2
    3
    4
    
  2. Example 2

    Input
    10 1 1
    21 2
    
    Expected output
    1
    
  3. Example 3

    Input
    10 1 1
    20 2
    
    Expected output
    NONE