-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path12865bj.cpp
More file actions
50 lines (38 loc) · 930 Bytes
/
12865bj.cpp
File metadata and controls
50 lines (38 loc) · 930 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
39
40
41
42
43
44
45
46
47
48
49
50
#include <iostream>
#include <utility>
#include <vector>
#include <algorithm>
using namespace std;
int n,k;
int W[101]; //W[0]=0
int V[101]; //V[0]=0
int D[101][100001]; //D[0][]=0
int main() {
cin >> n>> k;
//vector<pair<int, int> > v1(n);//물건 n개
for (int i = 1; i <= n; i++) //1번부터 n번 물건
{
cin >> W[i] >> V[i];
}
//초깃값
for (int i = 0; i <= n; i++) {
D[i][0] = 0;
}
for (int i = 0; i <= k; i++) {
D[0][i] = 0;
}
//dp -->d[i][k]:1번째~i번째 물건까지 고려서 담을수 있는 최대 가치
//limit 기준으로 i=1~n까지 d[i][limit] 계산하는 흐름
for (int limit = 1; limit <= k; limit++) {
for (int i = 1; i <= n; i++) { // i=1부터 i=n까지 d[i][k]
if (W[i] > limit) { //담을수 없어
D[i][limit] = D[i - 1][limit];
}
else {//있어
D[i][limit] = max(D[i-1][limit-W[i]]+V[i],D[i-1][limit]);
}
}
}
cout << D[n][k];
return 0;
}