-
Notifications
You must be signed in to change notification settings - Fork 0
/
dpe.cpp
70 lines (63 loc) · 1.39 KB
/
dpe.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
62
63
64
65
66
67
68
69
70
#include <iostream>
using namespace std;
template <class T>
inline bool chmax(T &a, T b)
{
if (a < b)
{
a = b;
return true;
}
return false;
}
template <class T>
inline bool chmin(T &a, T b)
{
if (a > b)
{
a = b;
return true;
}
return false;
}
const long long INF = 1LL << 60;
const int MAX_N = 110;
const int MAX_V = 100100;
//入力
int N;
long long W, weight[MAX_N], value[MAX_N]; //品物の品質は100個なので少し余裕を持たせてサイズ110
//DPテーブル
long long dp[MAX_N][MAX_V];
int main()
{
cin >> N >> W;
for (int i = 0; i < N; ++i)
cin >> weight[i] >> value[i];
//初期化
for (int i = 0; i < MAX_N; ++i)
for (int j = 0; j < MAX_V; ++j)
dp[i][j] = INF;
//初期条件
dp[0][0] = 0;
//DPループ
for (int i = 0; i < N; ++i)
{
for (int sum_v = 0; sum_v < MAX_V; ++sum_v)
{
//i番目の品物を選ぶ場合
if (sum_v - value[i] >= 0)
{
chmin(dp[i + 1][sum_v], dp[i][sum_v - value[i]] + weight[i]);
}
//i番目の品物を選ばない場合
chmin(dp[i + 1][sum_v], dp[i][sum_v]);
}
}
long long res = 0;
for (int sum_v = 0; sum_v < MAX_V; ++sum_v)
{
if (dp[N][sum_v] <= W)
res = sum_v;
}
cout << res << endl;
}