PARTY - Party Schedule


You just received another bill which you cannot pay because you lack the money. Unfortunately, this is not the first time to happen, and now you decide to investigate the cause of your constant monetary shortness. The reason is quite obvious: the lion's share of your money routinely disappears at the entrance of party localities.

You make up your mind to solve the problem where it arises, namely at the parties themselves. You introduce a limit for your party budget and try to have the most possible fun with regard to this limit.

You inquire beforehand about the entrance fee to each party and estimate how much fun you might have there. The list is readily compiled, but how do you actually pick the parties that give you the most fun and do not exceed your budget?

Write a program which finds this optimal set of parties that offer the most fun. Keep in mind that your budget need not necessarily be reached exactly. Achieve the highest possible fun level, and do not spend more money than is absolutely necessary.

Input

The first line of the input specifies your party budget and the number n of parties.

The following n lines contain two numbers each. The first number indicates the entrance fee of each party. Parties cost between 5 and 25 francs. The second number indicates the amount of fun of each party, given as an integer number ranging from 0 to 10.

The budget will not exceed 500 and there will be at most 100 parties. All numbers are separated by a single space.

There are many test cases. Input ends with 0 0.

Output

For each test case your program must output the sum of the entrance fees and the sum of all fun values of an optimal solution. Both numbers must be separated by a single space.

Example

Sample input:
50 10
12 3
15 8
16 9
16 6
10 2
21 9
18 4
12 4
17 8
18 9 

50 10
13 8
19 10
16 8
12 9
10 2
12 8
13 5
15 5
11 7
16 2

0 0

Sample output:
49 26
48 32

hide comments
ROHIT KUMAR: 2013-08-06 13:37:22

"AC" in 1 go :) :) :)
one step closer to save the PRINCESS(#codechef) :P
calculation of entrance fee makes it a li'l diff frm a "0-1 Knapsack Problem" :)

Last edit: 2013-08-06 13:37:39
Geanyous: 2013-07-10 06:53:06

easy dp

archana maurya: 2013-07-07 21:57:31

I am getting wrong answer for this :) Please help.

Govind Lahoti: 2013-06-20 18:39:17

superb problem

Erti-Chris Eelmaa: 2013-06-11 09:29:53

Don't forget to read BLANK line between each test case. Had many-many WA's because of this.

manu: 2013-05-19 05:08:13

the code runs properly on codeblocks giving desired results.. but it fails fo r for first test case in spoj.. why so???any one.

jinkies: 2013-04-10 21:09:38

keep calm and codon.

Jagatheesvaran Palanisamy: 2013-04-02 13:48:14

DP

Juɑƞ Chɑpɑrro: 2013-11-07 15:22:12

Input:
5 4
3 2
1 1
Output:
4 3

Juɑƞ Chɑpɑrro: 2013-03-31 05:41:14

@ankurgupta24
Output:
36 23


Added by:Patryk Pomykalski
Date:2004-07-01
Time limit:3s
Source limit:50000B
Memory limit:1536MB
Cluster: Cube (Intel G860)
Languages:All
Resource:Swiss Olympiad in Informatics 2004