Vitcoin Lottery at Moloco (Easy)
InterviewTime limit2sMemory limit512 MB
Order n tickets with prizes w_i and continuation probabilities p_i to maximize the expected sum of collected prizes, breaking ties by the lexicographically smallest permutation.
- Level
Medium6 of 10
- Topics
- Greedy, Sorting, Math, Probability
- Solved
- No attempts yet
Problem
At the end of every month Moloco runs a simple lottery for extra employee perks.
You receive lottery tickets, and ticket pays a prize of Vitcoins ().
Each ticket is paired with its own coin. The coin of ticket comes up heads with probability and tails with probability (). All coin flips are independent.
At each step you choose one ticket you have not chosen yet and claim its prize immediately. Then you flip the coin paired with that ticket. On heads the lottery continues and you choose another ticket, repeating until you run out of tickets. On tails the lottery ends at once and you keep every Vitcoin you have collected so far.
You know all values and . Find an order of picking the tickets that maximizes the expected number of Vitcoins you win.
Input
The first line contains an integer ().
Each of the next lines contains two integers and (, ). The probability is defined as .
Output
Print, on a single line, the permutation that describes the order in which you pick the tickets. The permutation has length , consists of the integers from to , and its entries are separated by single spaces.
If several permutations maximize the expected number of Vitcoins, print the one that comes first lexicographically, so the smaller index must come earlier.
Note
Picking the tickets in the order wins the following expected number of Vitcoins.
In the first example the order has expectation . The order maximizes the expectation at .
In the third example the orders and have the same expectation, and comes first lexicographically.