Raffle Tickets
InterviewTime limit1sMemory limit128 MB
Buy tickets from baskets with known winning and losing counts to guarantee at least g wins with the fewest tickets.
- Level
Medium5 of 10
- Topics
- Dynamic programming
- Solved
- No attempts yet
Problem
At a school event there are baskets, and each basket is filled with a number of raffle tickets. Some tickets are winning: drawing a winning ticket earns the right to skip one class hour without an excuse.
Kozik has decided that he wants to skip hours this school year, so he needs to end up holding at least winning tickets. He will buy tickets from the baskets, but he cannot choose which individual tickets he receives, so he wants to be certain: no matter which tickets happen to come out, the tickets he buys must include at least winning ones. Because money is tight, he wants to buy as few tickets as possible.
Kozik knows exactly how many winning and how many losing tickets each basket holds. When he takes tickets from a basket, assume the worst case: every losing ticket in that basket comes out before any winning one. So to be guaranteed winning tickets from a basket that holds winning and losing tickets (with ), he must take tickets from it.
Find the smallest total number of tickets Kozik must buy so that he is guaranteed at least winning tickets.
Input
The first line contains two integers and (, ): the number of baskets and the number of hours Kozik wants to skip.
Each of the next lines describes one basket with two integers and (, ): the number of winning and losing tickets in the -th basket.
Output
Print a single integer: the minimum number of tickets Kozik must buy to be guaranteed at least winning tickets. If this is impossible (the baskets together hold fewer than winning tickets), print the word NIE instead.