-
Notifications
You must be signed in to change notification settings - Fork 78
/
knapsack.cpp
61 lines (53 loc) · 1.18 KB
/
knapsack.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
50
51
52
53
54
55
56
57
58
59
60
61
/*Author : Abdallah Hemdan */
/***********************************************/
/*
___ __
* |\ \|\ \
* \ \ \_\ \
* \ \ ___ \emdan
* \ \ \\ \ \
* \ \__\\ \_\
* \|__| \|__|
*/
#include <iostream>
#include <cmath>
#include <string>
#include <vector>
#include <algorithm>
#include <iomanip>
#include <vector>
#include <functional>
#define all(v) v.begin(), v.end()
#define mp make_pair
#define pb push_back
#define endl '\n'
typedef long long int ll;
// freopen("input.txt","r",stdin);
// freopen("output.txt","w",stdout);
using namespace std;
int _0_1_KnapSack(int W, vector<int> val, vector<int> wt, int n) {
vector<vector<int>> K(n + 1,vector<int>(W + 1));
for (int i = 0; i <= n; i++) {
for (int w = 0; w <= W; w++) {
if (i == 0 || w == 0)
K[i][w] = 0;
else if (wt[i - 1] <= w)
K[i][w] = max(val[i - 1] + K[i - 1][w - wt[i - 1]], K[i - 1][w]);
else
K[i][w] = K[i - 1][w];
}
}
return K[n][W];
}
int main() {
int n, Capas;
cin >> Capas >> n;
vector<int> Values(n);
vector<int> Weights(n);
int Temp;
for (size_t i = 0; i < n; i++) {
cin >> Temp;
Values[i] = Weights[i] = Temp;
}
cout << _0_1_KnapSack(Capas, Values, Weights, n) << endl;
}