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 <= 10^7
Wi <= 10^7


hide comments
vilay: 2013-07-05 01:01:04

@shikhar : why tle.? my algo run in O(nk), got accepted before and now it shows tle..
ok -> got accepted. good one!.

Last edit: 2013-07-03 06:04:13
[Lakshman]: 2013-07-05 01:01:04

@Shikhar Sharad Thanks got AC now

Shikhar Sharad: 2013-07-05 01:01:04

@shashank nagamalla...Python takes a lot of time...the logic which took me 1.4s in C/C++ did get TLE in Python even when i had increased the limit to 300s.

Last edit: 2013-06-30 06:30:09
shashank nagamalla: 2013-07-05 01:01:04

@shikhar sharad I think I am using optimized algo but I am getting TLE can u check 9565710 and tell me why.

Last edit: 2013-06-28 05:20:16
Shikhar Sharad: 2013-07-05 01:01:04

@Lakshman...you have declared the global knapsack matrix of order 1000x1000....but max K is 2000000...your code works only for K<1000.

Last edit: 2013-06-26 14:06:36
[Lakshman]: 2013-07-05 01:01:04

@Shikhar Sharad can you please tell me why I am getting WA.
Thanks.

technophyle: 2013-07-05 01:01:04

Easy :D

raunakrocks: 2013-07-05 01:01:04

gud1 :)


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