#include "fish.h"
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
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);
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);
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 |
199 ms |
109252 KB |
Output is correct |
2 |
Correct |
242 ms |
124352 KB |
Output is correct |
3 |
Correct |
121 ms |
93824 KB |
Output is correct |
4 |
Correct |
123 ms |
93824 KB |
Output is correct |
5 |
Correct |
693 ms |
227120 KB |
Output is correct |
6 |
Correct |
628 ms |
226208 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
344 KB |
Output is correct |
2 |
Correct |
309 ms |
125156 KB |
Output is correct |
3 |
Correct |
360 ms |
141556 KB |
Output is correct |
4 |
Correct |
208 ms |
109256 KB |
Output is correct |
5 |
Correct |
240 ms |
124360 KB |
Output is correct |
6 |
Correct |
0 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 |
117 ms |
93780 KB |
Output is correct |
11 |
Correct |
116 ms |
93964 KB |
Output is correct |
12 |
Correct |
241 ms |
120504 KB |
Output is correct |
13 |
Correct |
290 ms |
138184 KB |
Output is correct |
14 |
Correct |
221 ms |
115012 KB |
Output is correct |
15 |
Correct |
232 ms |
116680 KB |
Output is correct |
16 |
Correct |
222 ms |
114804 KB |
Output is correct |
17 |
Correct |
253 ms |
127552 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
117 ms |
93716 KB |
Output is correct |
2 |
Correct |
115 ms |
93860 KB |
Output is correct |
3 |
Correct |
131 ms |
92244 KB |
Output is correct |
4 |
Correct |
146 ms |
99664 KB |
Output is correct |
5 |
Correct |
165 ms |
107088 KB |
Output is correct |
6 |
Correct |
157 ms |
107096 KB |
Output is correct |
7 |
Correct |
160 ms |
107064 KB |
Output is correct |
8 |
Correct |
154 ms |
107156 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 |
348 KB |
Output is correct |
6 |
Correct |
0 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 |
604 KB |
Output is correct |
10 |
Correct |
1 ms |
968 KB |
Output is correct |
11 |
Correct |
1 ms |
604 KB |
Output is correct |
12 |
Correct |
1 ms |
860 KB |
Output is correct |
13 |
Correct |
0 ms |
348 KB |
Output is correct |
14 |
Correct |
1 ms |
856 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 |
348 KB |
Output is correct |
6 |
Correct |
0 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 |
604 KB |
Output is correct |
10 |
Correct |
1 ms |
968 KB |
Output is correct |
11 |
Correct |
1 ms |
604 KB |
Output is correct |
12 |
Correct |
1 ms |
860 KB |
Output is correct |
13 |
Correct |
0 ms |
348 KB |
Output is correct |
14 |
Correct |
1 ms |
856 KB |
Output is correct |
15 |
Correct |
1 ms |
604 KB |
Output is correct |
16 |
Correct |
2 ms |
860 KB |
Output is correct |
17 |
Correct |
34 ms |
10644 KB |
Output is correct |
18 |
Correct |
30 ms |
9420 KB |
Output is correct |
19 |
Correct |
31 ms |
9672 KB |
Output is correct |
20 |
Correct |
26 ms |
8560 KB |
Output is correct |
21 |
Correct |
28 ms |
8540 KB |
Output is correct |
22 |
Correct |
55 ms |
16440 KB |
Output is correct |
23 |
Correct |
10 ms |
4188 KB |
Output is correct |
24 |
Correct |
31 ms |
10424 KB |
Output is correct |
25 |
Correct |
2 ms |
860 KB |
Output is correct |
26 |
Correct |
10 ms |
3672 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 |
348 KB |
Output is correct |
6 |
Correct |
0 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 |
604 KB |
Output is correct |
10 |
Correct |
1 ms |
968 KB |
Output is correct |
11 |
Correct |
1 ms |
604 KB |
Output is correct |
12 |
Correct |
1 ms |
860 KB |
Output is correct |
13 |
Correct |
0 ms |
348 KB |
Output is correct |
14 |
Correct |
1 ms |
856 KB |
Output is correct |
15 |
Correct |
1 ms |
604 KB |
Output is correct |
16 |
Correct |
2 ms |
860 KB |
Output is correct |
17 |
Correct |
34 ms |
10644 KB |
Output is correct |
18 |
Correct |
30 ms |
9420 KB |
Output is correct |
19 |
Correct |
31 ms |
9672 KB |
Output is correct |
20 |
Correct |
26 ms |
8560 KB |
Output is correct |
21 |
Correct |
28 ms |
8540 KB |
Output is correct |
22 |
Correct |
55 ms |
16440 KB |
Output is correct |
23 |
Correct |
10 ms |
4188 KB |
Output is correct |
24 |
Correct |
31 ms |
10424 KB |
Output is correct |
25 |
Correct |
2 ms |
860 KB |
Output is correct |
26 |
Correct |
10 ms |
3672 KB |
Output is correct |
27 |
Correct |
4 ms |
4188 KB |
Output is correct |
28 |
Correct |
204 ms |
46572 KB |
Output is correct |
29 |
Correct |
370 ms |
81016 KB |
Output is correct |
30 |
Correct |
468 ms |
131960 KB |
Output is correct |
31 |
Correct |
474 ms |
130896 KB |
Output is correct |
32 |
Correct |
226 ms |
54356 KB |
Output is correct |
33 |
Correct |
472 ms |
134740 KB |
Output is correct |
34 |
Correct |
474 ms |
134772 KB |
Output is correct |
35 |
Correct |
128 ms |
42380 KB |
Output is correct |
36 |
Correct |
448 ms |
115800 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
117 ms |
93716 KB |
Output is correct |
2 |
Correct |
115 ms |
93860 KB |
Output is correct |
3 |
Correct |
131 ms |
92244 KB |
Output is correct |
4 |
Correct |
146 ms |
99664 KB |
Output is correct |
5 |
Correct |
165 ms |
107088 KB |
Output is correct |
6 |
Correct |
157 ms |
107096 KB |
Output is correct |
7 |
Correct |
160 ms |
107064 KB |
Output is correct |
8 |
Correct |
154 ms |
107156 KB |
Output is correct |
9 |
Correct |
236 ms |
133712 KB |
Output is correct |
10 |
Correct |
112 ms |
63824 KB |
Output is correct |
11 |
Correct |
242 ms |
127204 KB |
Output is correct |
12 |
Correct |
0 ms |
344 KB |
Output is correct |
13 |
Correct |
1 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 |
0 ms |
348 KB |
Output is correct |
17 |
Correct |
0 ms |
348 KB |
Output is correct |
18 |
Correct |
114 ms |
93780 KB |
Output is correct |
19 |
Correct |
119 ms |
93780 KB |
Output is correct |
20 |
Correct |
125 ms |
93992 KB |
Output is correct |
21 |
Correct |
115 ms |
93788 KB |
Output is correct |
22 |
Correct |
305 ms |
135968 KB |
Output is correct |
23 |
Correct |
383 ms |
151124 KB |
Output is correct |
24 |
Correct |
386 ms |
153940 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
199 ms |
109252 KB |
Output is correct |
2 |
Correct |
242 ms |
124352 KB |
Output is correct |
3 |
Correct |
121 ms |
93824 KB |
Output is correct |
4 |
Correct |
123 ms |
93824 KB |
Output is correct |
5 |
Correct |
693 ms |
227120 KB |
Output is correct |
6 |
Correct |
628 ms |
226208 KB |
Output is correct |
7 |
Correct |
1 ms |
344 KB |
Output is correct |
8 |
Correct |
309 ms |
125156 KB |
Output is correct |
9 |
Correct |
360 ms |
141556 KB |
Output is correct |
10 |
Correct |
208 ms |
109256 KB |
Output is correct |
11 |
Correct |
240 ms |
124360 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 |
348 KB |
Output is correct |
16 |
Correct |
117 ms |
93780 KB |
Output is correct |
17 |
Correct |
116 ms |
93964 KB |
Output is correct |
18 |
Correct |
241 ms |
120504 KB |
Output is correct |
19 |
Correct |
290 ms |
138184 KB |
Output is correct |
20 |
Correct |
221 ms |
115012 KB |
Output is correct |
21 |
Correct |
232 ms |
116680 KB |
Output is correct |
22 |
Correct |
222 ms |
114804 KB |
Output is correct |
23 |
Correct |
253 ms |
127552 KB |
Output is correct |
24 |
Correct |
117 ms |
93716 KB |
Output is correct |
25 |
Correct |
115 ms |
93860 KB |
Output is correct |
26 |
Correct |
131 ms |
92244 KB |
Output is correct |
27 |
Correct |
146 ms |
99664 KB |
Output is correct |
28 |
Correct |
165 ms |
107088 KB |
Output is correct |
29 |
Correct |
157 ms |
107096 KB |
Output is correct |
30 |
Correct |
160 ms |
107064 KB |
Output is correct |
31 |
Correct |
154 ms |
107156 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 |
348 KB |
Output is correct |
37 |
Correct |
0 ms |
348 KB |
Output is correct |
38 |
Correct |
0 ms |
348 KB |
Output is correct |
39 |
Correct |
0 ms |
348 KB |
Output is correct |
40 |
Correct |
0 ms |
604 KB |
Output is correct |
41 |
Correct |
1 ms |
968 KB |
Output is correct |
42 |
Correct |
1 ms |
604 KB |
Output is correct |
43 |
Correct |
1 ms |
860 KB |
Output is correct |
44 |
Correct |
0 ms |
348 KB |
Output is correct |
45 |
Correct |
1 ms |
856 KB |
Output is correct |
46 |
Correct |
1 ms |
604 KB |
Output is correct |
47 |
Correct |
2 ms |
860 KB |
Output is correct |
48 |
Correct |
34 ms |
10644 KB |
Output is correct |
49 |
Correct |
30 ms |
9420 KB |
Output is correct |
50 |
Correct |
31 ms |
9672 KB |
Output is correct |
51 |
Correct |
26 ms |
8560 KB |
Output is correct |
52 |
Correct |
28 ms |
8540 KB |
Output is correct |
53 |
Correct |
55 ms |
16440 KB |
Output is correct |
54 |
Correct |
10 ms |
4188 KB |
Output is correct |
55 |
Correct |
31 ms |
10424 KB |
Output is correct |
56 |
Correct |
2 ms |
860 KB |
Output is correct |
57 |
Correct |
10 ms |
3672 KB |
Output is correct |
58 |
Correct |
4 ms |
4188 KB |
Output is correct |
59 |
Correct |
204 ms |
46572 KB |
Output is correct |
60 |
Correct |
370 ms |
81016 KB |
Output is correct |
61 |
Correct |
468 ms |
131960 KB |
Output is correct |
62 |
Correct |
474 ms |
130896 KB |
Output is correct |
63 |
Correct |
226 ms |
54356 KB |
Output is correct |
64 |
Correct |
472 ms |
134740 KB |
Output is correct |
65 |
Correct |
474 ms |
134772 KB |
Output is correct |
66 |
Correct |
128 ms |
42380 KB |
Output is correct |
67 |
Correct |
448 ms |
115800 KB |
Output is correct |
68 |
Correct |
236 ms |
133712 KB |
Output is correct |
69 |
Correct |
112 ms |
63824 KB |
Output is correct |
70 |
Correct |
242 ms |
127204 KB |
Output is correct |
71 |
Correct |
0 ms |
344 KB |
Output is correct |
72 |
Correct |
1 ms |
348 KB |
Output is correct |
73 |
Correct |
0 ms |
348 KB |
Output is correct |
74 |
Correct |
0 ms |
348 KB |
Output is correct |
75 |
Correct |
0 ms |
348 KB |
Output is correct |
76 |
Correct |
0 ms |
348 KB |
Output is correct |
77 |
Correct |
114 ms |
93780 KB |
Output is correct |
78 |
Correct |
119 ms |
93780 KB |
Output is correct |
79 |
Correct |
125 ms |
93992 KB |
Output is correct |
80 |
Correct |
115 ms |
93788 KB |
Output is correct |
81 |
Correct |
305 ms |
135968 KB |
Output is correct |
82 |
Correct |
383 ms |
151124 KB |
Output is correct |
83 |
Correct |
386 ms |
153940 KB |
Output is correct |
84 |
Correct |
646 ms |
202076 KB |
Output is correct |
85 |
Correct |
677 ms |
210376 KB |
Output is correct |
86 |
Correct |
618 ms |
215744 KB |
Output is correct |
87 |
Correct |
661 ms |
226132 KB |
Output is correct |
88 |
Correct |
0 ms |
344 KB |
Output is correct |
89 |
Correct |
644 ms |
226132 KB |
Output is correct |
90 |
Correct |
512 ms |
194660 KB |
Output is correct |
91 |
Correct |
372 ms |
157820 KB |
Output is correct |