Queensland and Schools

3.7

7 votes
Problem

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) (i1,i2,i3...ik) , such that :

1kN

A[i1]+A[i2]+A[i3]+...+A[ik]X

B[i1]+B[i2]+B[i3]+...+B[ik] is maximized.

You need to find and print the maximum possible value of B[i1]+B[i2]+B[i3]+...+B[ik] such that A[i1]+A[i2]+A[i3]+...+A[ik]X . Can you do it ?

Note that a single index can be picked multiple number of times to be part of the subset (i1,i2,i3...ik)

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 ith line denote A[i] and B[i].

Output Format :

Print the required answer on a single line.

Constrains:

1X1000

1N,A[i],B[i]1000

Time Limit: 1
Memory Limit: 256
Source Limit:
Explanation

In the first sample, we can pick index 1 80 times. In such a manner, we find that A[1]×80X , and the value of the answer is maximized i.e. 80

Editor Image

?