Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include<bits/stdc++.h>
- using namespace std;
- int func(int i,int w,int *cost,int *weight,int m,int n);
- int profit1,profit2;
- int main()
- {
- int a,t,n,p,m;
- cin>>t;
- while(t--)
- {
- cin>>n;
- int cost[n],weight[n];
- for(a=0;a<n;a++)
- {
- cin>>cost[a]>>weight[a];
- }
- cin>>p;
- while(p--)
- {
- cin>>m;
- int k=func(0,0,cost,weight,m,n);
- cout<<k<<endl;
- }
- }
- return 0;
- }
- int func(int i,int w,int *cost,int *weight,int m,int n)
- {
- //cout<<profit1<<" "<<profit2<<endl;;
- if(i=n-1) return 0;
- else
- {
- if(w+weight[i]<=m) profit1=cost[i]+func(i+1,w+weight[i],cost,weight,m,n);
- else profit1=0;
- profit2=func(i+1,w,cost,weight,m,n);
- return max(profit1,profit2);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement