Need For Speed

Interview

Time limit1sMemory limit128 MB

Summary
Given a car's base force and mass plus N parts that each add force and mass, choose the subset maximizing total force over total mass, breaking ties by smaller total mass.
Level

Medium7 of 10

Topics
Greedy, Sorting, Implementation, Brute force
Solved
No attempts yet

Problem

Bessie is tuning her race car for the upcoming Grand Prix and wants to buy extra parts to make it faster.

Right now the car has mass MM (1≤M≤10001 \le M \le 1000) and can push itself forward with force FF (1≤F≤1,000,0001 \le F \le 1{,}000{,}000). By Newton's second law F=MAF = MA, so its acceleration is A=F/MA = F / M.

The parts store sells NN (1≤N≤10,0001 \le N \le 10{,}000) parts numbered 11 through NN, with at most one of each in stock. Buying part ii adds force FiF_i (1≤Fi≤1,000,0001 \le F_i \le 1{,}000{,}000) and mass MiM_i (1≤Mi≤10001 \le M_i \le 1000). Bessie may buy any subset of the parts, including none of them.

If Bessie installs the parts in a set SS, the car's total force becomes F+∑i∈SFiF + \sum_{i \in S} F_i and its total mass becomes M+∑i∈SMiM + \sum_{i \in S} M_i, giving acceleration

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

Choose the set of parts that maximizes this acceleration. If several sets achieve the same maximum acceleration, choose the one with the smallest total mass. The optimal set is guaranteed to be unique.

For example, installing a single part with force ff and mass mm changes the acceleration to (F+f)/(M+m)(F + f) / (M + m).

Input

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

Each of the next NN lines contains two space-separated integers: line i+1i+1 holds FiF_i and MiM_i, the force and mass added by part ii.

Output

Print the 1-based indices of the parts Bessie should install, in increasing order, one per line. If installing no part is optimal, 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
    1 1000 1
    1000000 1
    
    Expected output
    1
    
  3. Example 3

    Input
    1000000 1 1
    1 1000
    
    Expected output
    NONE