#include "fish.h"
#include <bits/stdc++.h>
using namespace std;
#define int long long
#define pii pair<int, int>
#define ff first
#define ss second
#define pb push_back
#pragma GCC optimize("Ofast")
long long max_weights(int32_t n, int32_t m, vector<int32_t> X, vector<int32_t> Y,
vector<int32_t> W) {
for (int i = 0; i < m; i++) {
X[i]++; Y[i]++;
}
vector<pii> a[n + 1], ps[n + 1];
vector<int> critpts[n + 1];
for (int i = 1; i <= n; i++) {
a[i].pb({0, 0});
ps[i].pb({0, 0});
critpts[i].pb(0);
critpts[i].pb(n);
}
for (int i = 0; i < m; i++) {
a[X[i]].pb({Y[i], W[i]});
// critical points: (X[i] +- 1, Y[i]), (X[i], Y[i] - 1)
if (X[i] > 1) {
critpts[X[i] - 1].pb(Y[i]);
}
if (X[i] < n) {
critpts[X[i] + 1].pb(Y[i]);
}
critpts[X[i]].pb(Y[i] - 1);
}
for (int i = 1; i <= n; i++) {
sort(a[i].begin(), a[i].end());
for (pii &x : a[i]) {
ps[i].pb({x.ff, ps[i].back().ss + x.ss});
}
}
function<int(int, int)> GetPS = [&](int x, int y) -> int {
int lb = 0, rb = (int)(ps[x].size()) - 1;
while (lb < rb) {
int mid = (lb + rb + 1) >> 1;
if (ps[x][mid].ff <= y) lb = mid;
else rb = mid - 1;
}
return ps[x][lb].ss;
};
// O(n^2): dp[increasing][pos][height] = max ans from 1 to [pos] with [pos] getting [height], height must be [increasing]
vector<vector<pii>> dp[2];
dp[0].resize(n + 1); dp[1].resize(n + 1);
for (int i = 0; i <= n; i++) {
dp[0][0].pb({i, (i == 0 ? 0 : -1e18)});
dp[1][0].pb({i, (i == 0 ? 0 : -1e18)});
dp[0][1].pb({i, 0});
dp[1][1].pb({i, 0});
}
for (int i = 2; i <= n; i++) {
sort(critpts[i].begin(), critpts[i].end());
for (int &x : critpts[i]) {
if (!dp[0][i].empty() && x == dp[0][i].back().ff) continue;
dp[0][i].pb({x, 0});
dp[1][i].pb({x, 0});
}
}
vector<pii> cur;
int ptr = 0, curans = 0;
for (int i = 2; i <= n; i++) {
// Case 0: height of i is 0
// dp[flag][i][0].ff is always 0
for (int j = 0; j < (int)(dp[0][i - 1].size()); j++) {
dp[0][i][0].ss = max(dp[0][i][0].ss, max(dp[0][i - 1][j].ss, dp[1][i - 1][j].ss) + GetPS(i, dp[0][i - 1][j].ff));
}
dp[1][i][0].ss = dp[0][i][0].ss;
// Case 1: height of i-1 is 0
// Case 1.1: height of i-2 <= height of i
cur.clear();
for (int j = 0; j < (int)(dp[0][i - 2].size()); j++) {
cur.pb({dp[0][i - 2][j].ff, max(dp[0][i - 2][j].ss, dp[1][i - 2][j].ss)});
}
for (int j = 1; j < (int)(cur.size()); j++) {
cur[j].ss = max(cur[j].ss, cur[j - 1].ss);
}
ptr = 0; curans = 0;
for (int j = 0; j < (int)(dp[0][i].size()); j++) {
while (ptr < (int)(cur.size()) && cur[ptr].ff <= dp[0][i][j].ff) {
curans = max(curans, cur[ptr].ss);
ptr++;
}
dp[0][i][j].ss = max(dp[0][i][j].ss, curans + GetPS(i - 1, dp[0][i][j].ff));
dp[1][i][j].ss = max(dp[1][i][j].ss, curans + GetPS(i - 1, dp[0][i][j].ff));
}
// Case 1.2: height of i-2 >= height of i
cur.clear();
for (int j = 0; j < (int)(dp[0][i - 2].size()); j++) {
cur.pb({dp[0][i - 2][j].ff, max(dp[0][i - 2][j].ss, dp[1][i - 2][j].ss) + GetPS(i - 1, dp[0][i - 2][j].ff)});
}
for (int j = (int)(cur.size()) - 2; j >= 0; j--) {
cur[j].ss = max(cur[j].ss, cur[j + 1].ss);
}
ptr = (int)(cur.size()) - 1; curans = 0;
for (int j = (int)(dp[0][i].size()) - 1; j >= 0; j--) {
while (ptr >= 0 && cur[ptr].ff >= dp[0][i][j].ff) {
curans = max(curans, cur[ptr].ss);
ptr--;
}
dp[0][i][j].ss = max(dp[0][i][j].ss, curans);
dp[1][i][j].ss = max(dp[1][i][j].ss, curans);
}
// now height of i-1 > 0
// Case 2: height of i-1 <= height of i
cur.clear();
ptr = 1; curans = 0;
for (int j = 1; j < (int)(dp[0][i].size()); j++) {
curans += (GetPS(i - 1, dp[0][i][j].ff) - GetPS(i - 1, (j == 1 ? 1 : dp[0][i][j - 1].ff)));
while (ptr < (int)(dp[0][i - 1].size()) && dp[0][i - 1][ptr].ff <= dp[0][i][j].ff) {
curans = max(curans, dp[0][i - 1][ptr].ss + (GetPS(i - 1, dp[0][i][j].ff) - GetPS(i - 1, dp[0][i - 1][ptr].ff)));
ptr++;
}
dp[0][i][j].ss = max(dp[0][i][j].ss, curans);
}
// Case 3: height of i-1 >= height of i
cur.clear();
ptr = (int)(dp[1][i - 1].size()) - 1; curans = 0;
for (int j = (int)(dp[1][i].size()) - 1; j >= 1; j--) {
curans += (GetPS(i, (j + 1 == (int)(dp[1][i].size()) ? n : dp[1][i][j + 1].ff)) - GetPS(i, dp[1][i][j].ff));
while (ptr >= 0 && dp[1][i - 1][ptr].ff >= dp[0][i][j].ff) {
curans = max(curans, max(dp[0][i - 1][ptr].ss, dp[1][i - 1][ptr].ss) + (GetPS(i, dp[1][i - 1][ptr].ff) - GetPS(i, dp[1][i][j].ff)));
ptr--;
}
dp[1][i][j].ss = max(dp[1][i][j].ss, curans);
}
}
int ans = 0;
for (pii &x : dp[0][n]) ans = max(ans, x.ss);
for (pii &x : dp[1][n]) ans = max(ans, x.ss);
return ans;
}
#undef int
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
166 ms |
50980 KB |
Output is correct |
2 |
Correct |
197 ms |
57280 KB |
Output is correct |
3 |
Correct |
90 ms |
44828 KB |
Output is correct |
4 |
Correct |
86 ms |
44820 KB |
Output is correct |
5 |
Correct |
573 ms |
109300 KB |
Output is correct |
6 |
Correct |
660 ms |
123664 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
212 KB |
Output is correct |
2 |
Correct |
265 ms |
61596 KB |
Output is correct |
3 |
Correct |
317 ms |
69900 KB |
Output is correct |
4 |
Correct |
157 ms |
50992 KB |
Output is correct |
5 |
Correct |
183 ms |
57192 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 |
300 KB |
Output is correct |
9 |
Correct |
1 ms |
296 KB |
Output is correct |
10 |
Correct |
91 ms |
44820 KB |
Output is correct |
11 |
Correct |
93 ms |
44776 KB |
Output is correct |
12 |
Correct |
208 ms |
54140 KB |
Output is correct |
13 |
Correct |
234 ms |
61140 KB |
Output is correct |
14 |
Correct |
194 ms |
52680 KB |
Output is correct |
15 |
Correct |
210 ms |
56148 KB |
Output is correct |
16 |
Correct |
206 ms |
52768 KB |
Output is correct |
17 |
Correct |
226 ms |
57956 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
93 ms |
44828 KB |
Output is correct |
2 |
Correct |
88 ms |
44840 KB |
Output is correct |
3 |
Correct |
171 ms |
51288 KB |
Output is correct |
4 |
Correct |
190 ms |
52472 KB |
Output is correct |
5 |
Correct |
239 ms |
63092 KB |
Output is correct |
6 |
Correct |
272 ms |
62472 KB |
Output is correct |
7 |
Correct |
235 ms |
63024 KB |
Output is correct |
8 |
Correct |
240 ms |
63120 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
212 KB |
Output is correct |
2 |
Correct |
1 ms |
296 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 |
300 KB |
Output is correct |
6 |
Correct |
1 ms |
300 KB |
Output is correct |
7 |
Correct |
0 ms |
304 KB |
Output is correct |
8 |
Correct |
1 ms |
300 KB |
Output is correct |
9 |
Correct |
2 ms |
440 KB |
Output is correct |
10 |
Correct |
3 ms |
872 KB |
Output is correct |
11 |
Correct |
2 ms |
468 KB |
Output is correct |
12 |
Correct |
2 ms |
596 KB |
Output is correct |
13 |
Correct |
1 ms |
340 KB |
Output is correct |
14 |
Correct |
2 ms |
596 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
212 KB |
Output is correct |
2 |
Correct |
1 ms |
296 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 |
300 KB |
Output is correct |
6 |
Correct |
1 ms |
300 KB |
Output is correct |
7 |
Correct |
0 ms |
304 KB |
Output is correct |
8 |
Correct |
1 ms |
300 KB |
Output is correct |
9 |
Correct |
2 ms |
440 KB |
Output is correct |
10 |
Correct |
3 ms |
872 KB |
Output is correct |
11 |
Correct |
2 ms |
468 KB |
Output is correct |
12 |
Correct |
2 ms |
596 KB |
Output is correct |
13 |
Correct |
1 ms |
340 KB |
Output is correct |
14 |
Correct |
2 ms |
596 KB |
Output is correct |
15 |
Correct |
1 ms |
468 KB |
Output is correct |
16 |
Correct |
3 ms |
724 KB |
Output is correct |
17 |
Correct |
62 ms |
8636 KB |
Output is correct |
18 |
Correct |
54 ms |
9164 KB |
Output is correct |
19 |
Correct |
46 ms |
8984 KB |
Output is correct |
20 |
Correct |
47 ms |
8436 KB |
Output is correct |
21 |
Correct |
49 ms |
8364 KB |
Output is correct |
22 |
Correct |
99 ms |
16560 KB |
Output is correct |
23 |
Correct |
16 ms |
2616 KB |
Output is correct |
24 |
Correct |
47 ms |
6524 KB |
Output is correct |
25 |
Correct |
2 ms |
596 KB |
Output is correct |
26 |
Correct |
13 ms |
2516 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
212 KB |
Output is correct |
2 |
Correct |
1 ms |
296 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 |
300 KB |
Output is correct |
6 |
Correct |
1 ms |
300 KB |
Output is correct |
7 |
Correct |
0 ms |
304 KB |
Output is correct |
8 |
Correct |
1 ms |
300 KB |
Output is correct |
9 |
Correct |
2 ms |
440 KB |
Output is correct |
10 |
Correct |
3 ms |
872 KB |
Output is correct |
11 |
Correct |
2 ms |
468 KB |
Output is correct |
12 |
Correct |
2 ms |
596 KB |
Output is correct |
13 |
Correct |
1 ms |
340 KB |
Output is correct |
14 |
Correct |
2 ms |
596 KB |
Output is correct |
15 |
Correct |
1 ms |
468 KB |
Output is correct |
16 |
Correct |
3 ms |
724 KB |
Output is correct |
17 |
Correct |
62 ms |
8636 KB |
Output is correct |
18 |
Correct |
54 ms |
9164 KB |
Output is correct |
19 |
Correct |
46 ms |
8984 KB |
Output is correct |
20 |
Correct |
47 ms |
8436 KB |
Output is correct |
21 |
Correct |
49 ms |
8364 KB |
Output is correct |
22 |
Correct |
99 ms |
16560 KB |
Output is correct |
23 |
Correct |
16 ms |
2616 KB |
Output is correct |
24 |
Correct |
47 ms |
6524 KB |
Output is correct |
25 |
Correct |
2 ms |
596 KB |
Output is correct |
26 |
Correct |
13 ms |
2516 KB |
Output is correct |
27 |
Correct |
6 ms |
2160 KB |
Output is correct |
28 |
Correct |
299 ms |
37880 KB |
Output is correct |
29 |
Correct |
405 ms |
52716 KB |
Output is correct |
30 |
Correct |
530 ms |
86500 KB |
Output is correct |
31 |
Correct |
539 ms |
85976 KB |
Output is correct |
32 |
Correct |
343 ms |
51536 KB |
Output is correct |
33 |
Correct |
538 ms |
86624 KB |
Output is correct |
34 |
Correct |
529 ms |
86460 KB |
Output is correct |
35 |
Correct |
169 ms |
27748 KB |
Output is correct |
36 |
Correct |
507 ms |
70904 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
93 ms |
44828 KB |
Output is correct |
2 |
Correct |
88 ms |
44840 KB |
Output is correct |
3 |
Correct |
171 ms |
51288 KB |
Output is correct |
4 |
Correct |
190 ms |
52472 KB |
Output is correct |
5 |
Correct |
239 ms |
63092 KB |
Output is correct |
6 |
Correct |
272 ms |
62472 KB |
Output is correct |
7 |
Correct |
235 ms |
63024 KB |
Output is correct |
8 |
Correct |
240 ms |
63120 KB |
Output is correct |
9 |
Correct |
297 ms |
62900 KB |
Output is correct |
10 |
Correct |
153 ms |
34808 KB |
Output is correct |
11 |
Correct |
355 ms |
69440 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 |
212 KB |
Output is correct |
15 |
Correct |
1 ms |
212 KB |
Output is correct |
16 |
Correct |
1 ms |
296 KB |
Output is correct |
17 |
Correct |
0 ms |
212 KB |
Output is correct |
18 |
Correct |
93 ms |
44872 KB |
Output is correct |
19 |
Correct |
88 ms |
44820 KB |
Output is correct |
20 |
Correct |
98 ms |
44840 KB |
Output is correct |
21 |
Correct |
92 ms |
44776 KB |
Output is correct |
22 |
Correct |
336 ms |
69596 KB |
Output is correct |
23 |
Correct |
383 ms |
81660 KB |
Output is correct |
24 |
Correct |
394 ms |
82492 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
166 ms |
50980 KB |
Output is correct |
2 |
Correct |
197 ms |
57280 KB |
Output is correct |
3 |
Correct |
90 ms |
44828 KB |
Output is correct |
4 |
Correct |
86 ms |
44820 KB |
Output is correct |
5 |
Correct |
573 ms |
109300 KB |
Output is correct |
6 |
Correct |
660 ms |
123664 KB |
Output is correct |
7 |
Correct |
1 ms |
212 KB |
Output is correct |
8 |
Correct |
265 ms |
61596 KB |
Output is correct |
9 |
Correct |
317 ms |
69900 KB |
Output is correct |
10 |
Correct |
157 ms |
50992 KB |
Output is correct |
11 |
Correct |
183 ms |
57192 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 |
300 KB |
Output is correct |
15 |
Correct |
1 ms |
296 KB |
Output is correct |
16 |
Correct |
91 ms |
44820 KB |
Output is correct |
17 |
Correct |
93 ms |
44776 KB |
Output is correct |
18 |
Correct |
208 ms |
54140 KB |
Output is correct |
19 |
Correct |
234 ms |
61140 KB |
Output is correct |
20 |
Correct |
194 ms |
52680 KB |
Output is correct |
21 |
Correct |
210 ms |
56148 KB |
Output is correct |
22 |
Correct |
206 ms |
52768 KB |
Output is correct |
23 |
Correct |
226 ms |
57956 KB |
Output is correct |
24 |
Correct |
93 ms |
44828 KB |
Output is correct |
25 |
Correct |
88 ms |
44840 KB |
Output is correct |
26 |
Correct |
171 ms |
51288 KB |
Output is correct |
27 |
Correct |
190 ms |
52472 KB |
Output is correct |
28 |
Correct |
239 ms |
63092 KB |
Output is correct |
29 |
Correct |
272 ms |
62472 KB |
Output is correct |
30 |
Correct |
235 ms |
63024 KB |
Output is correct |
31 |
Correct |
240 ms |
63120 KB |
Output is correct |
32 |
Correct |
1 ms |
212 KB |
Output is correct |
33 |
Correct |
1 ms |
296 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 |
300 KB |
Output is correct |
37 |
Correct |
1 ms |
300 KB |
Output is correct |
38 |
Correct |
0 ms |
304 KB |
Output is correct |
39 |
Correct |
1 ms |
300 KB |
Output is correct |
40 |
Correct |
2 ms |
440 KB |
Output is correct |
41 |
Correct |
3 ms |
872 KB |
Output is correct |
42 |
Correct |
2 ms |
468 KB |
Output is correct |
43 |
Correct |
2 ms |
596 KB |
Output is correct |
44 |
Correct |
1 ms |
340 KB |
Output is correct |
45 |
Correct |
2 ms |
596 KB |
Output is correct |
46 |
Correct |
1 ms |
468 KB |
Output is correct |
47 |
Correct |
3 ms |
724 KB |
Output is correct |
48 |
Correct |
62 ms |
8636 KB |
Output is correct |
49 |
Correct |
54 ms |
9164 KB |
Output is correct |
50 |
Correct |
46 ms |
8984 KB |
Output is correct |
51 |
Correct |
47 ms |
8436 KB |
Output is correct |
52 |
Correct |
49 ms |
8364 KB |
Output is correct |
53 |
Correct |
99 ms |
16560 KB |
Output is correct |
54 |
Correct |
16 ms |
2616 KB |
Output is correct |
55 |
Correct |
47 ms |
6524 KB |
Output is correct |
56 |
Correct |
2 ms |
596 KB |
Output is correct |
57 |
Correct |
13 ms |
2516 KB |
Output is correct |
58 |
Correct |
6 ms |
2160 KB |
Output is correct |
59 |
Correct |
299 ms |
37880 KB |
Output is correct |
60 |
Correct |
405 ms |
52716 KB |
Output is correct |
61 |
Correct |
530 ms |
86500 KB |
Output is correct |
62 |
Correct |
539 ms |
85976 KB |
Output is correct |
63 |
Correct |
343 ms |
51536 KB |
Output is correct |
64 |
Correct |
538 ms |
86624 KB |
Output is correct |
65 |
Correct |
529 ms |
86460 KB |
Output is correct |
66 |
Correct |
169 ms |
27748 KB |
Output is correct |
67 |
Correct |
507 ms |
70904 KB |
Output is correct |
68 |
Correct |
297 ms |
62900 KB |
Output is correct |
69 |
Correct |
153 ms |
34808 KB |
Output is correct |
70 |
Correct |
355 ms |
69440 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 |
212 KB |
Output is correct |
74 |
Correct |
1 ms |
212 KB |
Output is correct |
75 |
Correct |
1 ms |
296 KB |
Output is correct |
76 |
Correct |
0 ms |
212 KB |
Output is correct |
77 |
Correct |
93 ms |
44872 KB |
Output is correct |
78 |
Correct |
88 ms |
44820 KB |
Output is correct |
79 |
Correct |
98 ms |
44840 KB |
Output is correct |
80 |
Correct |
92 ms |
44776 KB |
Output is correct |
81 |
Correct |
336 ms |
69596 KB |
Output is correct |
82 |
Correct |
383 ms |
81660 KB |
Output is correct |
83 |
Correct |
394 ms |
82492 KB |
Output is correct |
84 |
Correct |
679 ms |
95960 KB |
Output is correct |
85 |
Correct |
708 ms |
100876 KB |
Output is correct |
86 |
Correct |
616 ms |
114328 KB |
Output is correct |
87 |
Correct |
647 ms |
119904 KB |
Output is correct |
88 |
Correct |
1 ms |
212 KB |
Output is correct |
89 |
Correct |
682 ms |
119836 KB |
Output is correct |
90 |
Correct |
535 ms |
108732 KB |
Output is correct |
91 |
Correct |
401 ms |
98452 KB |
Output is correct |