LKS - Large Knapsack


The knapsack problem or rucksack problem is a problem in combinatorial optimization: Given a set of items, each with a weight and a value, determine the number of each item to include in a collection so that the total weight is less than or equal to a given limit and the total value is as large as possible. It derives its name from the problem faced by someone who is constrained by a fixed-size knapsack and must fill it with the most valuable items.

Just implement 0/1 Knapsack.

Input

First line contains two integers K and N, where K in the maximum knapsack size and N is the number of items. N lines follow where ith line describes ith item in the form vi and wi where vi is the value and wi is the weight of ith item.

Output

Output a single number - maximum value of knapsack. (All operations and the answer are guaranteed to fit in signed 32-bit integer.)

Time limit changed to 2s on 02.07.11.

Example

Input:
10 3
7 3
8 8
4 6

Output:
11

Constraints

K ≤ 2000000
N ≤ 500
Vi ≤ 107
Wi ≤ 107


hide comments
rohit9934: 2017-07-05 17:59:10

use the optimized knapsack
int will work fine.

vivace: 2017-06-15 11:47:42

How are people getting 0.00 seconds on this one . My space optimised 0-1 knapsack dp of order O(nW) took 1 second even in C.

meenaranga: 2017-06-10 19:15:28

unbelievable turned long long to int got ac??

cichipi_: 2017-06-10 18:45:18

normal knapsack...N*K time complexity...i think input file doesn't contain k = 2000000 and n=500....cz if so then n*k = 1e9...-> tle

snehalkr: 2017-06-05 13:06:49

LONG LONG INT GAVE TLE INT 1.54 Sec

prasoonbatham: 2017-05-24 17:47:46

Space optimized java solution gives tle. Same code accepted in cpp.

pcgsf22: 2017-05-18 09:31:17

I thought it need some complicated optimization, but it turns out not.

shahzada: 2017-05-01 19:45:43

need some pretty good space optimisations.

swapnesh1997: 2017-04-30 11:40:17

easy one!!!

bansal1232: 2017-01-30 15:44:54

Mine complexity is N*K ( 10^9 )

Still wounder how it can be accepted here?


Added by:Ace
Date:2013-06-24
Time limit:2s
Source limit:50000B
Memory limit:1536MB
Cluster: Cube (Intel G860)
Languages:All except: ASM64