-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathu.cpp
More file actions
38 lines (30 loc) · 635 Bytes
/
u.cpp
File metadata and controls
38 lines (30 loc) · 635 Bytes
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
#include <iostream>
#include <vector>
using namespace std;
int knapsack(int n, int w, int wItems[], int vItems[])
{
if (w == 0 || n == 0)
{
return 0;
}
if (wItems[n - 1] > w)
{
return knapsack(n - 1, w, wItems, vItems);
}
else
{
return max(vItems[n - 1] + knapsack(n - 1, w - wItems[n - 1], wItems, vItems), knapsack(n - 1, w, wItems, vItems));
}
}
int main()
{
int n, w;
cin >> n >> w;
int wItems[n], vItems[n];
for (int i = 0; i < n; i++)
{
cin >> wItems[i] >> vItems[i];
}
cout << knapsack(n, w, wItems, vItems);
return 0;
}