-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathProblem0002.h
More file actions
56 lines (48 loc) · 1.05 KB
/
Problem0002.h
File metadata and controls
56 lines (48 loc) · 1.05 KB
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
//
// Created by Fengwei Zhang on 2021/6/17.
//
#ifndef ACWINGSOLUTION_PROBLEM0002_H
#define ACWINGSOLUTION_PROBLEM0002_H
#include <iostream>
#include <cstring>
using namespace std;
class Problem0002
{
private:
struct Item
{
int value;
int size;
Item()
{
value = 0;
size = 0;
}
};
int knapsack_max_value(const int &n, const int &v, const Item *items)
{
int dp[v + 1];
memset(dp, 0, sizeof dp);
for (int i = 0; i < n; ++i)
{
for (auto j = v; j >= items[i].size; --j)
{
dp[j] = max(dp[j], dp[j - items[i].size] + items[i].value);
}
}
return dp[v];
}
int main()
{
int n, v;
scanf("%d%d", &n, &v);
Item items[n];
for (int i = 0; i < n; ++i)
{
scanf("%d%d", &items[i].size, &items[i].value);
}
printf("%d\n", knapsack_max_value(n, v, items));
return 0;
}
};
#endif // ACWINGSOLUTION_PROBLEM0002_H