#include "fish.h"
#include <bits/stdc++.h>
using namespace std;
#define sz(x) ((int) (x).size())
typedef long long ll;
ll max_weights(int n, int m, vector<int> x, vector<int> y, vector<int> w) {
vector<vector<int>> p(n + 3);
for (int i = 0; i < m; i++)
for (int d = x[i] == 0 ? 0 : -1; d < (x[i] == n - 1 ? 1 : 2); d++)
p[x[i] + d + 1].push_back(y[i]);
vector<vector<ll>> ws(n + 3);
for (int i = 0; i < n + 3; i++) {
p[i].push_back(-1);
sort(p[i].begin(), p[i].end());
p[i].erase(unique(p[i].begin(), p[i].end()), p[i].end());
ws[i].resize(sz(p[i]));
}
for (int i = 0; i < m; i++) {
int j = (int) (lower_bound(p[x[i] + 1].begin(), p[x[i] + 1].end(), y[i]) - p[x[i] + 1].begin());
ws[x[i] + 1][j] += w[i];
}
for (int i = 0; i < n + 3; i++)
for (int j = 1; j < sz(ws[i]); j++)
ws[i][j] += ws[i][j - 1];
auto weightBelow = [&](int i, int j) {
int k = (int) (upper_bound(p[i].begin(), p[i].end(), j) - p[i].begin()) - 1;
return ws[i][k];
};
vector<vector<ll>> dpu(n + 3), dpd(n + 3);
dpu[0].push_back(0);
dpd[0].push_back(0);
for (int i = 1; i < n + 2; i++) {
dpu[i].resize(sz(p[i]));
dpd[i].resize(sz(p[i]));
ll mdp = 0;
for (int j = 0, k = 0; j < sz(p[i]); j++) {
while (k < sz(p[i - 1]) && p[i - 1][k] <= p[i][j])
mdp = max(mdp, dpu[i - 1][k] - weightBelow(i - 1, p[i - 1][k])), k++;
dpu[i][j] = mdp + weightBelow(i - 1, p[i][j]);
}
mdp = 0;
for (int j = sz(p[i]) - 1, k = sz(p[i - 1]) - 1; j >= 0; j--) {
while (k >= 0 && p[i - 1][k] >= p[i][j])
mdp = max(mdp, dpd[i - 1][k]), k--;
dpd[i][j] = max(dpd[i][j], mdp - weightBelow(i, p[i][j]) + weightBelow(i + 1, p[i][j]));
}
if (i > 1) {
mdp = 0;
for (int j = 0, k = 0; j < sz(p[i]); j++) {
while (k < sz(p[i - 2]) && p[i - 2][k] <= p[i][j])
mdp = max(mdp, dpd[i - 2][k] - weightBelow(i - 1, p[i - 2][k])), k++;
dpu[i][j] = max(dpu[i][j], mdp + weightBelow(i - 1, p[i][j]));
}
mdp = 0;
for (int j = sz(p[i]) - 1, k = sz(p[i - 2]) - 1; j >= 0; j--) {
while (k >= 0 && p[i - 2][k] >= p[i][j])
mdp = max(mdp, dpd[i - 2][k]), k--;
dpu[i][j] = max(dpu[i][j], mdp);
}
}
for (int j = 0; j < sz(p[i]); j++)
dpd[i][j] = max(dpd[i][j], dpu[i][j] + weightBelow(i + 1, p[i][j]));
}
return dpd[n + 1][0];
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
103 ms |
27720 KB |
Output is correct |
2 |
Correct |
121 ms |
31776 KB |
Output is correct |
3 |
Correct |
36 ms |
22192 KB |
Output is correct |
4 |
Correct |
34 ms |
22304 KB |
Output is correct |
5 |
Correct |
481 ms |
60260 KB |
Output is correct |
6 |
Correct |
380 ms |
57456 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
212 KB |
Output is correct |
2 |
Correct |
195 ms |
34348 KB |
Output is correct |
3 |
Correct |
219 ms |
39328 KB |
Output is correct |
4 |
Correct |
103 ms |
27648 KB |
Output is correct |
5 |
Correct |
131 ms |
31900 KB |
Output is correct |
6 |
Correct |
1 ms |
300 KB |
Output is correct |
7 |
Correct |
0 ms |
212 KB |
Output is correct |
8 |
Correct |
1 ms |
212 KB |
Output is correct |
9 |
Correct |
1 ms |
212 KB |
Output is correct |
10 |
Correct |
32 ms |
22212 KB |
Output is correct |
11 |
Correct |
31 ms |
22120 KB |
Output is correct |
12 |
Correct |
139 ms |
29936 KB |
Output is correct |
13 |
Correct |
170 ms |
34492 KB |
Output is correct |
14 |
Correct |
130 ms |
28832 KB |
Output is correct |
15 |
Correct |
117 ms |
30172 KB |
Output is correct |
16 |
Correct |
131 ms |
28892 KB |
Output is correct |
17 |
Correct |
137 ms |
31948 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
31 ms |
22200 KB |
Output is correct |
2 |
Correct |
36 ms |
22092 KB |
Output is correct |
3 |
Correct |
66 ms |
22212 KB |
Output is correct |
4 |
Correct |
55 ms |
23628 KB |
Output is correct |
5 |
Correct |
90 ms |
26188 KB |
Output is correct |
6 |
Correct |
86 ms |
25448 KB |
Output is correct |
7 |
Correct |
94 ms |
26016 KB |
Output is correct |
8 |
Correct |
96 ms |
26224 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
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 |
1 ms |
212 KB |
Output is correct |
7 |
Correct |
1 ms |
212 KB |
Output is correct |
8 |
Correct |
1 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 |
312 KB |
Output is correct |
12 |
Correct |
1 ms |
340 KB |
Output is correct |
13 |
Correct |
1 ms |
212 KB |
Output is correct |
14 |
Correct |
1 ms |
340 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
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 |
1 ms |
212 KB |
Output is correct |
7 |
Correct |
1 ms |
212 KB |
Output is correct |
8 |
Correct |
1 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 |
312 KB |
Output is correct |
12 |
Correct |
1 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 |
3 ms |
468 KB |
Output is correct |
17 |
Correct |
40 ms |
4340 KB |
Output is correct |
18 |
Correct |
35 ms |
4012 KB |
Output is correct |
19 |
Correct |
34 ms |
4016 KB |
Output is correct |
20 |
Correct |
28 ms |
3880 KB |
Output is correct |
21 |
Correct |
28 ms |
3860 KB |
Output is correct |
22 |
Correct |
62 ms |
7396 KB |
Output is correct |
23 |
Correct |
12 ms |
1404 KB |
Output is correct |
24 |
Correct |
32 ms |
3632 KB |
Output is correct |
25 |
Correct |
1 ms |
468 KB |
Output is correct |
26 |
Correct |
9 ms |
1236 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
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 |
1 ms |
212 KB |
Output is correct |
7 |
Correct |
1 ms |
212 KB |
Output is correct |
8 |
Correct |
1 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 |
312 KB |
Output is correct |
12 |
Correct |
1 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 |
3 ms |
468 KB |
Output is correct |
17 |
Correct |
40 ms |
4340 KB |
Output is correct |
18 |
Correct |
35 ms |
4012 KB |
Output is correct |
19 |
Correct |
34 ms |
4016 KB |
Output is correct |
20 |
Correct |
28 ms |
3880 KB |
Output is correct |
21 |
Correct |
28 ms |
3860 KB |
Output is correct |
22 |
Correct |
62 ms |
7396 KB |
Output is correct |
23 |
Correct |
12 ms |
1404 KB |
Output is correct |
24 |
Correct |
32 ms |
3632 KB |
Output is correct |
25 |
Correct |
1 ms |
468 KB |
Output is correct |
26 |
Correct |
9 ms |
1236 KB |
Output is correct |
27 |
Correct |
3 ms |
1080 KB |
Output is correct |
28 |
Correct |
183 ms |
18648 KB |
Output is correct |
29 |
Correct |
310 ms |
29428 KB |
Output is correct |
30 |
Correct |
442 ms |
39952 KB |
Output is correct |
31 |
Correct |
406 ms |
39776 KB |
Output is correct |
32 |
Correct |
229 ms |
24276 KB |
Output is correct |
33 |
Correct |
425 ms |
40556 KB |
Output is correct |
34 |
Correct |
423 ms |
40436 KB |
Output is correct |
35 |
Correct |
113 ms |
13476 KB |
Output is correct |
36 |
Correct |
385 ms |
35468 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
31 ms |
22200 KB |
Output is correct |
2 |
Correct |
36 ms |
22092 KB |
Output is correct |
3 |
Correct |
66 ms |
22212 KB |
Output is correct |
4 |
Correct |
55 ms |
23628 KB |
Output is correct |
5 |
Correct |
90 ms |
26188 KB |
Output is correct |
6 |
Correct |
86 ms |
25448 KB |
Output is correct |
7 |
Correct |
94 ms |
26016 KB |
Output is correct |
8 |
Correct |
96 ms |
26224 KB |
Output is correct |
9 |
Correct |
110 ms |
30540 KB |
Output is correct |
10 |
Correct |
72 ms |
16296 KB |
Output is correct |
11 |
Correct |
155 ms |
32312 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 |
300 KB |
Output is correct |
15 |
Correct |
0 ms |
300 KB |
Output is correct |
16 |
Correct |
1 ms |
296 KB |
Output is correct |
17 |
Correct |
1 ms |
340 KB |
Output is correct |
18 |
Correct |
39 ms |
22092 KB |
Output is correct |
19 |
Correct |
31 ms |
22128 KB |
Output is correct |
20 |
Correct |
36 ms |
22192 KB |
Output is correct |
21 |
Correct |
42 ms |
22088 KB |
Output is correct |
22 |
Correct |
145 ms |
30884 KB |
Output is correct |
23 |
Correct |
201 ms |
37840 KB |
Output is correct |
24 |
Correct |
179 ms |
39120 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
103 ms |
27720 KB |
Output is correct |
2 |
Correct |
121 ms |
31776 KB |
Output is correct |
3 |
Correct |
36 ms |
22192 KB |
Output is correct |
4 |
Correct |
34 ms |
22304 KB |
Output is correct |
5 |
Correct |
481 ms |
60260 KB |
Output is correct |
6 |
Correct |
380 ms |
57456 KB |
Output is correct |
7 |
Correct |
1 ms |
212 KB |
Output is correct |
8 |
Correct |
195 ms |
34348 KB |
Output is correct |
9 |
Correct |
219 ms |
39328 KB |
Output is correct |
10 |
Correct |
103 ms |
27648 KB |
Output is correct |
11 |
Correct |
131 ms |
31900 KB |
Output is correct |
12 |
Correct |
1 ms |
300 KB |
Output is correct |
13 |
Correct |
0 ms |
212 KB |
Output is correct |
14 |
Correct |
1 ms |
212 KB |
Output is correct |
15 |
Correct |
1 ms |
212 KB |
Output is correct |
16 |
Correct |
32 ms |
22212 KB |
Output is correct |
17 |
Correct |
31 ms |
22120 KB |
Output is correct |
18 |
Correct |
139 ms |
29936 KB |
Output is correct |
19 |
Correct |
170 ms |
34492 KB |
Output is correct |
20 |
Correct |
130 ms |
28832 KB |
Output is correct |
21 |
Correct |
117 ms |
30172 KB |
Output is correct |
22 |
Correct |
131 ms |
28892 KB |
Output is correct |
23 |
Correct |
137 ms |
31948 KB |
Output is correct |
24 |
Correct |
31 ms |
22200 KB |
Output is correct |
25 |
Correct |
36 ms |
22092 KB |
Output is correct |
26 |
Correct |
66 ms |
22212 KB |
Output is correct |
27 |
Correct |
55 ms |
23628 KB |
Output is correct |
28 |
Correct |
90 ms |
26188 KB |
Output is correct |
29 |
Correct |
86 ms |
25448 KB |
Output is correct |
30 |
Correct |
94 ms |
26016 KB |
Output is correct |
31 |
Correct |
96 ms |
26224 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 |
1 ms |
212 KB |
Output is correct |
38 |
Correct |
1 ms |
212 KB |
Output is correct |
39 |
Correct |
1 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 |
312 KB |
Output is correct |
43 |
Correct |
1 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 |
3 ms |
468 KB |
Output is correct |
48 |
Correct |
40 ms |
4340 KB |
Output is correct |
49 |
Correct |
35 ms |
4012 KB |
Output is correct |
50 |
Correct |
34 ms |
4016 KB |
Output is correct |
51 |
Correct |
28 ms |
3880 KB |
Output is correct |
52 |
Correct |
28 ms |
3860 KB |
Output is correct |
53 |
Correct |
62 ms |
7396 KB |
Output is correct |
54 |
Correct |
12 ms |
1404 KB |
Output is correct |
55 |
Correct |
32 ms |
3632 KB |
Output is correct |
56 |
Correct |
1 ms |
468 KB |
Output is correct |
57 |
Correct |
9 ms |
1236 KB |
Output is correct |
58 |
Correct |
3 ms |
1080 KB |
Output is correct |
59 |
Correct |
183 ms |
18648 KB |
Output is correct |
60 |
Correct |
310 ms |
29428 KB |
Output is correct |
61 |
Correct |
442 ms |
39952 KB |
Output is correct |
62 |
Correct |
406 ms |
39776 KB |
Output is correct |
63 |
Correct |
229 ms |
24276 KB |
Output is correct |
64 |
Correct |
425 ms |
40556 KB |
Output is correct |
65 |
Correct |
423 ms |
40436 KB |
Output is correct |
66 |
Correct |
113 ms |
13476 KB |
Output is correct |
67 |
Correct |
385 ms |
35468 KB |
Output is correct |
68 |
Correct |
110 ms |
30540 KB |
Output is correct |
69 |
Correct |
72 ms |
16296 KB |
Output is correct |
70 |
Correct |
155 ms |
32312 KB |
Output is correct |
71 |
Correct |
0 ms |
212 KB |
Output is correct |
72 |
Correct |
1 ms |
212 KB |
Output is correct |
73 |
Correct |
0 ms |
300 KB |
Output is correct |
74 |
Correct |
0 ms |
300 KB |
Output is correct |
75 |
Correct |
1 ms |
296 KB |
Output is correct |
76 |
Correct |
1 ms |
340 KB |
Output is correct |
77 |
Correct |
39 ms |
22092 KB |
Output is correct |
78 |
Correct |
31 ms |
22128 KB |
Output is correct |
79 |
Correct |
36 ms |
22192 KB |
Output is correct |
80 |
Correct |
42 ms |
22088 KB |
Output is correct |
81 |
Correct |
145 ms |
30884 KB |
Output is correct |
82 |
Correct |
201 ms |
37840 KB |
Output is correct |
83 |
Correct |
179 ms |
39120 KB |
Output is correct |
84 |
Correct |
478 ms |
55424 KB |
Output is correct |
85 |
Correct |
474 ms |
56864 KB |
Output is correct |
86 |
Correct |
363 ms |
55412 KB |
Output is correct |
87 |
Correct |
388 ms |
57292 KB |
Output is correct |
88 |
Correct |
1 ms |
212 KB |
Output is correct |
89 |
Correct |
377 ms |
57296 KB |
Output is correct |
90 |
Correct |
298 ms |
53044 KB |
Output is correct |
91 |
Correct |
216 ms |
47396 KB |
Output is correct |