Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include<bits/stdc++.h>
- using namespace std;
- int main(){
- int t;
- cin>>t;
- while(t--){
- int n;
- cin>>n;
- int W;
- cin>>W;
- vector<int> A(n);
- for(int i=0; i<n; i++){
- cin>>A[i];
- }
- vector<int> wt(n);
- for(int i=0; i<n; i++){
- cin>>wt[i];
- }
- vector<vector<int> >dp(n, vector<int>(W+1));//stores the final values(not the weights)
- for(int i=0; i<n; i++){
- dp[i][0]=0;//if w=0; the maximum value we can make is zero
- }
- //in the answer first entire first row is made zero(why?) and i=0; i<=n;
- for(int i=0; i<n; i++){
- for(int w=1; w<=W; w++){
- if(i==0){
- (wt[i]>w) ? dp[i][w]=0 : dp[i][w]=A[i];
- }
- else if(wt[i]>w){
- dp[i][w]=dp[i-1][w];
- }
- else if(wt[i]<=W){
- dp[i][w]=max(dp[i-1][w], A[i] + dp[i-1][w-wt[i]]);
- }
- }
- }
- cout<<dp[n-1][W]<<endl;;
- }
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement