You’re planning to start a library for the next nnn days, where you will rent books to students. Each day you will serve a single student. The students will come in the morning take a book and return it before evening. Your shelves in the library can hold at most mmm books at a time. To stock the shelves, you will go to the local book market called Nilkhet. At Nilkhet kkk types of books are available numbered from 111 to kkk. Each book has a purchasing price pip_ipi and a return price rir_iri. That means you have to pay pip_ipi amount of money to buy the book but after that, if you want to return it the shop will give you rir_iri money back. It is guaranteed that for all the books pi>rip_i>r_ipi>ri. Each night you go to Nilkhet, return some(zero or more) books that you previously bought and after that buy some(zero or more) books to stock your library. Remember your shelves can only keep mmm books at a time. So before buying books you may need to return some to make empty spaces for the books. Initially, all shelves are empty.
This is a companion discussion topic for the original entry at https://toph.co/p/nilkhet