forked from sitz/UVa-Online-Judge
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy path10819.cpp
112 lines (107 loc) · 1.78 KB
/
10819.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
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
#include <bits/stdc++.h>
using namespace std;
#define MAX(a, b) (a > b ? a : b)
struct ss
{
int cost;
int fav;
};
struct NewOld
{
int New, Old;
};
ss List[102];
int T[10300];
char F[10300];
NewOld B[10300];
int D, I, Sum, Max, k;
vector<ss> tmp;
vector<int> V, New;
void Dynamic()
{
int i, ind, cost, fav;
int limit = D;
ss xyz;
if (D > 1800)
limit = D + 200;
Max = 0;
V.clear();
for (i = 0; i < I; i++)
{
if (List[i].cost > limit)
continue;
tmp.clear();
New.clear();
for (ind = 0; ind < V.size(); ind++)
{
cost = V[ind] + List[i].cost;
fav = T[V[ind]] + List[i].fav;
if (cost > limit)
continue;
if (F[cost] == 1)
if (T[cost] >= fav)
continue;
if (F[cost] == 0)
{
F[cost] = 1;
New.push_back(cost);
}
xyz.cost = cost;
xyz.fav = fav;
tmp.push_back(xyz);
if (cost <= D || cost > 2000)
if (fav > Max)
Max = fav;
}
if (F[List[i].cost] == 0 || T[List[i].cost] < List[i].fav)
{
if (F[List[i].cost] == 0)
{
F[List[i].cost] = 1;
V.push_back(List[i].cost);
}
if (List[i].fav > Max && (List[i].cost <= D || List[i].cost > 2000))
Max = List[i].fav;
T[List[i].cost] = List[i].fav;
}
for (ind = tmp.size() - 1; ind >= 0; ind--)
T[tmp[ind].cost] = tmp[ind].fav;
for (ind = New.size() - 1; ind >= 0; ind--)
V.push_back(New[ind]);
}
}
void Free()
{
int i;
for (i = V.size() - 1; i >= 0; i--)
F[V[i]] = 0;
}
void Cal()
{
int i, total = 0;
if (Sum <= D)
{
for (i = 0; i < I; i++)
total += List[i].fav;
printf("%d\n", total);
return;
}
Dynamic();
printf("%d\n", Max);
Free();
}
int main()
{
int i;
while (scanf("%d%d", &D, &I) == 2)
{
Sum = 0;
for (i = 0; i < I; i++)
{
scanf("%d%d", &List[i].cost, &List[i].fav);
Sum += List[i].cost;
}
Cal();
}
return 0;
}