Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added a folder for DP problems code and added knapsack #123

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
38 changes: 38 additions & 0 deletions DP/knap_sack.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
#include<bits/stdc++.h>
using namespace std;
#define ll long long int
#define mod 1000000007
ll T[1000][1000];
void knapsack_generate(ll w[] ,ll n,ll sum, ll val[]){
for(ll i=1;i<=n;i++){
for(ll j=1;j<=sum;j++){
if(j<w[i]){
T[i][j] = T[i-1][j];
}else{
T[i][j] = max(val[i-1] + T[i-1][j-w[i-1]],T[i-1][j]);
}
}
}
}
int main()
{
#ifndef ONLINE_JUDGE
freopen("input.txt", "r", stdin);
freopen("output.txt", "w", stdout);
#endif
ios_base::sync_with_stdio(false);
cin.tie(NULL);
ll t,i,j,l,m,n,q;
cin>>n;
ll a[n+11];
for(i=0;i<n;i++){
cin>>a[i]>>w[i];
}
knapsack_generate(a,n,maxi_sum);
cin>>q;
while(q--){
cin>>l;
cout<<T[n][l]<<"\n";
}
return 0;
}