-
Notifications
You must be signed in to change notification settings - Fork 1
/
P1776.c
54 lines (46 loc) · 1014 Bytes
/
P1776.c
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
#include <stdio.h>
#include <stdlib.h>
#define maxw 40000
typedef struct Node {
int index;
int value;
} Node;
Node queue[maxw];
int head, tail;
void Init() {
head = 0;
tail = -1;
}
Node Top() {
return queue[head];
}
void Push(int index, int value) {
while (head <= tail && queue[tail].value <= value)
tail--;
tail++;
queue[tail].index = index;
queue[tail].value = value;
}
void Check(int index, int timeout) {
if (queue[head].index < index - timeout)
head++;
}
int dp[maxw + 1];
int main() {
int n, w;
scanf("%d %d", &n, &w);
int vi, wi, mi;
for (int i = 0; i < n; i++) {
scanf("%d %d %d", &vi, &wi, &mi);
for (int j = 0; j < wi; j++) {
Init();
for (int k = 0; k <= (w - j) / wi; k++) {
Push(k, dp[k * wi + j] - k * vi);
Check(k, mi);
dp[k * wi + j] = Top().value + k * vi;
}
}
}
printf("%d\n", dp[w]);
exit(0);
}