Skip to content

Commit df5e78e

Browse files
authored
Create 1535.cpp
1 parent 795350a commit df5e78e

File tree

1 file changed

+44
-0
lines changed

1 file changed

+44
-0
lines changed

2주차 Knapsack/박준석/1535.cpp

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -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+
return mem[i][hp];
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+
for (int i = 0; i < N; i++) {
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

Comments
 (0)