#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 |
75 ms |
27636 KB |
Output is correct |
2 |
Correct |
90 ms |
31788 KB |
Output is correct |
3 |
Correct |
37 ms |
22100 KB |
Output is correct |
4 |
Correct |
34 ms |
22100 KB |
Output is correct |
5 |
Correct |
315 ms |
60276 KB |
Output is correct |
6 |
Correct |
429 ms |
57564 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Correct |
153 ms |
34256 KB |
Output is correct |
3 |
Correct |
158 ms |
39500 KB |
Output is correct |
4 |
Correct |
74 ms |
27660 KB |
Output is correct |
5 |
Correct |
89 ms |
31768 KB |
Output is correct |
6 |
Correct |
0 ms |
212 KB |
Output is correct |
7 |
Correct |
1 ms |
212 KB |
Output is correct |
8 |
Correct |
0 ms |
212 KB |
Output is correct |
9 |
Correct |
1 ms |
212 KB |
Output is correct |
10 |
Correct |
41 ms |
22156 KB |
Output is correct |
11 |
Correct |
32 ms |
22128 KB |
Output is correct |
12 |
Correct |
89 ms |
30016 KB |
Output is correct |
13 |
Correct |
99 ms |
34488 KB |
Output is correct |
14 |
Correct |
85 ms |
28872 KB |
Output is correct |
15 |
Correct |
119 ms |
30196 KB |
Output is correct |
16 |
Correct |
107 ms |
28880 KB |
Output is correct |
17 |
Correct |
113 ms |
32016 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
44 ms |
22092 KB |
Output is correct |
2 |
Correct |
36 ms |
22228 KB |
Output is correct |
3 |
Correct |
106 ms |
22200 KB |
Output is correct |
4 |
Correct |
79 ms |
23372 KB |
Output is correct |
5 |
Correct |
133 ms |
24700 KB |
Output is correct |
6 |
Correct |
106 ms |
25416 KB |
Output is correct |
7 |
Correct |
109 ms |
26060 KB |
Output is correct |
8 |
Correct |
106 ms |
24520 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
212 KB |
Output is correct |
2 |
Correct |
1 ms |
212 KB |
Output is correct |
3 |
Correct |
1 ms |
212 KB |
Output is correct |
4 |
Correct |
1 ms |
212 KB |
Output is correct |
5 |
Correct |
1 ms |
212 KB |
Output is correct |
6 |
Correct |
0 ms |
212 KB |
Output is correct |
7 |
Correct |
1 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 |
2 ms |
468 KB |
Output is correct |
11 |
Correct |
1 ms |
340 KB |
Output is correct |
12 |
Correct |
2 ms |
340 KB |
Output is correct |
13 |
Correct |
1 ms |
212 KB |
Output is correct |
14 |
Correct |
1 ms |
340 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
212 KB |
Output is correct |
2 |
Correct |
1 ms |
212 KB |
Output is correct |
3 |
Correct |
1 ms |
212 KB |
Output is correct |
4 |
Correct |
1 ms |
212 KB |
Output is correct |
5 |
Correct |
1 ms |
212 KB |
Output is correct |
6 |
Correct |
0 ms |
212 KB |
Output is correct |
7 |
Correct |
1 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 |
2 ms |
468 KB |
Output is correct |
11 |
Correct |
1 ms |
340 KB |
Output is correct |
12 |
Correct |
2 ms |
340 KB |
Output is correct |
13 |
Correct |
1 ms |
212 KB |
Output is correct |
14 |
Correct |
1 ms |
340 KB |
Output is correct |
15 |
Correct |
1 ms |
340 KB |
Output is correct |
16 |
Correct |
2 ms |
468 KB |
Output is correct |
17 |
Correct |
35 ms |
3896 KB |
Output is correct |
18 |
Correct |
27 ms |
3992 KB |
Output is correct |
19 |
Correct |
32 ms |
3328 KB |
Output is correct |
20 |
Correct |
25 ms |
3128 KB |
Output is correct |
21 |
Correct |
27 ms |
3740 KB |
Output is correct |
22 |
Correct |
55 ms |
6220 KB |
Output is correct |
23 |
Correct |
9 ms |
1432 KB |
Output is correct |
24 |
Correct |
31 ms |
3204 KB |
Output is correct |
25 |
Correct |
2 ms |
468 KB |
Output is correct |
26 |
Correct |
9 ms |
1140 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
212 KB |
Output is correct |
2 |
Correct |
1 ms |
212 KB |
Output is correct |
3 |
Correct |
1 ms |
212 KB |
Output is correct |
4 |
Correct |
1 ms |
212 KB |
Output is correct |
5 |
Correct |
1 ms |
212 KB |
Output is correct |
6 |
Correct |
0 ms |
212 KB |
Output is correct |
7 |
Correct |
1 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 |
2 ms |
468 KB |
Output is correct |
11 |
Correct |
1 ms |
340 KB |
Output is correct |
12 |
Correct |
2 ms |
340 KB |
Output is correct |
13 |
Correct |
1 ms |
212 KB |
Output is correct |
14 |
Correct |
1 ms |
340 KB |
Output is correct |
15 |
Correct |
1 ms |
340 KB |
Output is correct |
16 |
Correct |
2 ms |
468 KB |
Output is correct |
17 |
Correct |
35 ms |
3896 KB |
Output is correct |
18 |
Correct |
27 ms |
3992 KB |
Output is correct |
19 |
Correct |
32 ms |
3328 KB |
Output is correct |
20 |
Correct |
25 ms |
3128 KB |
Output is correct |
21 |
Correct |
27 ms |
3740 KB |
Output is correct |
22 |
Correct |
55 ms |
6220 KB |
Output is correct |
23 |
Correct |
9 ms |
1432 KB |
Output is correct |
24 |
Correct |
31 ms |
3204 KB |
Output is correct |
25 |
Correct |
2 ms |
468 KB |
Output is correct |
26 |
Correct |
9 ms |
1140 KB |
Output is correct |
27 |
Correct |
4 ms |
1140 KB |
Output is correct |
28 |
Correct |
178 ms |
18580 KB |
Output is correct |
29 |
Correct |
240 ms |
29164 KB |
Output is correct |
30 |
Correct |
249 ms |
35836 KB |
Output is correct |
31 |
Correct |
225 ms |
38336 KB |
Output is correct |
32 |
Correct |
207 ms |
19704 KB |
Output is correct |
33 |
Correct |
291 ms |
40096 KB |
Output is correct |
34 |
Correct |
234 ms |
35324 KB |
Output is correct |
35 |
Correct |
88 ms |
11844 KB |
Output is correct |
36 |
Correct |
302 ms |
30176 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
44 ms |
22092 KB |
Output is correct |
2 |
Correct |
36 ms |
22228 KB |
Output is correct |
3 |
Correct |
106 ms |
22200 KB |
Output is correct |
4 |
Correct |
79 ms |
23372 KB |
Output is correct |
5 |
Correct |
133 ms |
24700 KB |
Output is correct |
6 |
Correct |
106 ms |
25416 KB |
Output is correct |
7 |
Correct |
109 ms |
26060 KB |
Output is correct |
8 |
Correct |
106 ms |
24520 KB |
Output is correct |
9 |
Correct |
145 ms |
29268 KB |
Output is correct |
10 |
Correct |
122 ms |
14652 KB |
Output is correct |
11 |
Correct |
236 ms |
29332 KB |
Output is correct |
12 |
Correct |
1 ms |
212 KB |
Output is correct |
13 |
Correct |
1 ms |
212 KB |
Output is correct |
14 |
Correct |
1 ms |
328 KB |
Output is correct |
15 |
Correct |
1 ms |
212 KB |
Output is correct |
16 |
Correct |
1 ms |
212 KB |
Output is correct |
17 |
Correct |
1 ms |
212 KB |
Output is correct |
18 |
Correct |
34 ms |
22100 KB |
Output is correct |
19 |
Correct |
41 ms |
22128 KB |
Output is correct |
20 |
Correct |
46 ms |
22224 KB |
Output is correct |
21 |
Correct |
59 ms |
22200 KB |
Output is correct |
22 |
Correct |
185 ms |
29040 KB |
Output is correct |
23 |
Correct |
234 ms |
34708 KB |
Output is correct |
24 |
Correct |
261 ms |
37272 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
75 ms |
27636 KB |
Output is correct |
2 |
Correct |
90 ms |
31788 KB |
Output is correct |
3 |
Correct |
37 ms |
22100 KB |
Output is correct |
4 |
Correct |
34 ms |
22100 KB |
Output is correct |
5 |
Correct |
315 ms |
60276 KB |
Output is correct |
6 |
Correct |
429 ms |
57564 KB |
Output is correct |
7 |
Correct |
0 ms |
212 KB |
Output is correct |
8 |
Correct |
153 ms |
34256 KB |
Output is correct |
9 |
Correct |
158 ms |
39500 KB |
Output is correct |
10 |
Correct |
74 ms |
27660 KB |
Output is correct |
11 |
Correct |
89 ms |
31768 KB |
Output is correct |
12 |
Correct |
0 ms |
212 KB |
Output is correct |
13 |
Correct |
1 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 |
41 ms |
22156 KB |
Output is correct |
17 |
Correct |
32 ms |
22128 KB |
Output is correct |
18 |
Correct |
89 ms |
30016 KB |
Output is correct |
19 |
Correct |
99 ms |
34488 KB |
Output is correct |
20 |
Correct |
85 ms |
28872 KB |
Output is correct |
21 |
Correct |
119 ms |
30196 KB |
Output is correct |
22 |
Correct |
107 ms |
28880 KB |
Output is correct |
23 |
Correct |
113 ms |
32016 KB |
Output is correct |
24 |
Correct |
44 ms |
22092 KB |
Output is correct |
25 |
Correct |
36 ms |
22228 KB |
Output is correct |
26 |
Correct |
106 ms |
22200 KB |
Output is correct |
27 |
Correct |
79 ms |
23372 KB |
Output is correct |
28 |
Correct |
133 ms |
24700 KB |
Output is correct |
29 |
Correct |
106 ms |
25416 KB |
Output is correct |
30 |
Correct |
109 ms |
26060 KB |
Output is correct |
31 |
Correct |
106 ms |
24520 KB |
Output is correct |
32 |
Correct |
1 ms |
212 KB |
Output is correct |
33 |
Correct |
1 ms |
212 KB |
Output is correct |
34 |
Correct |
1 ms |
212 KB |
Output is correct |
35 |
Correct |
1 ms |
212 KB |
Output is correct |
36 |
Correct |
1 ms |
212 KB |
Output is correct |
37 |
Correct |
0 ms |
212 KB |
Output is correct |
38 |
Correct |
1 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 |
2 ms |
468 KB |
Output is correct |
42 |
Correct |
1 ms |
340 KB |
Output is correct |
43 |
Correct |
2 ms |
340 KB |
Output is correct |
44 |
Correct |
1 ms |
212 KB |
Output is correct |
45 |
Correct |
1 ms |
340 KB |
Output is correct |
46 |
Correct |
1 ms |
340 KB |
Output is correct |
47 |
Correct |
2 ms |
468 KB |
Output is correct |
48 |
Correct |
35 ms |
3896 KB |
Output is correct |
49 |
Correct |
27 ms |
3992 KB |
Output is correct |
50 |
Correct |
32 ms |
3328 KB |
Output is correct |
51 |
Correct |
25 ms |
3128 KB |
Output is correct |
52 |
Correct |
27 ms |
3740 KB |
Output is correct |
53 |
Correct |
55 ms |
6220 KB |
Output is correct |
54 |
Correct |
9 ms |
1432 KB |
Output is correct |
55 |
Correct |
31 ms |
3204 KB |
Output is correct |
56 |
Correct |
2 ms |
468 KB |
Output is correct |
57 |
Correct |
9 ms |
1140 KB |
Output is correct |
58 |
Correct |
4 ms |
1140 KB |
Output is correct |
59 |
Correct |
178 ms |
18580 KB |
Output is correct |
60 |
Correct |
240 ms |
29164 KB |
Output is correct |
61 |
Correct |
249 ms |
35836 KB |
Output is correct |
62 |
Correct |
225 ms |
38336 KB |
Output is correct |
63 |
Correct |
207 ms |
19704 KB |
Output is correct |
64 |
Correct |
291 ms |
40096 KB |
Output is correct |
65 |
Correct |
234 ms |
35324 KB |
Output is correct |
66 |
Correct |
88 ms |
11844 KB |
Output is correct |
67 |
Correct |
302 ms |
30176 KB |
Output is correct |
68 |
Correct |
145 ms |
29268 KB |
Output is correct |
69 |
Correct |
122 ms |
14652 KB |
Output is correct |
70 |
Correct |
236 ms |
29332 KB |
Output is correct |
71 |
Correct |
1 ms |
212 KB |
Output is correct |
72 |
Correct |
1 ms |
212 KB |
Output is correct |
73 |
Correct |
1 ms |
328 KB |
Output is correct |
74 |
Correct |
1 ms |
212 KB |
Output is correct |
75 |
Correct |
1 ms |
212 KB |
Output is correct |
76 |
Correct |
1 ms |
212 KB |
Output is correct |
77 |
Correct |
34 ms |
22100 KB |
Output is correct |
78 |
Correct |
41 ms |
22128 KB |
Output is correct |
79 |
Correct |
46 ms |
22224 KB |
Output is correct |
80 |
Correct |
59 ms |
22200 KB |
Output is correct |
81 |
Correct |
185 ms |
29040 KB |
Output is correct |
82 |
Correct |
234 ms |
34708 KB |
Output is correct |
83 |
Correct |
261 ms |
37272 KB |
Output is correct |
84 |
Correct |
324 ms |
52260 KB |
Output is correct |
85 |
Correct |
304 ms |
54188 KB |
Output is correct |
86 |
Correct |
417 ms |
50456 KB |
Output is correct |
87 |
Correct |
451 ms |
51544 KB |
Output is correct |
88 |
Correct |
1 ms |
304 KB |
Output is correct |
89 |
Correct |
463 ms |
52360 KB |
Output is correct |
90 |
Correct |
326 ms |
48084 KB |
Output is correct |
91 |
Correct |
278 ms |
42752 KB |
Output is correct |