Lottery for Vitcoins at Moloco (Hard)
Time limit2sMemory limit512 MB
Order the tickets to maximize the expected sum of prizes, where ticket i stops the lottery with probability 1 - p_i, and break ties by the lexicographically smallest permutation.
- Level
Medium7 of 10
- Topics
- Greedy, Sorting, Math, Probability
- Solved
- No attempts yet
Problem
At the end of each month, Moloco employees take part in a simple lottery for extra employee perks.
As a valuable employee of Moloco, you receive lottery tickets. Ticket pays a prize of Vitcoins.
Each ticket is also paired with a coin. That coin flips heads with probability and tails with probability , where . Every coin flip is independent.
At each step you choose a ticket among those 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 pick another ticket, and this repeats until you run out of tickets. On tails the lottery ends immediately, and you keep all the Vitcoins you have collected so far.
You know every and every . Find an order of picking the tickets that maximizes the expected number of Vitcoins you win.
Input
The first line contains one integer ().
Each of the next lines contains two integers and (, ). The probability is .
Output
Print, on a single line and separated by spaces, a permutation of length whose elements are the integers from to . It describes the order in which you pick the lottery tickets.
If several permutations maximize the expected number of Vitcoins, print the one that comes first lexicographically.
Note
The expected number of Vitcoins for the order is .
In the first example the order yields , and the maximum is , reached by the order . In the third example the orders and yield the same expectation, and comes first lexicographically.