Most Ordered Way
InterviewTime limit0.3sMemory limit1024 MB
Order N tasks with times T and deadlines D so each finishes on time, choosing the lexicographically smallest order, or print * if impossible.
Problem
Sofia was given assignments from school, numbered from 1 to . Each assignment has two values, and . is the number of minutes needed to finish it, and is its deadline, meaning it must be completed no later than minutes from now.
Sofia can do the assignments in any order, one at a time. Once she starts an assignment, she keeps working on it until it is done. She spends all her time on the assignments. She can start right away, and after finishing one assignment she starts the next immediately, without breaks.
Sofia is a perfectionist and wants to finish every assignment. She first planned to do them in the given order, but she soon realized that this could make some of them miss their deadlines. If several orders finish every assignment on time, she wants the most ordered one. Write a program that tells her how to organize her work.
Time limit: 300 ms. Memory limit: 1024 MB.
Input
The first line contains an integer (). Each of the next lines contains two integers and ().
Output
Output a single line with a permutation of the integers from 1 to describing an order in which the assignments can be done so that each is completed on time. If no such order exists, output * (asterisk). If more than one permutation works, output the lexicographically smallest one.