Submit | All submissions | Best solutions | Back to list |
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
Added by: | Ace |
Date: | 2013-06-24 |
Time limit: | 2s |
Source limit: | 50000B |
Memory limit: | 1536MB |
Cluster: | Cube (Intel G860) |
Languages: | All except: ASM64 |
hide comments
|
||||||||||||
2023-09-12 17:29:29
i ask |
||||||||||||
2023-09-12 07:23:45
who ask |
||||||||||||
2022-06-29 10:18:19
TLE with separate knapsack function but AC when implementing knapsack function in the main function. why? |
||||||||||||
2021-06-19 04:05:25
Java: NZEC may mean you need memory-optimized DP (essentially == MLE). Java is really tight, you will need O(K) memory (not O(nK) or not even O(2K)), plus time optimization too (do check out those geeksforgeeks links). |
||||||||||||
2021-04-27 11:08:33
i got AC one year ago , now i submit the same code , it give runtime error (SIGKILL) ?!!!! |
||||||||||||
2019-08-15 19:57:32
my fst day of learning knapsack and accepted in one go!! |
||||||||||||
2019-07-08 13:38:44
use int instead of long long int |
||||||||||||
2018-02-20 22:51:19
For all of you wondering how to get 0.0s learn branch and bound algorithm very useful and fun very useful link: https://www.slideshare.net/AbhishekSingh1904/0-1-knapsack-using-branch-and-bound?qid=7d155ea2-0377-4e83-b2f0-de118b4409a1&v=&b=&from_search=1 |
||||||||||||
2018-02-12 23:03:31
*Edit: nevermind, i used a different compiler and it was fine. Did you guys need to use iteration to pass this one? I've used recursion with O(n*K) space complexity, and hence at most O(n*K) time complexity. However, I'm getting time-out. I know that iteration would be faster, but' thought this was more about dynamic programming (reducing time complexity) so didn't think it would be needed? Last edit: 2018-02-12 23:25:55 |
||||||||||||
2017-12-24 10:16:35
This should help if you stuck for a long time. See the second program in this link : https://www.geeksforgeeks.org/space-optimized-dp-solution-0-1-knapsack-problem/ |