#include "fish.h"
#include <bits/stdc++.h>
using namespace std;
long long max_weights(int N, int M, vector<int> X, vector<int> Y, vector<int> W) {
vector<vector<long long>> fishes(N+1);
vector<vector<int>> states(N+1, {0});
for (int i = 0; i < M; ++i) {
++Y[i];
for (int j = max(X[i]-1, 0); j <= min(X[i]+1, N-1); ++j) {
states[j].push_back(Y[i]);
}
}
for (int i = 0; i <= N; ++i) {
sort(states[i].begin(), states[i].end());
states[i].erase(unique(states[i].begin(), states[i].end()), states[i].end());
fishes[i].resize(states[i].size());
}
for (int i = 0; i < M; ++i) {
int x = X[i];
int y = lower_bound(states[x].begin(), states[x].end(), Y[i]) - states[x].begin();
fishes[x][y] += W[i];
}
vector<vector<long long>> dp_up(N+1), dp_down(N+1);
for (int i = 0; i <= N; ++i) {
int sz = states[i].size();
dp_up[i].assign(sz, 0);
dp_down[i].assign(sz, 0);
if (i > 1) {
long long best = max(dp_up[i-2][0], dp_down[i-2][0]), sum = 0;
for (int j = 0, lef = 0, mid = 0; j < sz; ++j) {
while (lef+1 < (int)states[i-2].size() && states[i-2][lef+1] <= states[i][j]) {
++lef;
while (mid+1 < (int)states[i-1].size() && states[i-1][mid+1] <= states[i-2][lef]) {
++mid;
best += fishes[i-1][mid];
sum += fishes[i-1][mid];
}
best = max(best, max(dp_up[i-2][lef], dp_down[i-2][lef]) + sum);
}
dp_up[i][j] = max(dp_up[i][j], best);
}
best = 0;
sum = accumulate(fishes[i-1].begin(), fishes[i-1].end(), 0LL);
for (int j = sz-1, lef = states[i-2].size(), mid = states[i-1].size(); j >= 0; --j) {
while (lef > 0 && states[i-2][lef-1] >= states[i][j]) {
--lef;
while (mid > 0 && states[i-1][mid-1] > states[i-2][lef]) {
--mid;
sum -= fishes[i-1][mid];
}
best = max(best, max(dp_up[i-2][lef], dp_down[i-2][lef]) + sum);
}
dp_up[i][j] = max(dp_up[i][j], best);
}
}
if (i > 0) {
long long best = dp_up[i-1][0], sum = 0;
for (int j = 0, lef = 0; j < sz; ++j) {
while (lef+1 < (int)states[i-1].size() && states[i-1][lef+1] <= states[i][j]) {
++lef;
best += fishes[i-1][lef];
best = max(best, dp_up[i-1][lef]);
}
dp_up[i][j] = max(dp_up[i][j], best);
}
best = 0;
sum = accumulate(fishes[i].begin(), fishes[i].end(), 0LL);
for (int j = sz-1, lef = states[i-1].size(); j >= 0; --j) {
while (lef > 0 && states[i-1][lef-1] >= states[i][j]) {
--lef;
best = max(best, max(dp_down[i-1][lef], dp_up[i-1][lef]) + sum);
}
dp_down[i][j] = max(dp_down[i][j], best-sum);
sum -= fishes[i][j];
}
}
}
return max(dp_up[N][0], dp_down[N][0]);
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
63 ms |
27760 KB |
Output is correct |
2 |
Correct |
74 ms |
31752 KB |
Output is correct |
3 |
Correct |
25 ms |
22184 KB |
Output is correct |
4 |
Correct |
24 ms |
22204 KB |
Output is correct |
5 |
Correct |
204 ms |
60324 KB |
Output is correct |
6 |
Correct |
246 ms |
57592 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Correct |
109 ms |
34340 KB |
Output is correct |
3 |
Correct |
131 ms |
39304 KB |
Output is correct |
4 |
Correct |
61 ms |
27692 KB |
Output is correct |
5 |
Correct |
79 ms |
31796 KB |
Output is correct |
6 |
Correct |
0 ms |
212 KB |
Output is correct |
7 |
Correct |
0 ms |
212 KB |
Output is correct |
8 |
Correct |
0 ms |
212 KB |
Output is correct |
9 |
Correct |
0 ms |
212 KB |
Output is correct |
10 |
Correct |
24 ms |
22228 KB |
Output is correct |
11 |
Correct |
30 ms |
22112 KB |
Output is correct |
12 |
Correct |
70 ms |
29956 KB |
Output is correct |
13 |
Correct |
90 ms |
34500 KB |
Output is correct |
14 |
Correct |
68 ms |
29000 KB |
Output is correct |
15 |
Correct |
72 ms |
30160 KB |
Output is correct |
16 |
Correct |
65 ms |
28900 KB |
Output is correct |
17 |
Correct |
74 ms |
32044 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
25 ms |
22148 KB |
Output is correct |
2 |
Correct |
25 ms |
22092 KB |
Output is correct |
3 |
Correct |
42 ms |
22228 KB |
Output is correct |
4 |
Correct |
42 ms |
23672 KB |
Output is correct |
5 |
Correct |
73 ms |
26128 KB |
Output is correct |
6 |
Correct |
59 ms |
25524 KB |
Output is correct |
7 |
Correct |
62 ms |
26060 KB |
Output is correct |
8 |
Correct |
65 ms |
26060 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Correct |
0 ms |
212 KB |
Output is correct |
3 |
Correct |
0 ms |
212 KB |
Output is correct |
4 |
Correct |
0 ms |
212 KB |
Output is correct |
5 |
Correct |
0 ms |
212 KB |
Output is correct |
6 |
Correct |
1 ms |
212 KB |
Output is correct |
7 |
Correct |
0 ms |
212 KB |
Output is correct |
8 |
Correct |
0 ms |
212 KB |
Output is correct |
9 |
Correct |
1 ms |
340 KB |
Output is correct |
10 |
Correct |
1 ms |
468 KB |
Output is correct |
11 |
Correct |
1 ms |
340 KB |
Output is correct |
12 |
Correct |
1 ms |
456 KB |
Output is correct |
13 |
Correct |
1 ms |
212 KB |
Output is correct |
14 |
Correct |
1 ms |
312 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Correct |
0 ms |
212 KB |
Output is correct |
3 |
Correct |
0 ms |
212 KB |
Output is correct |
4 |
Correct |
0 ms |
212 KB |
Output is correct |
5 |
Correct |
0 ms |
212 KB |
Output is correct |
6 |
Correct |
1 ms |
212 KB |
Output is correct |
7 |
Correct |
0 ms |
212 KB |
Output is correct |
8 |
Correct |
0 ms |
212 KB |
Output is correct |
9 |
Correct |
1 ms |
340 KB |
Output is correct |
10 |
Correct |
1 ms |
468 KB |
Output is correct |
11 |
Correct |
1 ms |
340 KB |
Output is correct |
12 |
Correct |
1 ms |
456 KB |
Output is correct |
13 |
Correct |
1 ms |
212 KB |
Output is correct |
14 |
Correct |
1 ms |
312 KB |
Output is correct |
15 |
Correct |
1 ms |
340 KB |
Output is correct |
16 |
Correct |
1 ms |
468 KB |
Output is correct |
17 |
Correct |
20 ms |
4360 KB |
Output is correct |
18 |
Correct |
20 ms |
4024 KB |
Output is correct |
19 |
Correct |
19 ms |
4044 KB |
Output is correct |
20 |
Correct |
19 ms |
3892 KB |
Output is correct |
21 |
Correct |
19 ms |
3760 KB |
Output is correct |
22 |
Correct |
39 ms |
7488 KB |
Output is correct |
23 |
Correct |
5 ms |
1364 KB |
Output is correct |
24 |
Correct |
15 ms |
3596 KB |
Output is correct |
25 |
Correct |
1 ms |
440 KB |
Output is correct |
26 |
Correct |
5 ms |
1236 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Correct |
0 ms |
212 KB |
Output is correct |
3 |
Correct |
0 ms |
212 KB |
Output is correct |
4 |
Correct |
0 ms |
212 KB |
Output is correct |
5 |
Correct |
0 ms |
212 KB |
Output is correct |
6 |
Correct |
1 ms |
212 KB |
Output is correct |
7 |
Correct |
0 ms |
212 KB |
Output is correct |
8 |
Correct |
0 ms |
212 KB |
Output is correct |
9 |
Correct |
1 ms |
340 KB |
Output is correct |
10 |
Correct |
1 ms |
468 KB |
Output is correct |
11 |
Correct |
1 ms |
340 KB |
Output is correct |
12 |
Correct |
1 ms |
456 KB |
Output is correct |
13 |
Correct |
1 ms |
212 KB |
Output is correct |
14 |
Correct |
1 ms |
312 KB |
Output is correct |
15 |
Correct |
1 ms |
340 KB |
Output is correct |
16 |
Correct |
1 ms |
468 KB |
Output is correct |
17 |
Correct |
20 ms |
4360 KB |
Output is correct |
18 |
Correct |
20 ms |
4024 KB |
Output is correct |
19 |
Correct |
19 ms |
4044 KB |
Output is correct |
20 |
Correct |
19 ms |
3892 KB |
Output is correct |
21 |
Correct |
19 ms |
3760 KB |
Output is correct |
22 |
Correct |
39 ms |
7488 KB |
Output is correct |
23 |
Correct |
5 ms |
1364 KB |
Output is correct |
24 |
Correct |
15 ms |
3596 KB |
Output is correct |
25 |
Correct |
1 ms |
440 KB |
Output is correct |
26 |
Correct |
5 ms |
1236 KB |
Output is correct |
27 |
Correct |
2 ms |
1076 KB |
Output is correct |
28 |
Correct |
106 ms |
18676 KB |
Output is correct |
29 |
Correct |
156 ms |
29528 KB |
Output is correct |
30 |
Correct |
184 ms |
39864 KB |
Output is correct |
31 |
Correct |
172 ms |
39772 KB |
Output is correct |
32 |
Correct |
134 ms |
24292 KB |
Output is correct |
33 |
Correct |
177 ms |
40416 KB |
Output is correct |
34 |
Correct |
180 ms |
40400 KB |
Output is correct |
35 |
Correct |
56 ms |
13492 KB |
Output is correct |
36 |
Correct |
164 ms |
35544 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
25 ms |
22148 KB |
Output is correct |
2 |
Correct |
25 ms |
22092 KB |
Output is correct |
3 |
Correct |
42 ms |
22228 KB |
Output is correct |
4 |
Correct |
42 ms |
23672 KB |
Output is correct |
5 |
Correct |
73 ms |
26128 KB |
Output is correct |
6 |
Correct |
59 ms |
25524 KB |
Output is correct |
7 |
Correct |
62 ms |
26060 KB |
Output is correct |
8 |
Correct |
65 ms |
26060 KB |
Output is correct |
9 |
Correct |
68 ms |
30500 KB |
Output is correct |
10 |
Correct |
61 ms |
16244 KB |
Output is correct |
11 |
Correct |
124 ms |
32456 KB |
Output is correct |
12 |
Correct |
1 ms |
212 KB |
Output is correct |
13 |
Correct |
0 ms |
212 KB |
Output is correct |
14 |
Correct |
0 ms |
212 KB |
Output is correct |
15 |
Correct |
1 ms |
212 KB |
Output is correct |
16 |
Correct |
0 ms |
212 KB |
Output is correct |
17 |
Correct |
0 ms |
212 KB |
Output is correct |
18 |
Correct |
25 ms |
22232 KB |
Output is correct |
19 |
Correct |
25 ms |
22228 KB |
Output is correct |
20 |
Correct |
25 ms |
22132 KB |
Output is correct |
21 |
Correct |
24 ms |
22184 KB |
Output is correct |
22 |
Correct |
101 ms |
30900 KB |
Output is correct |
23 |
Correct |
144 ms |
37900 KB |
Output is correct |
24 |
Correct |
139 ms |
39164 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
63 ms |
27760 KB |
Output is correct |
2 |
Correct |
74 ms |
31752 KB |
Output is correct |
3 |
Correct |
25 ms |
22184 KB |
Output is correct |
4 |
Correct |
24 ms |
22204 KB |
Output is correct |
5 |
Correct |
204 ms |
60324 KB |
Output is correct |
6 |
Correct |
246 ms |
57592 KB |
Output is correct |
7 |
Correct |
0 ms |
212 KB |
Output is correct |
8 |
Correct |
109 ms |
34340 KB |
Output is correct |
9 |
Correct |
131 ms |
39304 KB |
Output is correct |
10 |
Correct |
61 ms |
27692 KB |
Output is correct |
11 |
Correct |
79 ms |
31796 KB |
Output is correct |
12 |
Correct |
0 ms |
212 KB |
Output is correct |
13 |
Correct |
0 ms |
212 KB |
Output is correct |
14 |
Correct |
0 ms |
212 KB |
Output is correct |
15 |
Correct |
0 ms |
212 KB |
Output is correct |
16 |
Correct |
24 ms |
22228 KB |
Output is correct |
17 |
Correct |
30 ms |
22112 KB |
Output is correct |
18 |
Correct |
70 ms |
29956 KB |
Output is correct |
19 |
Correct |
90 ms |
34500 KB |
Output is correct |
20 |
Correct |
68 ms |
29000 KB |
Output is correct |
21 |
Correct |
72 ms |
30160 KB |
Output is correct |
22 |
Correct |
65 ms |
28900 KB |
Output is correct |
23 |
Correct |
74 ms |
32044 KB |
Output is correct |
24 |
Correct |
25 ms |
22148 KB |
Output is correct |
25 |
Correct |
25 ms |
22092 KB |
Output is correct |
26 |
Correct |
42 ms |
22228 KB |
Output is correct |
27 |
Correct |
42 ms |
23672 KB |
Output is correct |
28 |
Correct |
73 ms |
26128 KB |
Output is correct |
29 |
Correct |
59 ms |
25524 KB |
Output is correct |
30 |
Correct |
62 ms |
26060 KB |
Output is correct |
31 |
Correct |
65 ms |
26060 KB |
Output is correct |
32 |
Correct |
0 ms |
212 KB |
Output is correct |
33 |
Correct |
0 ms |
212 KB |
Output is correct |
34 |
Correct |
0 ms |
212 KB |
Output is correct |
35 |
Correct |
0 ms |
212 KB |
Output is correct |
36 |
Correct |
0 ms |
212 KB |
Output is correct |
37 |
Correct |
1 ms |
212 KB |
Output is correct |
38 |
Correct |
0 ms |
212 KB |
Output is correct |
39 |
Correct |
0 ms |
212 KB |
Output is correct |
40 |
Correct |
1 ms |
340 KB |
Output is correct |
41 |
Correct |
1 ms |
468 KB |
Output is correct |
42 |
Correct |
1 ms |
340 KB |
Output is correct |
43 |
Correct |
1 ms |
456 KB |
Output is correct |
44 |
Correct |
1 ms |
212 KB |
Output is correct |
45 |
Correct |
1 ms |
312 KB |
Output is correct |
46 |
Correct |
1 ms |
340 KB |
Output is correct |
47 |
Correct |
1 ms |
468 KB |
Output is correct |
48 |
Correct |
20 ms |
4360 KB |
Output is correct |
49 |
Correct |
20 ms |
4024 KB |
Output is correct |
50 |
Correct |
19 ms |
4044 KB |
Output is correct |
51 |
Correct |
19 ms |
3892 KB |
Output is correct |
52 |
Correct |
19 ms |
3760 KB |
Output is correct |
53 |
Correct |
39 ms |
7488 KB |
Output is correct |
54 |
Correct |
5 ms |
1364 KB |
Output is correct |
55 |
Correct |
15 ms |
3596 KB |
Output is correct |
56 |
Correct |
1 ms |
440 KB |
Output is correct |
57 |
Correct |
5 ms |
1236 KB |
Output is correct |
58 |
Correct |
2 ms |
1076 KB |
Output is correct |
59 |
Correct |
106 ms |
18676 KB |
Output is correct |
60 |
Correct |
156 ms |
29528 KB |
Output is correct |
61 |
Correct |
184 ms |
39864 KB |
Output is correct |
62 |
Correct |
172 ms |
39772 KB |
Output is correct |
63 |
Correct |
134 ms |
24292 KB |
Output is correct |
64 |
Correct |
177 ms |
40416 KB |
Output is correct |
65 |
Correct |
180 ms |
40400 KB |
Output is correct |
66 |
Correct |
56 ms |
13492 KB |
Output is correct |
67 |
Correct |
164 ms |
35544 KB |
Output is correct |
68 |
Correct |
68 ms |
30500 KB |
Output is correct |
69 |
Correct |
61 ms |
16244 KB |
Output is correct |
70 |
Correct |
124 ms |
32456 KB |
Output is correct |
71 |
Correct |
1 ms |
212 KB |
Output is correct |
72 |
Correct |
0 ms |
212 KB |
Output is correct |
73 |
Correct |
0 ms |
212 KB |
Output is correct |
74 |
Correct |
1 ms |
212 KB |
Output is correct |
75 |
Correct |
0 ms |
212 KB |
Output is correct |
76 |
Correct |
0 ms |
212 KB |
Output is correct |
77 |
Correct |
25 ms |
22232 KB |
Output is correct |
78 |
Correct |
25 ms |
22228 KB |
Output is correct |
79 |
Correct |
25 ms |
22132 KB |
Output is correct |
80 |
Correct |
24 ms |
22184 KB |
Output is correct |
81 |
Correct |
101 ms |
30900 KB |
Output is correct |
82 |
Correct |
144 ms |
37900 KB |
Output is correct |
83 |
Correct |
139 ms |
39164 KB |
Output is correct |
84 |
Correct |
217 ms |
55252 KB |
Output is correct |
85 |
Correct |
235 ms |
56944 KB |
Output is correct |
86 |
Correct |
245 ms |
55480 KB |
Output is correct |
87 |
Correct |
244 ms |
57296 KB |
Output is correct |
88 |
Correct |
0 ms |
212 KB |
Output is correct |
89 |
Correct |
251 ms |
57400 KB |
Output is correct |
90 |
Correct |
186 ms |
53060 KB |
Output is correct |
91 |
Correct |
157 ms |
47408 KB |
Output is correct |