Passing marks

4.2

10 votes
2D dynamic programming, Dynamic Programming, Algorithms
Problem

There are N students in the class where each of them has two parameters, marks and the bound. There are two arrays M and B denoting the marks and the bound of N students in the class. Initially, the cutoff is zero for passing. Now, each of the students will be evaluated on the basis of his marks.
If the marks obtained by the student are greater than the cutoff, he or she will pass and the cutoff will be incremented by the bound of that student.

Your task is to find the maximum number of students that can pass if the arrangement of students is done properly.

Input format

  • The first line contains an integer denoting the number of test cases T
  • The first line of each test case contains an integer N denoting the number of students in the class.
  • Next N lines of each test case contain two space-separated integers marks (Mi) and bound (Bi) of the ith student.

Output format

Print T lines. For each test case:

  • Print a single line indicating the maximum number of students that can pass.

Constraints

1T200

1N2000

1Mi,Bi109i[1,N]

The sum of N over all test cases does not exceed 2000.

Sample Input
1
5
1 3
2 1
4 7
3 5
7 1
Sample Output
3
Time Limit: 1
Memory Limit: 256
Source Limit:
Explanation

Order should be [2,1,3,4,5], initially cutoff is zero.

Second student has more marks than cutoff, he will pass and cutoff will now be one as bound for second student is 1.

First student has marks equal to cutoff, he will also pass and cutoff will be 1+3=4.

Third student has marks equal to cutoff , he will also pass and cutoff will be 4+7=11.

Now fourth and fifth students will fail because both of them have marks less than 11.

Editor Image

?