knapsack

#include<bits/stdc++.h>
using namespace std;
vector<pair<int,int> >a;
//dp table is full of zeros
int n,s,dp[1002][1002];
void ini(){
    for(int i=0;i<1002;i++)
        for(int j=0;j<1002;j++)
            dp[i][j]=-1;
}
int f(int x,int b){
	//base solution
	if(x>=n or b<=0)return 0;
	//if we calculate this before, we just return the answer (value diferente of 0)
	if(dp[x][b]!=-1)return dp[x][b];
	//calculate de answer for x (position) and b(empty space in knapsack)
	//we get max between take it or not and element, this gonna calculate all the
	//posible combinations, with dp we won't calculate what is already calculated.
	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);
}
int main(){
	//fast scan and print
	ios_base::sync_with_stdio(0);cin.tie(0);
	//we obtain quantity of elements and size of knapsack
	cin>>n>>s;
	a.resize(n);
	//we get value of elements
	for(int i=0;i<n;i++)
		cin>>a[i].first;
	//we get size of elements
	for(int i=0;i<n;i++)
		cin>>a[i].second;
	//initialize dp table
	ini();
	//print answer
	cout<<f(0,s);
	return 0;
}

Are there any code examples left?
Create a Free Account
Unlock the power of data and AI by diving into Python, ChatGPT, SQL, Power BI, and beyond.
Sign up
Develop soft skills on BrainApps
Complete the IQ Test
Relative searches
Knapsack Implementation algorithm knapsack problem knapsack python code Knapsack js knapsack algorithm use knapsack python what is a knapsack problem - Knapsack Problem Knapsack problem easy knapsack algorithm cryptography knapsack algorithm example Knapsack {0,1) question how to use knapsack minecraft knaosack algorithm )-1 knapsack how to do knapsack problem code for knapsack problem knapsack in cryptography knapsack problem spoj knapsack problem code knapsack example Knapsack problem time com' Explain knapsack problem. knapsack algorithm implementation knapsack optimization knapsack definition Define knapsack problem knapsack problem solver dynamic knapsack knapsack to blockchain knapsack problem using lcbb method code of binary knapscak problem optimality of knapsack problem knapsack problem example with solution knapsack java which stragy is better for knapsack fractional knapsack problem what is m in knapsack problem knapsack problem dynamic programming complexity knapsack problem a* one cancel the oi knapsack Optimisation problem by using an algorithm that solves the oven knapsack decision backpack algorithm dynamic programming example of knapsack problem knapsack problem without matrix knapsack problem dynamic programming algorithm knapsack problem O(w knapsack problem 'simple implementation ' what type of algorithm is knapsack knapsack problem 'simple' knapsack problem knapsack problem dynamic programming solution explained knapsack problem dp knapsack 0-1 for (100,50,20,10,7,3) knapsack problem explained knapsack that you must fill optimally A greedy algorithm finds the optimal solution to the integer knapsack problem methods to solve knapsack problems knapsack problem complexity Knapsack problem decription what is the knapsack problem knapsack problem time complexity The complexity of the knapsack problem is dynamic knapsack also known as solution to knapsack problem the knapsack problem Which technique cannot be used to solve Knapsack problem 0/1 knapsack problem dynamic knapsack complexity knapsack problem dynamic programming cons of knapsack dynamix programming givent the value and weight of each item, find the maximum number of items that could be put in kanpsack of weight W 01 knapsack code Max Knapsack problem why do we need knapsack algorithm Knapsack Problem (Dynamic Programming) backpack problem dynamic programming backpack algorithm 0 1 knapsack problem binary knapsack problem is knapsack problem solution sack bag PROBLEM dynamic programming knapsack knapsack dynamic programming bounded knapsack problem knapsack code knapsack problem list knapsack problem example knapsack problem is an example of 0-1 nacksack problem Knapsack problem. knapsack weight knapsack solution coding 0/1 Knapsack what is the optimal solution for knapsack problem knapsack solution knapsack problem solution 0-1 knapsack problem l knapsack algorithm knapsack problem algorithm what is knapsack problem knapsack memoization diagram 0-1 knapsack problem knsapsack knapsack algorithm in java knapsack problem knapsack
Made with love
This website uses cookies to make IQCode work for you. By using this site, you agree to our cookie policy

Welcome Back!

Sign up to unlock all of IQCode features:
  • Test your skills and track progress
  • Engage in comprehensive interactive courses
  • Commit to daily skill-enhancing challenges
  • Solve practical, real-world issues
  • Share your insights and learnings
Create an account
Sign in
Recover lost password
Or log in with

Create a Free Account

Sign up to unlock all of IQCode features:
  • Test your skills and track progress
  • Engage in comprehensive interactive courses
  • Commit to daily skill-enhancing challenges
  • Solve practical, real-world issues
  • Share your insights and learnings
Create an account
Sign up
Or sign up with
By signing up, you agree to the Terms and Conditions and Privacy Policy. You also agree to receive product-related marketing emails from IQCode, which you can unsubscribe from at any time.
Creating a new code example
Code snippet title
Source