We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
There was an error while loading. Please reload this page.
1 parent 795350a commit df5e78eCopy full SHA for df5e78e
2주차 Knapsack/박준석/1535.cpp
@@ -0,0 +1,44 @@
1
+#include <iostream>
2
+#include <algorithm>
3
+
4
+using namespace std;
5
6
+int L[20] = { 0, };
7
+int J[20] = { 0, };
8
+int N;
9
+int mem[20][101] = {0,};
10
11
+int knap(int i, int hp) {
12
+ if (hp <= 0) //죽으면 -100
13
+ return -100;
14
+ if (i == N) //다 돌면 끝
15
+ return 0;
16
+ if (mem[i][hp] != -1) // 이미 계산
17
+ return mem[i][hp];
18
+ mem[i][hp] = max((knap(i + 1, hp - L[i]) + J[i]), knap(i + 1, hp));
19
20
+}
21
22
+int main() {
23
+ ios::sync_with_stdio(false);
24
+ cin.tie(NULL);
25
+ cout.tie(NULL);
26
27
+ cin >> N;
28
29
+ for (int i = 0; i < N; i++) {
30
+ cin >> L[i];
31
+ }
32
33
+ cin >> J[i];
34
35
+ for (int i = 0; i < 20; i++) {
36
+ for (int j = 0; j < 101; j++) {
37
+ mem[i][j] = -1;
38
39
40
41
+ cout << knap(0, 100);
42
43
44
0 commit comments