Queensland and Schools
Practice
3.7 (7 votes)
Dynamic programming
Medium
Two dimensional
Problem
47% Success 1647 Attempts 30 Points 1s Time Limit 256MB Memory 1024 KB Max Code

You have been given 2 arrays A and B each of size N, and an integer X. Now, you need to pick a subset of k indices (maybe with repetitions) \((i_1 , i_2 , i_3 ... i_k) \) , such that :

\( 1 \le k \le N \)

\( A[i_1]+A[i_2]+A[i_3]+...+A[i_k] \le X \)

\( B[i_1]+B[i_2]+B[i_3]+...+B[i_k]\) is maximized.

You need to find and print the maximum possible value of \( B[i_1]+B[i_2]+B[i_3]+...+B[i_k]\) such that \( A[i_1]+A[i_2]+A[i_3]+...+A[i_k] \le X \) . Can you do it ?

Note that a single index can be picked multiple number of times to be part of the subset \( (i_1,i_2,i_3...i_k) \)

Input Format :

The first line contains a single integer X

The next line contains a single integer N.

Each of the next N lines contains 2 space separated integers, where the integers on the \(i^{th}\) line denote \(A[i]\) and \(B[i]\).

Output Format :

Print the required answer on a single line.

Constrains:

\( 1 \le X \le 1000 \)

\( 1 \le N, A[i],B[i] \le 1000 \)

Please login to use the editor

You need to be logged in to access the code editor

Loading...

Please wait while we load the editor

Loading...
Results
Custom Input
Run your code to see the output
Submissions
Please login to view your submissions
Similar Problems
Points:30
116 votes
Tags:
ApprovedGeometryMediumOpenTwo dimensional
Points:30
54 votes
Tags:
2D dynamic programmingAlgorithmsDynamic ProgrammingMediumPermutation and combination
Points:30
3 votes
Tags:
AlgorithmsApprovedDynamic ProgrammingImplementationMediumOpenProbabilityStatisticsTwo dimensional