I Am Knowledge
InterviewTime limit2sMemory limit1024 MB
Given books with a start cost a_i and a finish reward b_i, decide whether some reading order keeps joy from going negative.
Problem
A wizard who lives in a mansion often visits the underground library. One day, the wizard decides to read all books in the library. Only one book can be read at a time, but the wizard can choose the order.
Starting a book is hard, but after finishing it the wizard gains the joy of acquired knowledge. Joy is an integer, and the wizard starts with joy . Until all books are read, the wizard repeats the following process.
- Choose any unread book.
- If the chosen book has number , spend joy to start reading it. If joy becomes less than , the wizard falls asleep without finishing the book and cannot read any more books.
- If joy does not become less than and the book is finished, gain joy.
Tell whether the wizard can finish all books without falling asleep.
Input
The first line contains the number of books in the library ().
From the second line, each of the next lines contains two integers separated by a space. ()
This means that reading book costs joy, and finishing it gives joy.
Output
Print 1 if the wizard can finish all books in the library, and 0 otherwise.