-
Notifications
You must be signed in to change notification settings - Fork 0
/
Q5-FinalVoyage.cpp
49 lines (43 loc) · 1.01 KB
/
Q5-FinalVoyage.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
// link - https://www.hackerearth.com/practice/algorithms/dynamic-programming/2-dimensional/practice-problems/algorithm/final-voyage-1/description/
#include<bits/stdc++.h>
using namespace std;
int main()
{
int t;
cin>>t;
while(t--)
{
int n;
cin>>n;
int W;
cin>>W;
int weights[n];
int val[n];
for(int i = 0 ; i < n ; i++)
{
cin>>weights[i];
}
for(int i = 0 ; i < n ; i++)
{
cin>>val[i];
}
int dp[n+1][W+1];
for(int i = 0 ; i <=n ; i++)
{
dp[i][0] =0;
}
for(int j = 0 ; j <=W ; j++)
{
dp[0][j] =0;
}
for(int i=1;i<=n;i++)
for(int j=1;j<=W;j++)
{
if(j-weights[i-1]>=0)
dp[i][j] = max(dp[i-1][j],dp[i-1][j-weights[i-1]]+val[i-1]);
else
dp[i][j] = dp[i-1][j];
}
cout<<dp[n][W]<<endl;
}
}