Snow White and the Dwarfs
Time limit2sMemory limit1024 MB
Order the dwarfs so that at some moment every dwarf is asleep at once; output the order or -1.
- Level
Medium6 of 10
- Topics
- Greedy, Sorting, Implementation, Math
- Solved
- No attempts yet
Problem
<<They are not dwarfs, they are a punishment!>>, Snow White thought as she tried yet again to put the dwarfs to sleep. You put one to sleep and another has already woken up! And so it goes all night.
Snow White has dwarfs, and they are all very different. She knows that putting the -th dwarf to sleep takes minutes, after which he sleeps for exactly minutes. Help Snow White find out whether she can get at least a minute of rest while all the dwarfs are asleep, and if so, in what order she must put the dwarfs to sleep to achieve it.
For example, suppose there are only two dwarfs, , , , . If Snow White starts by putting the first dwarf to sleep, she then needs a whole 10 minutes to put the second one to sleep, and during that time the first one wakes up. But if she starts with the second dwarf, she can then put the first one to sleep in time and get a whole 10 minutes of rest.
Input
The first line contains the number (). The second line contains the numbers , and the third line contains the numbers ().
Output
Output numbers: the order in which the dwarfs must be put to sleep. If Snow White cannot rest, output the number .