#include "fish.h"
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
#pragma GCC optimize("O3,unroll-loops")
#pragma GCC target("avx,bmi,bmi2,lzcnt,popcnt")
ll max_weights(int N, int M, vector<int> X, vector<int> Y, vector<int> W) {
for(int i=0; i<M; i++) X[i]++, Y[i]++;
map<int, int> mp[N+5];
for(int i=0; i<M; i++) mp[X[i]][Y[i]] = W[i];
ll ans = 0;
vector<pair<int, int> > vec[N+5];
vector<int> pos[N+5], SZ(N+5);
for(int i=1; i<=N; i++) vec[i].push_back({ 0, 0 });
for(int i=0; i<M; i++) vec[X[i]].push_back({ Y[i], W[i] });
for(int i=1; i<=N; i++) {
set<int> st;
for(int j=-1; j<=1; j++) for(auto &x : vec[i-j]) st.insert(x.first);
for(auto &x : st) pos[i].push_back(x);
sort(vec[i].begin(), vec[i].end());
}
vector<vector<ll> > pref(N+5), pmx1(N+5), smx1(N+5), pmx2(N+5), smx2(N+5);
vector<vector<vector<ll> > > dp(N+5);
for(int i=N; i>=1; i--) {
SZ[i] = pos[i].size() - 1;
pref[i].resize(SZ[i] + 10);
pmx1[i].resize(SZ[i] + 10);
smx1[i].resize(SZ[i] + 10);
pmx2[i].resize(SZ[i] + 10);
smx2[i].resize(SZ[i] + 10);
dp[i].resize(SZ[i] + 5, vector<ll>(2));
if(i == 1) {
pmx1[i][0] = dp[i][0][1] - pref[i][0];
for(int j=1; j<=SZ[i]; j++) pmx1[i][j] = max(pmx1[i][j-1], dp[i][j][1] - pref[i][j]);
for(int j=SZ[i]; j>=0; j--) {
int p = upper_bound(pos[i+1].begin(), pos[i+1].end(), pos[i][j]) - pos[i+1].begin() - 1;
smx1[i][j] = pref[i+1][p];
}
for(int j=SZ[i]-1; j>=0; j--) smx1[i][j] = max(smx1[i][j], smx1[i][j+1]);
}
for(int j=1; j<=SZ[i]; j++) pref[i][j] = pref[i][j-1] + mp[i][pos[i][j]];
}
for(int i=2; i<=N; i++) {
int ptr = 0, ptr2 = 0;
for(int j=0; j<=SZ[i]; j++) {
int p = upper_bound(pos[i-1].begin(), pos[i-1].end(), pos[i][j]) - pos[i-1].begin() - 1;
dp[i][j][1] = max(dp[i][j][1], pmx1[i-1][p] + pref[i-1][p]);
while(ptr + 1 <= SZ[i-1] && pos[i-1][ptr] < pos[i][j]) ptr++;
if(pos[i-1][ptr] >= pos[i][j]) dp[i][j][0] = max(dp[i][j][0], smx1[i-1][ptr] - pref[i][j]);
if(i < 3) continue;
int pa = upper_bound(pos[i-2].begin(), pos[i-2].end(), pos[i][j]) - pos[i-2].begin() - 1;
int pb = upper_bound(pos[i-1].begin(), pos[i-1].end(), pos[i][j]) - pos[i-1].begin() - 1;
dp[i][j][0] = max(dp[i][j][0], pmx2[i-2][pa] + pref[i-1][pb]);
while(ptr2 + 1 <= SZ[i-2] && pos[i-2][ptr2] < pos[i][j]) ptr2++;
if(pos[i-2][ptr2] >= pos[i][j]) dp[i][j][1] = max(dp[i][j][1], smx1[i-2][ptr2]);
}
pmx1[i][0] = dp[i][0][1] - pref[i][0];
pmx2[i][0] = max(dp[i][0][0], dp[i][0][1]);
for(int j=1; j<=SZ[i]; j++) {
pmx1[i][j] = max(pmx1[i][j-1], dp[i][j][1] - pref[i][j]);
pmx2[i][j] = max(pmx2[i][j-1], max(dp[i][j][0], dp[i][j][1]));
}
if(i == N) continue;
for(int j=SZ[i]; j>=0; j--) {
int p = upper_bound(pos[i+1].begin(), pos[i+1].end(), pos[i][j]) - pos[i+1].begin() - 1;
smx1[i][j] = max(dp[i][j][0], dp[i][j][1]) + pref[i+1][p];
}
for(int j=SZ[i]-1; j>=0; j--) smx1[i][j] = max(smx1[i][j], smx1[i][j+1]);
}
for(int i=1; i<=N; i++) {
for(int j=0; j<=SZ[i]; j++) {
if(pref[i+1].empty() || j == 0) ans = max({ ans, dp[i][j][0], dp[i][j][1] });
else {
int p = upper_bound(pos[i+1].begin(), pos[i+1].end(), pos[i][j]) - pos[i+1].begin() - 1;
ans = max(ans, max(dp[i][j][0], dp[i][j][1]) + pref[i+1][p]);
}
}
}
return ans;
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
218 ms |
121028 KB |
Output is correct |
2 |
Correct |
255 ms |
137672 KB |
Output is correct |
3 |
Correct |
127 ms |
105552 KB |
Output is correct |
4 |
Correct |
125 ms |
105604 KB |
Output is correct |
5 |
Correct |
783 ms |
245812 KB |
Output is correct |
6 |
Correct |
679 ms |
251356 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
348 KB |
Output is correct |
2 |
Correct |
341 ms |
137864 KB |
Output is correct |
3 |
Correct |
389 ms |
158904 KB |
Output is correct |
4 |
Correct |
226 ms |
122308 KB |
Output is correct |
5 |
Correct |
257 ms |
139460 KB |
Output is correct |
6 |
Correct |
1 ms |
348 KB |
Output is correct |
7 |
Correct |
0 ms |
348 KB |
Output is correct |
8 |
Correct |
0 ms |
348 KB |
Output is correct |
9 |
Correct |
0 ms |
348 KB |
Output is correct |
10 |
Correct |
125 ms |
105496 KB |
Output is correct |
11 |
Correct |
132 ms |
105552 KB |
Output is correct |
12 |
Correct |
260 ms |
134192 KB |
Output is correct |
13 |
Correct |
332 ms |
154032 KB |
Output is correct |
14 |
Correct |
242 ms |
128864 KB |
Output is correct |
15 |
Correct |
240 ms |
131020 KB |
Output is correct |
16 |
Correct |
247 ms |
128460 KB |
Output is correct |
17 |
Correct |
259 ms |
142532 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
124 ms |
105668 KB |
Output is correct |
2 |
Correct |
118 ms |
105636 KB |
Output is correct |
3 |
Correct |
157 ms |
102956 KB |
Output is correct |
4 |
Correct |
160 ms |
111356 KB |
Output is correct |
5 |
Correct |
164 ms |
118868 KB |
Output is correct |
6 |
Correct |
169 ms |
118912 KB |
Output is correct |
7 |
Correct |
169 ms |
118864 KB |
Output is correct |
8 |
Correct |
175 ms |
118864 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
348 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
0 ms |
348 KB |
Output is correct |
4 |
Correct |
0 ms |
348 KB |
Output is correct |
5 |
Correct |
0 ms |
344 KB |
Output is correct |
6 |
Correct |
0 ms |
348 KB |
Output is correct |
7 |
Correct |
0 ms |
348 KB |
Output is correct |
8 |
Correct |
1 ms |
348 KB |
Output is correct |
9 |
Correct |
1 ms |
604 KB |
Output is correct |
10 |
Correct |
1 ms |
1188 KB |
Output is correct |
11 |
Correct |
1 ms |
604 KB |
Output is correct |
12 |
Correct |
2 ms |
860 KB |
Output is correct |
13 |
Correct |
0 ms |
348 KB |
Output is correct |
14 |
Correct |
1 ms |
860 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
348 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
0 ms |
348 KB |
Output is correct |
4 |
Correct |
0 ms |
348 KB |
Output is correct |
5 |
Correct |
0 ms |
344 KB |
Output is correct |
6 |
Correct |
0 ms |
348 KB |
Output is correct |
7 |
Correct |
0 ms |
348 KB |
Output is correct |
8 |
Correct |
1 ms |
348 KB |
Output is correct |
9 |
Correct |
1 ms |
604 KB |
Output is correct |
10 |
Correct |
1 ms |
1188 KB |
Output is correct |
11 |
Correct |
1 ms |
604 KB |
Output is correct |
12 |
Correct |
2 ms |
860 KB |
Output is correct |
13 |
Correct |
0 ms |
348 KB |
Output is correct |
14 |
Correct |
1 ms |
860 KB |
Output is correct |
15 |
Correct |
1 ms |
860 KB |
Output is correct |
16 |
Correct |
2 ms |
860 KB |
Output is correct |
17 |
Correct |
38 ms |
11204 KB |
Output is correct |
18 |
Correct |
32 ms |
9756 KB |
Output is correct |
19 |
Correct |
32 ms |
10068 KB |
Output is correct |
20 |
Correct |
29 ms |
9048 KB |
Output is correct |
21 |
Correct |
30 ms |
8788 KB |
Output is correct |
22 |
Correct |
61 ms |
17236 KB |
Output is correct |
23 |
Correct |
11 ms |
4440 KB |
Output is correct |
24 |
Correct |
46 ms |
10844 KB |
Output is correct |
25 |
Correct |
1 ms |
1112 KB |
Output is correct |
26 |
Correct |
9 ms |
3856 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
348 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
0 ms |
348 KB |
Output is correct |
4 |
Correct |
0 ms |
348 KB |
Output is correct |
5 |
Correct |
0 ms |
344 KB |
Output is correct |
6 |
Correct |
0 ms |
348 KB |
Output is correct |
7 |
Correct |
0 ms |
348 KB |
Output is correct |
8 |
Correct |
1 ms |
348 KB |
Output is correct |
9 |
Correct |
1 ms |
604 KB |
Output is correct |
10 |
Correct |
1 ms |
1188 KB |
Output is correct |
11 |
Correct |
1 ms |
604 KB |
Output is correct |
12 |
Correct |
2 ms |
860 KB |
Output is correct |
13 |
Correct |
0 ms |
348 KB |
Output is correct |
14 |
Correct |
1 ms |
860 KB |
Output is correct |
15 |
Correct |
1 ms |
860 KB |
Output is correct |
16 |
Correct |
2 ms |
860 KB |
Output is correct |
17 |
Correct |
38 ms |
11204 KB |
Output is correct |
18 |
Correct |
32 ms |
9756 KB |
Output is correct |
19 |
Correct |
32 ms |
10068 KB |
Output is correct |
20 |
Correct |
29 ms |
9048 KB |
Output is correct |
21 |
Correct |
30 ms |
8788 KB |
Output is correct |
22 |
Correct |
61 ms |
17236 KB |
Output is correct |
23 |
Correct |
11 ms |
4440 KB |
Output is correct |
24 |
Correct |
46 ms |
10844 KB |
Output is correct |
25 |
Correct |
1 ms |
1112 KB |
Output is correct |
26 |
Correct |
9 ms |
3856 KB |
Output is correct |
27 |
Correct |
5 ms |
4700 KB |
Output is correct |
28 |
Correct |
241 ms |
48576 KB |
Output is correct |
29 |
Correct |
385 ms |
90708 KB |
Output is correct |
30 |
Correct |
489 ms |
144468 KB |
Output is correct |
31 |
Correct |
486 ms |
143444 KB |
Output is correct |
32 |
Correct |
249 ms |
61724 KB |
Output is correct |
33 |
Correct |
508 ms |
147796 KB |
Output is correct |
34 |
Correct |
507 ms |
147536 KB |
Output is correct |
35 |
Correct |
141 ms |
46612 KB |
Output is correct |
36 |
Correct |
490 ms |
127632 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
124 ms |
105668 KB |
Output is correct |
2 |
Correct |
118 ms |
105636 KB |
Output is correct |
3 |
Correct |
157 ms |
102956 KB |
Output is correct |
4 |
Correct |
160 ms |
111356 KB |
Output is correct |
5 |
Correct |
164 ms |
118868 KB |
Output is correct |
6 |
Correct |
169 ms |
118912 KB |
Output is correct |
7 |
Correct |
169 ms |
118864 KB |
Output is correct |
8 |
Correct |
175 ms |
118864 KB |
Output is correct |
9 |
Correct |
241 ms |
147024 KB |
Output is correct |
10 |
Correct |
122 ms |
70484 KB |
Output is correct |
11 |
Correct |
256 ms |
140624 KB |
Output is correct |
12 |
Correct |
0 ms |
348 KB |
Output is correct |
13 |
Correct |
0 ms |
348 KB |
Output is correct |
14 |
Correct |
0 ms |
348 KB |
Output is correct |
15 |
Correct |
0 ms |
344 KB |
Output is correct |
16 |
Correct |
0 ms |
348 KB |
Output is correct |
17 |
Correct |
0 ms |
348 KB |
Output is correct |
18 |
Correct |
128 ms |
105548 KB |
Output is correct |
19 |
Correct |
124 ms |
105520 KB |
Output is correct |
20 |
Correct |
115 ms |
105556 KB |
Output is correct |
21 |
Correct |
122 ms |
105936 KB |
Output is correct |
22 |
Correct |
322 ms |
149840 KB |
Output is correct |
23 |
Correct |
394 ms |
165456 KB |
Output is correct |
24 |
Correct |
395 ms |
168276 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
218 ms |
121028 KB |
Output is correct |
2 |
Correct |
255 ms |
137672 KB |
Output is correct |
3 |
Correct |
127 ms |
105552 KB |
Output is correct |
4 |
Correct |
125 ms |
105604 KB |
Output is correct |
5 |
Correct |
783 ms |
245812 KB |
Output is correct |
6 |
Correct |
679 ms |
251356 KB |
Output is correct |
7 |
Correct |
1 ms |
348 KB |
Output is correct |
8 |
Correct |
341 ms |
137864 KB |
Output is correct |
9 |
Correct |
389 ms |
158904 KB |
Output is correct |
10 |
Correct |
226 ms |
122308 KB |
Output is correct |
11 |
Correct |
257 ms |
139460 KB |
Output is correct |
12 |
Correct |
1 ms |
348 KB |
Output is correct |
13 |
Correct |
0 ms |
348 KB |
Output is correct |
14 |
Correct |
0 ms |
348 KB |
Output is correct |
15 |
Correct |
0 ms |
348 KB |
Output is correct |
16 |
Correct |
125 ms |
105496 KB |
Output is correct |
17 |
Correct |
132 ms |
105552 KB |
Output is correct |
18 |
Correct |
260 ms |
134192 KB |
Output is correct |
19 |
Correct |
332 ms |
154032 KB |
Output is correct |
20 |
Correct |
242 ms |
128864 KB |
Output is correct |
21 |
Correct |
240 ms |
131020 KB |
Output is correct |
22 |
Correct |
247 ms |
128460 KB |
Output is correct |
23 |
Correct |
259 ms |
142532 KB |
Output is correct |
24 |
Correct |
124 ms |
105668 KB |
Output is correct |
25 |
Correct |
118 ms |
105636 KB |
Output is correct |
26 |
Correct |
157 ms |
102956 KB |
Output is correct |
27 |
Correct |
160 ms |
111356 KB |
Output is correct |
28 |
Correct |
164 ms |
118868 KB |
Output is correct |
29 |
Correct |
169 ms |
118912 KB |
Output is correct |
30 |
Correct |
169 ms |
118864 KB |
Output is correct |
31 |
Correct |
175 ms |
118864 KB |
Output is correct |
32 |
Correct |
0 ms |
348 KB |
Output is correct |
33 |
Correct |
0 ms |
348 KB |
Output is correct |
34 |
Correct |
0 ms |
348 KB |
Output is correct |
35 |
Correct |
0 ms |
348 KB |
Output is correct |
36 |
Correct |
0 ms |
344 KB |
Output is correct |
37 |
Correct |
0 ms |
348 KB |
Output is correct |
38 |
Correct |
0 ms |
348 KB |
Output is correct |
39 |
Correct |
1 ms |
348 KB |
Output is correct |
40 |
Correct |
1 ms |
604 KB |
Output is correct |
41 |
Correct |
1 ms |
1188 KB |
Output is correct |
42 |
Correct |
1 ms |
604 KB |
Output is correct |
43 |
Correct |
2 ms |
860 KB |
Output is correct |
44 |
Correct |
0 ms |
348 KB |
Output is correct |
45 |
Correct |
1 ms |
860 KB |
Output is correct |
46 |
Correct |
1 ms |
860 KB |
Output is correct |
47 |
Correct |
2 ms |
860 KB |
Output is correct |
48 |
Correct |
38 ms |
11204 KB |
Output is correct |
49 |
Correct |
32 ms |
9756 KB |
Output is correct |
50 |
Correct |
32 ms |
10068 KB |
Output is correct |
51 |
Correct |
29 ms |
9048 KB |
Output is correct |
52 |
Correct |
30 ms |
8788 KB |
Output is correct |
53 |
Correct |
61 ms |
17236 KB |
Output is correct |
54 |
Correct |
11 ms |
4440 KB |
Output is correct |
55 |
Correct |
46 ms |
10844 KB |
Output is correct |
56 |
Correct |
1 ms |
1112 KB |
Output is correct |
57 |
Correct |
9 ms |
3856 KB |
Output is correct |
58 |
Correct |
5 ms |
4700 KB |
Output is correct |
59 |
Correct |
241 ms |
48576 KB |
Output is correct |
60 |
Correct |
385 ms |
90708 KB |
Output is correct |
61 |
Correct |
489 ms |
144468 KB |
Output is correct |
62 |
Correct |
486 ms |
143444 KB |
Output is correct |
63 |
Correct |
249 ms |
61724 KB |
Output is correct |
64 |
Correct |
508 ms |
147796 KB |
Output is correct |
65 |
Correct |
507 ms |
147536 KB |
Output is correct |
66 |
Correct |
141 ms |
46612 KB |
Output is correct |
67 |
Correct |
490 ms |
127632 KB |
Output is correct |
68 |
Correct |
241 ms |
147024 KB |
Output is correct |
69 |
Correct |
122 ms |
70484 KB |
Output is correct |
70 |
Correct |
256 ms |
140624 KB |
Output is correct |
71 |
Correct |
0 ms |
348 KB |
Output is correct |
72 |
Correct |
0 ms |
348 KB |
Output is correct |
73 |
Correct |
0 ms |
348 KB |
Output is correct |
74 |
Correct |
0 ms |
344 KB |
Output is correct |
75 |
Correct |
0 ms |
348 KB |
Output is correct |
76 |
Correct |
0 ms |
348 KB |
Output is correct |
77 |
Correct |
128 ms |
105548 KB |
Output is correct |
78 |
Correct |
124 ms |
105520 KB |
Output is correct |
79 |
Correct |
115 ms |
105556 KB |
Output is correct |
80 |
Correct |
122 ms |
105936 KB |
Output is correct |
81 |
Correct |
322 ms |
149840 KB |
Output is correct |
82 |
Correct |
394 ms |
165456 KB |
Output is correct |
83 |
Correct |
395 ms |
168276 KB |
Output is correct |
84 |
Correct |
681 ms |
224724 KB |
Output is correct |
85 |
Correct |
727 ms |
233672 KB |
Output is correct |
86 |
Correct |
648 ms |
239060 KB |
Output is correct |
87 |
Correct |
688 ms |
250800 KB |
Output is correct |
88 |
Correct |
1 ms |
348 KB |
Output is correct |
89 |
Correct |
667 ms |
250712 KB |
Output is correct |
90 |
Correct |
512 ms |
216804 KB |
Output is correct |
91 |
Correct |
396 ms |
178004 KB |
Output is correct |