Course Registration
Time limit1sMemory limit1024 MB
Given N courses with seat capacities a_i and interested-student counts b_i, find how many courses Dalgu can take under the best priority order against the worst registration order.
Problem
Dalgu looked over the courses offered this semester and narrowed the list to courses he wants to take. Ambitious as he is, Dalgu wants to take as many of them as possible this semester, so he surveyed each of the courses for its enrollment capacity and the number of students who want to take it. Course has a capacity of seats, and students, including Dalgu, want to take it. The school has students in total, including Dalgu, and each student can register for at most one course per second. If several students try to register for the same course at the same moment, Dalgu can always register for that course first. Registration is first come, first served, and a course that is full cannot accept more registrations. Students other than those Dalgu surveyed do not register for that course. Before registration begins, Dalgu assigns each of the courses a distinct priority and plans to register in priority order. If a higher-priority course is already full, he skips it and tries the next priority. He cannot change the priorities once registration has started. The order in which other students register changes how many courses Dalgu can get, sometimes by a lot. So Dalgu wants to choose the priorities that let him take the most courses even in the worst possible order of the other students' registrations. Given the best priority assignment for Dalgu, find the number of courses he can take in the worst case.
Input
The first line contains two integers and , separated by a space, where is the number of courses Dalgu wants to take and is the total number of students, including Dalgu. Each of the next lines contains two integers and , separated by a space, where is the capacity of the -th course and is the number of students, including Dalgu, who want to take it.
Output
Print the number of courses Dalgu can take in the worst case, when the priorities are assigned in the way that maximizes this number.