-
Notifications
You must be signed in to change notification settings - Fork 19
Expand file tree
/
Copy pathFinal_voyage.cpp
More file actions
181 lines (174 loc) · 3.16 KB
/
Final_voyage.cpp
File metadata and controls
181 lines (174 loc) · 3.16 KB
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
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
/*
"RMS Lusitania" was one of the world biggest ship of her time.On the outbreak of the First World War in 1914, she was commandeered by the Admiralty as an armed merchant cruiser. When Lusitania left New York for Liverpool on what would be her final voyage on 1 May 1915, submarine warfare was intensifying in the Atlantic. Germany had declared the seas around the United Kingdom to be a war-zone..On the afternoon of May 7, Lusitania was bombarded by a German U-Boat,few miles off the southern coast of Ireland and inside the declared “zone of war”.Lusitania was officially carrying among her cargo rifle/machine-gun ammunition, shrapnel artillery shells without powder charges and artillery fuses.Captain "Dow" of Lusitania sensed the danger before and ordered his men to save the ammunition materials as much as they can.
They had few small boats and the overall load those small boats could carry was "
W
W".Captain "Dow" knew the value of all the "
N
N" ammunition items.So he told his men the weight "
W
i
Wi" and value "
V
i
Vi" of each ammunition item.As it is too difficult to save all the ammunition,Captain then asked his men to smarty choose ammunition items such that the total weight of the ammunition is less than or equal to maximum load those small ships can afford and the corresponding total value of ammunitions saved is maximum.
INPUT:
the first line contain number of testcases "
T
T"."
T
T" testcases follows then.For each testcase,the first line contains the number of ammunition items "
N
N",the second line contains the maximum load "
W
W" those small ships can afford.The third line contains the individual weight of each ammunition item.Then the next line contains the value "
V
V" of those "
N
N" ammunition items.
OUTPUT:
for each testcase output the maximum value of the ammunitions that can be saved.
Constraint:
1
≤
T
≤
100
1≤T≤100
0
≤
N
≤
1000
0≤N≤1000
1
≤
m
a
x
i
m
u
m
l
o
a
d
≤
1000
1≤maximumload≤1000
1
≤
w
e
i
g
h
t
o
f
i
n
d
i
v
i
d
u
a
l
i
t
e
m
s
≤
1000
1≤weightofindividualitems≤1000
1
≤
v
a
l
u
e
o
f
i
n
d
i
v
i
d
u
a
l
i
t
e
m
s
≤
1000
1≤valueofindividualitems≤1000
SAMPLE INPUT
2
3
3
1 2 3
2 4 8
4
8
2 4 5 7
4 9 7 5
SAMPLE OUTPUT
8
13
*/
#include <bits/stdc++.h>
using namespace std;
int main()
{
int dp[1001][1001];
int tc;
int n,w,i,v;
vector<int> wt;
vector<int> val;
cin>>tc;
while(tc--)
{
cin>>n>>w;
wt.clear();
val.clear();
for(int a = 0; a <n; a++)
{
cin>>i;
wt.push_back(i);
}
for(int a = 0; a <n;a++)
{
cin>>v;
val.push_back(v);
}
for(int i = 0; i <= n; ++i)
{
for(int j = 0; j <= w; ++j)
{
if(i == 0 || j == 0)
{
dp[i][j] = 0;
}
else if(j-wt[i-1] <0)
{
dp[i][j] = dp[i-1][j];
}
else
{
dp[i][j] = max(dp[i-1][j], val[i-1] + dp[i-1][j-wt[i-1]]);
}
}
}
cout<<dp[n][w]<<endl;
}
return 0;
}