knapsack

Solutions on MaxInterview for knapsack by the best coders in the world

showing results for - "knapsack"
Usama
16 Jan 2016
1#include<bits/stdc++.h>
2using namespace std;
3vector<pair<int,int> >a;
4//dp table is full of zeros
5int n,s,dp[1002][1002];
6void ini(){
7    for(int i=0;i<1002;i++)
8        for(int j=0;j<1002;j++)
9            dp[i][j]=-1;
10}
11int f(int x,int b){
12	//base solution
13	if(x>=n or b<=0)return 0;
14	//if we calculate this before, we just return the answer (value diferente of 0)
15	if(dp[x][b]!=-1)return dp[x][b];
16	//calculate de answer for x (position) and b(empty space in knapsack)
17	//we get max between take it or not and element, this gonna calculate all the
18	//posible combinations, with dp we won't calculate what is already calculated.
19	return dp[x][b]=max(f(x+1,b),b-a[x].second>=0?f(x+1,b-a[x].second)+a[x].first:INT_MIN);
20}
21int main(){
22	//fast scan and print
23	ios_base::sync_with_stdio(0);cin.tie(0);
24	//we obtain quantity of elements and size of knapsack
25	cin>>n>>s;
26	a.resize(n);
27	//we get value of elements
28	for(int i=0;i<n;i++)
29		cin>>a[i].first;
30	//we get size of elements
31	for(int i=0;i<n;i++)
32		cin>>a[i].second;
33	//initialize dp table
34	ini();
35	//print answer
36	cout<<f(0,s);
37	return 0;
38}
39
queries leading to this page
knapsack python codeknapsack how to use knapsack minecraftknapsack problem 27simple implementation 27knapsack problem time com 27knapsack optimizationknapsack problem dynamic programmingknapsack problem 28dynamic programming 29what is the knapsack problemwhat is the use of knapsack algorithmknapsack algorithm cryptographybinary knapsack problemwhich stragy is better for knapsackknapsack problem example with solutionknapsack problemknapsack that you must fill optimallywhat is the optimal solution for knapsack problemknapsack codeknapsack 0 1 for 28100 2c50 2c20 2c10 2c7 2c3 29knapsack problem time complexityknapsack problem explainedexplain knapsack problem 3f01 knapsack codemax knapsack problemknapsack problem algorithmknapsack problem solverknsapsackknapsack pythonknapsack solutionknapsack problem a 2aknapsack treeprogram to implement knapsack problemknapsack weight knapsack problem spojone cancel the oi knapsack optimisation problem by using an algorithm that solves the oven knapsack decisionknaosack algorithm0 1 nacksack problem0 1 knapsack problemknapsack solution codingknapsack problem o 28wknapsack problem dynamic programming complexityknapsack algorithmknapsack algorithm usehow to do knapsack problemknapsack problem solutionbackpack algorithm dynamic programmingwhat is m in knapsack problem knapsack problem0 2f1 knapsackis knapsack problem solutionbackpack algorithm0 2f1 knapsack problem knapsack to blockchaindynamic knapsack complexityknapsack implementationwhich technique cannot be used to solve knapsack problemexplain knapsack problem sack bag problemknapsack problem runtime knapsack problemknapsack problem dynamic programming algorithmknapsack algorithm implementationbounded knapsack problemknapsack dynamic programmingknapsack problem codeknapsack problem dynamic knapsack also known asdynamic knapsackwhat is a knapsack problemcons of knapsack dynamix programmingdynamic programming knapsackknapsack problem examplebackpack problem dynamic programmingsolution to knapsack problemknapsack in cryptographycode of binary knapscak problemoptimality of knapsack problemknapsack problem examples 29 1 knapsacka greedy algorithm finds the optimal solution to the integer knapsack problemknapsack javawhat type of algorithm is knapsackknapsack algorithm in javaknapsack bagknapsack problem decriptiondefine knapsack problemwhat is knapsack problemfractional knapsack problemknapsack problem 27simple 270 1 knapsack problemknapsack problem is an example ofknapsack data structureknapsack exampleknapsack using stacckmethods to solve knapsack problemscode for knapsack problemknapsack problem listalgorithm knapsack problemgivent the value and weight of each item 2c find the maximum number of items that could be put in kanpsack of weight w0 1 knapsack problem lknapsack jsthe knapsack problemwhy do we need knapsack algorithmknapsack memoization diagramknapsack definitionknapsack problem using lcbb methodknapsack problem complexityknapsack problem dynamic programming solution explainedexample of knapsack problemknapsack problem dpknapsack problem isknapsack 7b0 2c1 29 questionknapsack algorithm exampleknapsack problem without matrixthe complexity of the knapsack problem isknapsack problem easy knapsack