KNAPSACK - The Knapsack Problem

no tags 

The famous knapsack problem. You are packing for a vacation on the sea side and you are going to carry only one bag with capacity S (1 <= S <= 2000). You also have N (1<= N <= 2000) items that you might want to take with you to the sea side. Unfortunately you can not fit all of them in the knapsack so you will have to choose. For each item you are given its size and its value. You want to maximize the total value of all the items you are going to bring. What is this maximum total value?


On the first line you are given S and N. N lines follow with two integers on each line describing one of your items. The first number is the size of the item and the next is the value of the item.


You should output a single integer on one like - the total maximum value from the best choice of items for your trip.


4 5
1 8
2 4
3 0
2 5
2 3


hide comments
cardinalx: 2018-12-06 13:22:01

dp in soal ke jozve badihijate ;)

ameernsr: 2018-12-06 12:47:24

ishala hame good bashan

sam_np: 2018-12-06 12:46:58

slm hji

ameernsr: 2018-12-06 12:46:22

salam guys

areubreathing: 2018-11-01 00:07:55

why do rec solution gets tle?

richardks3647: 2018-06-28 03:13:24

AC in one go

aryan12: 2018-06-16 19:01:17

AC in 1 go using DP. My first DP solution in SPOJ.
A nice question for a new comer in DP.

na0013: 2018-06-16 16:29:01

AC in one go!!
Challenge: Solve this using 1 D dp

k3rnelpan1c: 2018-04-27 23:07:29

I have a dream that one day correct python solution won't get a TLE !

goodwilllion: 2018-02-26 09:27:26


Added by:Nikola P Borisov
Time limit:1s
Source limit:50000B
Memory limit:1536MB
Cluster: Cube (Intel G860)
Languages:All except: ERL JS-RHINO NODEJS PERL6 VB.NET