#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<int>> a(n + 1);
vector<vector<array<int, 2>>> b(n + 1);
vector<vector<array<long long, 2>>> dp[2];
dp[0].resize(n + 1);
dp[1].resize(n + 1);
for (int i = 0; i < m; ++i) {
b[x[i]].push_back({y[i], w[i]});
if (0 <= x[i] - 1) {
a[x[i] - 1].push_back(y[i]);
}
if (x[i] + 1 < n) {
a[x[i] + 1].push_back(y[i]);
}
}
for (int i = 0; i <= n; ++i) {
a[i].push_back(-1);
sort(a[i].begin(), a[i].end());
sort(b[i].begin(), b[i].end());
a[i].resize(unique(a[i].begin(), a[i].end()) - a[i].begin());
}
for (int i = 0; i <= n; ++i) {
for (auto pos : a[i]) {
dp[0][i].push_back({pos, 0});
dp[1][i].push_back({pos, 0});
}
if (i == 0) {
continue;
}
long long sumW;
long long best;
int ptrW, ptrDp;
vector<array<int, 2>> weights;
auto pre = dp[0][i - 1];
// 0
weights = b[i - 1];
sumW = 0;
ptrW = 0;
for (auto &[h, val]: pre) {
while (ptrW < (int)weights.size() && h >= weights[ptrW][0]) {
sumW += weights[ptrW][1];
++ptrW;
}
val -= sumW;
}
sumW = 0;
ptrW = 0;
ptrDp = 0;
best = -1e18;
for (auto &[h, val]: dp[0][i]) {
while (ptrW < (int)weights.size() && h >= weights[ptrW][0]) {
sumW += weights[ptrW][1];
++ptrW;
}
while (ptrDp < (int)pre.size() && h >= pre[ptrDp][0]) {
best = max(best, pre[ptrDp][1]);
++ptrDp;
}
val = sumW + best;
}
// 1
weights = b[i];
sumW = 0;
ptrW = 0;
pre = dp[1][i - 1];
for (auto &[h, val]: pre) {
while (ptrW < (int)weights.size() && h >= weights[ptrW][0]) {
sumW += weights[ptrW][1];
++ptrW;
}
val += sumW;
}
ptrW = (int)weights.size() - 1;
ptrDp = (int)pre.size() - 1;
best = -1e18;
sumW = 0;
for (auto [h, w] : weights) {
sumW += w;
}
for (int j = (int)dp[1][i].size() - 1; j >= 0; --j) {
auto &[h, val] = dp[1][i][j];
while (ptrW >= 0 && h < weights[ptrW][0]) {
sumW -= weights[ptrW][1];
--ptrW;
}
while (ptrDp >= 0 && h <= pre[ptrDp][0]) {
best = max(best, pre[ptrDp][1]);
--ptrDp;
}
val = best - sumW;
}
// 1->0
if (i >= 2) {
weights = b[i - 1];
sumW = 0;
ptrW = 0;
ptrDp = 0;
best = 0;
pre = dp[1][i - 2];
for (auto &[h, val] : dp[0][i]) {
while (ptrW < (int)weights.size() && h >= weights[ptrW][0]) {
sumW += weights[ptrW][1];
++ptrW;
}
while (ptrDp < (int)pre.size() && h >= pre[ptrDp][0]) {
best = max(best, pre[ptrDp][1]);
++ptrDp;
}
val = max(val, best + sumW);
}
sumW = 0;
ptrW = 0;
for (auto &[h, val] : pre) {
while (ptrW < (int)weights.size() && h >= weights[ptrW][0]) {
sumW += weights[ptrW][1];
++ptrW;
}
val += sumW;
}
ptrDp = pre.size() - 1;
best = -1e18;
for (auto &[h, val] : dp[0][i]) {
while (ptrDp >= 0 && h <= pre[ptrDp][0]) {
best = max(best, pre[ptrDp][1]);
--ptrDp;
}
val = max(val, best);
}
}
// 0->1
for (int j = 0; j < (int) dp[0][i].size(); ++j) {
dp[1][i][j][1] = max(dp[1][i][j][1], dp[0][i][j][1]);
}
}
return dp[1][n][0][1];
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
69 ms |
22552 KB |
Output is correct |
2 |
Correct |
82 ms |
26124 KB |
Output is correct |
3 |
Correct |
31 ms |
19020 KB |
Output is correct |
4 |
Correct |
31 ms |
19012 KB |
Output is correct |
5 |
Correct |
229 ms |
56284 KB |
Output is correct |
6 |
Correct |
333 ms |
60596 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Correct |
130 ms |
32648 KB |
Output is correct |
3 |
Correct |
149 ms |
37364 KB |
Output is correct |
4 |
Correct |
82 ms |
22924 KB |
Output is correct |
5 |
Correct |
82 ms |
26064 KB |
Output is correct |
6 |
Correct |
0 ms |
304 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 |
32 ms |
19008 KB |
Output is correct |
11 |
Correct |
32 ms |
18964 KB |
Output is correct |
12 |
Correct |
97 ms |
26840 KB |
Output is correct |
13 |
Correct |
93 ms |
30164 KB |
Output is correct |
14 |
Correct |
73 ms |
24568 KB |
Output is correct |
15 |
Correct |
83 ms |
27104 KB |
Output is correct |
16 |
Correct |
78 ms |
24552 KB |
Output is correct |
17 |
Correct |
82 ms |
27140 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
36 ms |
18952 KB |
Output is correct |
2 |
Correct |
31 ms |
19076 KB |
Output is correct |
3 |
Correct |
72 ms |
21928 KB |
Output is correct |
4 |
Correct |
73 ms |
22276 KB |
Output is correct |
5 |
Correct |
143 ms |
27616 KB |
Output is correct |
6 |
Correct |
146 ms |
27660 KB |
Output is correct |
7 |
Correct |
141 ms |
27588 KB |
Output is correct |
8 |
Correct |
135 ms |
27592 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Correct |
0 ms |
212 KB |
Output is correct |
3 |
Correct |
1 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 |
1 ms |
212 KB |
Output is correct |
9 |
Correct |
1 ms |
340 KB |
Output is correct |
10 |
Correct |
2 ms |
596 KB |
Output is correct |
11 |
Correct |
1 ms |
340 KB |
Output is correct |
12 |
Correct |
1 ms |
468 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 |
0 ms |
212 KB |
Output is correct |
2 |
Correct |
0 ms |
212 KB |
Output is correct |
3 |
Correct |
1 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 |
1 ms |
212 KB |
Output is correct |
9 |
Correct |
1 ms |
340 KB |
Output is correct |
10 |
Correct |
2 ms |
596 KB |
Output is correct |
11 |
Correct |
1 ms |
340 KB |
Output is correct |
12 |
Correct |
1 ms |
468 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 |
1 ms |
468 KB |
Output is correct |
17 |
Correct |
24 ms |
5332 KB |
Output is correct |
18 |
Correct |
23 ms |
5588 KB |
Output is correct |
19 |
Correct |
23 ms |
5580 KB |
Output is correct |
20 |
Correct |
22 ms |
5200 KB |
Output is correct |
21 |
Correct |
23 ms |
5056 KB |
Output is correct |
22 |
Correct |
50 ms |
9932 KB |
Output is correct |
23 |
Correct |
6 ms |
1492 KB |
Output is correct |
24 |
Correct |
19 ms |
4052 KB |
Output is correct |
25 |
Correct |
1 ms |
468 KB |
Output is correct |
26 |
Correct |
5 ms |
1364 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Correct |
0 ms |
212 KB |
Output is correct |
3 |
Correct |
1 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 |
1 ms |
212 KB |
Output is correct |
9 |
Correct |
1 ms |
340 KB |
Output is correct |
10 |
Correct |
2 ms |
596 KB |
Output is correct |
11 |
Correct |
1 ms |
340 KB |
Output is correct |
12 |
Correct |
1 ms |
468 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 |
1 ms |
468 KB |
Output is correct |
17 |
Correct |
24 ms |
5332 KB |
Output is correct |
18 |
Correct |
23 ms |
5588 KB |
Output is correct |
19 |
Correct |
23 ms |
5580 KB |
Output is correct |
20 |
Correct |
22 ms |
5200 KB |
Output is correct |
21 |
Correct |
23 ms |
5056 KB |
Output is correct |
22 |
Correct |
50 ms |
9932 KB |
Output is correct |
23 |
Correct |
6 ms |
1492 KB |
Output is correct |
24 |
Correct |
19 ms |
4052 KB |
Output is correct |
25 |
Correct |
1 ms |
468 KB |
Output is correct |
26 |
Correct |
5 ms |
1364 KB |
Output is correct |
27 |
Correct |
3 ms |
1236 KB |
Output is correct |
28 |
Correct |
110 ms |
23428 KB |
Output is correct |
29 |
Correct |
183 ms |
31996 KB |
Output is correct |
30 |
Correct |
204 ms |
39176 KB |
Output is correct |
31 |
Correct |
209 ms |
39196 KB |
Output is correct |
32 |
Correct |
142 ms |
31208 KB |
Output is correct |
33 |
Correct |
199 ms |
38864 KB |
Output is correct |
34 |
Correct |
191 ms |
38384 KB |
Output is correct |
35 |
Correct |
68 ms |
15952 KB |
Output is correct |
36 |
Correct |
181 ms |
35148 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
36 ms |
18952 KB |
Output is correct |
2 |
Correct |
31 ms |
19076 KB |
Output is correct |
3 |
Correct |
72 ms |
21928 KB |
Output is correct |
4 |
Correct |
73 ms |
22276 KB |
Output is correct |
5 |
Correct |
143 ms |
27616 KB |
Output is correct |
6 |
Correct |
146 ms |
27660 KB |
Output is correct |
7 |
Correct |
141 ms |
27588 KB |
Output is correct |
8 |
Correct |
135 ms |
27592 KB |
Output is correct |
9 |
Correct |
137 ms |
34040 KB |
Output is correct |
10 |
Correct |
95 ms |
19876 KB |
Output is correct |
11 |
Correct |
200 ms |
39600 KB |
Output is correct |
12 |
Correct |
1 ms |
212 KB |
Output is correct |
13 |
Correct |
0 ms |
212 KB |
Output is correct |
14 |
Correct |
1 ms |
256 KB |
Output is correct |
15 |
Correct |
0 ms |
212 KB |
Output is correct |
16 |
Correct |
0 ms |
288 KB |
Output is correct |
17 |
Correct |
0 ms |
212 KB |
Output is correct |
18 |
Correct |
32 ms |
19076 KB |
Output is correct |
19 |
Correct |
32 ms |
18976 KB |
Output is correct |
20 |
Correct |
32 ms |
19028 KB |
Output is correct |
21 |
Correct |
32 ms |
19020 KB |
Output is correct |
22 |
Correct |
153 ms |
32204 KB |
Output is correct |
23 |
Correct |
226 ms |
46748 KB |
Output is correct |
24 |
Correct |
221 ms |
50764 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
69 ms |
22552 KB |
Output is correct |
2 |
Correct |
82 ms |
26124 KB |
Output is correct |
3 |
Correct |
31 ms |
19020 KB |
Output is correct |
4 |
Correct |
31 ms |
19012 KB |
Output is correct |
5 |
Correct |
229 ms |
56284 KB |
Output is correct |
6 |
Correct |
333 ms |
60596 KB |
Output is correct |
7 |
Correct |
0 ms |
212 KB |
Output is correct |
8 |
Correct |
130 ms |
32648 KB |
Output is correct |
9 |
Correct |
149 ms |
37364 KB |
Output is correct |
10 |
Correct |
82 ms |
22924 KB |
Output is correct |
11 |
Correct |
82 ms |
26064 KB |
Output is correct |
12 |
Correct |
0 ms |
304 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 |
32 ms |
19008 KB |
Output is correct |
17 |
Correct |
32 ms |
18964 KB |
Output is correct |
18 |
Correct |
97 ms |
26840 KB |
Output is correct |
19 |
Correct |
93 ms |
30164 KB |
Output is correct |
20 |
Correct |
73 ms |
24568 KB |
Output is correct |
21 |
Correct |
83 ms |
27104 KB |
Output is correct |
22 |
Correct |
78 ms |
24552 KB |
Output is correct |
23 |
Correct |
82 ms |
27140 KB |
Output is correct |
24 |
Correct |
36 ms |
18952 KB |
Output is correct |
25 |
Correct |
31 ms |
19076 KB |
Output is correct |
26 |
Correct |
72 ms |
21928 KB |
Output is correct |
27 |
Correct |
73 ms |
22276 KB |
Output is correct |
28 |
Correct |
143 ms |
27616 KB |
Output is correct |
29 |
Correct |
146 ms |
27660 KB |
Output is correct |
30 |
Correct |
141 ms |
27588 KB |
Output is correct |
31 |
Correct |
135 ms |
27592 KB |
Output is correct |
32 |
Correct |
0 ms |
212 KB |
Output is correct |
33 |
Correct |
0 ms |
212 KB |
Output is correct |
34 |
Correct |
1 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 |
1 ms |
212 KB |
Output is correct |
40 |
Correct |
1 ms |
340 KB |
Output is correct |
41 |
Correct |
2 ms |
596 KB |
Output is correct |
42 |
Correct |
1 ms |
340 KB |
Output is correct |
43 |
Correct |
1 ms |
468 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 |
1 ms |
468 KB |
Output is correct |
48 |
Correct |
24 ms |
5332 KB |
Output is correct |
49 |
Correct |
23 ms |
5588 KB |
Output is correct |
50 |
Correct |
23 ms |
5580 KB |
Output is correct |
51 |
Correct |
22 ms |
5200 KB |
Output is correct |
52 |
Correct |
23 ms |
5056 KB |
Output is correct |
53 |
Correct |
50 ms |
9932 KB |
Output is correct |
54 |
Correct |
6 ms |
1492 KB |
Output is correct |
55 |
Correct |
19 ms |
4052 KB |
Output is correct |
56 |
Correct |
1 ms |
468 KB |
Output is correct |
57 |
Correct |
5 ms |
1364 KB |
Output is correct |
58 |
Correct |
3 ms |
1236 KB |
Output is correct |
59 |
Correct |
110 ms |
23428 KB |
Output is correct |
60 |
Correct |
183 ms |
31996 KB |
Output is correct |
61 |
Correct |
204 ms |
39176 KB |
Output is correct |
62 |
Correct |
209 ms |
39196 KB |
Output is correct |
63 |
Correct |
142 ms |
31208 KB |
Output is correct |
64 |
Correct |
199 ms |
38864 KB |
Output is correct |
65 |
Correct |
191 ms |
38384 KB |
Output is correct |
66 |
Correct |
68 ms |
15952 KB |
Output is correct |
67 |
Correct |
181 ms |
35148 KB |
Output is correct |
68 |
Correct |
137 ms |
34040 KB |
Output is correct |
69 |
Correct |
95 ms |
19876 KB |
Output is correct |
70 |
Correct |
200 ms |
39600 KB |
Output is correct |
71 |
Correct |
1 ms |
212 KB |
Output is correct |
72 |
Correct |
0 ms |
212 KB |
Output is correct |
73 |
Correct |
1 ms |
256 KB |
Output is correct |
74 |
Correct |
0 ms |
212 KB |
Output is correct |
75 |
Correct |
0 ms |
288 KB |
Output is correct |
76 |
Correct |
0 ms |
212 KB |
Output is correct |
77 |
Correct |
32 ms |
19076 KB |
Output is correct |
78 |
Correct |
32 ms |
18976 KB |
Output is correct |
79 |
Correct |
32 ms |
19028 KB |
Output is correct |
80 |
Correct |
32 ms |
19020 KB |
Output is correct |
81 |
Correct |
153 ms |
32204 KB |
Output is correct |
82 |
Correct |
226 ms |
46748 KB |
Output is correct |
83 |
Correct |
221 ms |
50764 KB |
Output is correct |
84 |
Correct |
231 ms |
53472 KB |
Output is correct |
85 |
Correct |
234 ms |
53204 KB |
Output is correct |
86 |
Correct |
337 ms |
59656 KB |
Output is correct |
87 |
Correct |
363 ms |
60872 KB |
Output is correct |
88 |
Correct |
0 ms |
212 KB |
Output is correct |
89 |
Correct |
368 ms |
61504 KB |
Output is correct |
90 |
Correct |
264 ms |
56584 KB |
Output is correct |
91 |
Correct |
211 ms |
51104 KB |
Output is correct |