forked from drken1215/book_algorithm_solution
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcode_5_7.cpp
More file actions
38 lines (31 loc) · 820 Bytes
/
code_5_7.cpp
File metadata and controls
38 lines (31 loc) · 820 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;
template<class T> void chmax(T& a, T b) {
if (a < b) {
a = b;
}
}
int main() {
// 入力
int N;
long long W;
cin >> N >> W;
vector<long long> weight(N), value(N);
for (int i = 0; i < N; ++i) cin >> weight[i] >> value[i];
// DP テーブル定義
vector<vector<long long>> dp(N + 1, vector<long long>(W + 1, 0));
// DPループ
for (int i = 0; i < N; ++i) {
for (int w = 0; w <= W; ++w) {
// i 番目の品物を選ぶ場合
if (w - weight[i] >= 0) {
chmax(dp[i + 1][w], dp[i][w - weight[i]] + value[i]);
}
// i 番目の品物を選ばない場合
chmax(dp[i + 1][w], dp[i][w]);
}
}
// 最適値の出力
cout << dp[N][W] << endl;
}