#include "fish.h"
#include "bits/stdc++.h"
using namespace std;
#ifndef EVAL
#include "grader.cpp"
#endif
#define ar array
typedef int32_t ii;
typedef long long ll;
#define int ll
/*
5 4
0 2 5
1 1 2
4 4 1
3 3 3
*/
const int INF = 1e18;
int max_weights(ii n, ii m, vector<ii> x, vector<ii> y, vector<ii> w) {
vector<vector<ar<int, 2>>> a(n + 1);
for(int i=0;i<m;i++){
x[i]++, y[i]++;
for(int t=-1;t<2;t++){
if(0 < x[i] + t && x[i] + t <= n){
a[x[i] + t].push_back({y[i], (t == 0) * w[i]});
}
}
}
vector<vector<ar<int, 2>>> dp(n + 1);
vector<vector<int>> pref(n + 2);
vector<vector<int>> tp(n + 1);
for(int i=0;i<=n;i++){
a[i].push_back({0, 0});
sort(a[i].begin(), a[i].end());
vector<ar<int, 2>> b;
for(auto [x, w] : a[i]){
if(b.empty() || b.back()[0] != x) b.push_back({x, 0});
b.back()[1] += w;
}
swap(a[i], b);
pref[i].resize(a[i].size());
dp[i].resize(a[i].size());
tp[i].resize(a[i].size());
for(int j=1;j<(int)pref[i].size();j++){
pref[i][j] = pref[i][j-1] + a[i][j][1];
}
}
//~ auto low = [&](int i, int j){
//~ int p = upper_bound(a[i].begin(), a[i].end(), (ar<int, 2>){j, INF}) - a[i].begin();
//~ return p - 1;
//~ };
//~ auto upp = [&](int i, int j){
//~ int p = lower_bound(a[i].begin(), a[i].end(), (ar<int, 2>){j, 0ll}) - a[i].begin();
//~ return p - 1;
//~ };
vector<int> pref_tp, suff_tp, pref_00, suff_11, suff_01;
{
pref_tp.resize(a[1].size());
pref_00.resize(a[1].size());
suff_tp.resize(a[1].size());
suff_01.resize(a[1].size());
suff_11.resize(a[1].size());
}
vector<int> mx(n + 1);
/*
5 4
0 2 5
1 1 2
4 4 1
3 3 3
*/
for(int i=1;i<=n;i++){
int j_ = 0;
for(int j=1;j<(int)a[i].size();j++){ // int j_ = a[i][j][0];
while(j_ + 1 < (int)a[i - 1].size() && a[i - 1][j_ + 1][0] <= a[i][j][0]) j_++;
dp[i][j][0] = pref[i - 1][j_];
if(3 <= i) dp[i][j][0] += mx[i - 3];
if(i > 1){
dp[i][j][0] = max(dp[i][j][0], pref_00[j] + pref[i - 1][j_]);
dp[i][j][1] = max(dp[i][j][1], suff_11[j] - pref[i][j]);
dp[i][j][1] = max(dp[i][j][1], suff_01[j] - pref[i][j]);
}
if(i > 2){
dp[i][j][0] = max(dp[i][j][0], pref_tp[j] + pref[i - 1][j_]);
dp[i][j][0] = max(dp[i][j][0], suff_tp[j]);
}
tp[i][j] = max(dp[i][j][0], dp[i][j][1]);
mx[i] = max(mx[i], tp[i][j]);
}
//~ cout<<"here"<<endl;
if(i < n){
pref_00.resize(a[i + 1].size());
pref_tp.resize(a[i + 1].size());
suff_tp.resize(a[i + 1].size() + 1);
suff_11.resize(a[i + 1].size() + 1);
suff_01.resize(a[i + 1].size() + 1);
int j_ = 1;
for(int j=1;j<(int)a[i + 1].size();j++){
pref_00[j] = pref_00[j - 1];
while(j_ < (int)a[i].size() && a[i][j_][0] <= a[i + 1][j][0]){
pref_00[j] = max(pref_00[j], dp[i][j_][0] - pref[i][j_]);
j_++;
}
//~ pref_00[j] = max(pref_00[j], dp[i][j][0] - pref[i][j]);
}
{
int j_ = (int)a[i].size() - 1, _j = (int)a[i + 1].size() - 1;
for(int j=_j;j>0;j--){
suff_11[j] = suff_11[j + 1];
while(j_ >= 0 && a[i][j_][0] > a[i + 1][j][0]){
while(a[i][j_][0] < a[i + 1][_j][0]) _j--;
suff_11[j] = max(suff_11[j], dp[i][j_][1] + pref[i + 1][_j]);
j_--;
}
while(a[i][j_][0] < a[i + 1][_j][0]) _j--;
suff_11[j] = max(suff_11[j], dp[i][j_][1] + pref[i + 1][_j]);
}
j_ = (int)a[i].size() - 1, _j = (int)a[i + 1].size() - 1;
for(int j=_j;j>0;j--){
suff_01[j] = suff_01[j + 1];
while(j_ >= 0 && a[i][j_][0] > a[i + 1][j][0] + 1){
while(a[i][j_][0] < a[i + 1][_j][0]) _j--;
suff_01[j] = max(suff_01[j], dp[i][j_][0] + pref[i + 1][_j]);
j_--;
}
while(a[i][j_][0] < a[i + 1][_j][0]) _j--;
suff_01[j] = max(suff_01[j], dp[i][j_][0] + pref[i + 1][_j]);
}
}
if(1 < i){
int j_ = 1;
for(int j=1;j<(int)a[i + 1].size();j++){
pref_tp[j] = pref_tp[j - 1];
while(j_ < (int)a[i - 1].size() && a[i - 1][j_][0] <= a[i + 1][j][0]){
pref_tp[j] = max(pref_tp[j], tp[i - 1][j_]);
j_++;
}
}
{
int j_ = (int)a[i - 1].size() - 1, _j = (int)a[i].size() - 1;
for(int j = (int)a[i + 1].size() - 1;j>0;j--){
suff_tp[j] = suff_tp[j + 1];
while(j_ >= 0 && a[i - 1][j_][0] > a[i + 1][j][0]){
while(a[i - 1][j_][0] < a[i][_j][0]) _j--;
suff_tp[j] = max(suff_tp[j], tp[i - 1][j_] + pref[i][_j]);
j_--;
}
while(a[i - 1][j_][0] < a[i][_j][0]) _j--;
suff_tp[j] = max(suff_tp[j], tp[i - 1][j_] + pref[i][_j]);
}
}
}
{
int j_ = 0;
for(int j=1;j<(int)a[i].size();j++){
while(j_ + 1 < (int)a[i + 1].size() && a[i + 1][j_ + 1][0] <= a[i][j][0]){
j_++;
}
mx[i] = max(mx[i], tp[i][j] + pref[i + 1][j_]);
}
}
//~ for(int j=1;j<=n;j++){
//~ mx[i] = max(mx[i], tp[i][j] + pref[i + 1][j]);
//~ if(i > 1){
//~ pref_tp[j] = max(pref_tp[j - 1], tp[i - 1][j]);
//~ }
//~ pref_00[j] = max(pref_00[j - 1], dp[i][j][0] - pref[i][j]);
//~ }
//~ for(int j=n;j>=1;j--){
//~ if(i > 1){
//~ suff_tp[j] = max(suff_tp[j + 1], tp[i - 1][j] + pref[i][j]);
//~ }
//~ suff_11[j] = max(suff_11[j + 1], dp[i][j][1] + pref[i + 1][j]);
//~ suff_01[j] = max(suff_01[j + 1], dp[i][j][0] + pref[i + 1][j]);
//~ }
}
mx[i] = max(mx[i-1], mx[i]);
}
return mx[n];
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
86 ms |
35296 KB |
Output is correct |
2 |
Correct |
100 ms |
40936 KB |
Output is correct |
3 |
Correct |
43 ms |
22984 KB |
Output is correct |
4 |
Correct |
53 ms |
22984 KB |
Output is correct |
5 |
Correct |
291 ms |
76220 KB |
Output is correct |
6 |
Correct |
399 ms |
75636 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Correct |
146 ms |
42268 KB |
Output is correct |
3 |
Correct |
171 ms |
48024 KB |
Output is correct |
4 |
Correct |
88 ms |
35252 KB |
Output is correct |
5 |
Correct |
112 ms |
40932 KB |
Output is correct |
6 |
Correct |
0 ms |
212 KB |
Output is correct |
7 |
Correct |
0 ms |
212 KB |
Output is correct |
8 |
Correct |
0 ms |
212 KB |
Output is correct |
9 |
Correct |
0 ms |
212 KB |
Output is correct |
10 |
Correct |
40 ms |
22880 KB |
Output is correct |
11 |
Correct |
39 ms |
22912 KB |
Output is correct |
12 |
Correct |
98 ms |
39104 KB |
Output is correct |
13 |
Correct |
124 ms |
45676 KB |
Output is correct |
14 |
Correct |
96 ms |
37228 KB |
Output is correct |
15 |
Correct |
100 ms |
34956 KB |
Output is correct |
16 |
Correct |
96 ms |
37392 KB |
Output is correct |
17 |
Correct |
125 ms |
41256 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
41 ms |
22960 KB |
Output is correct |
2 |
Correct |
40 ms |
22868 KB |
Output is correct |
3 |
Correct |
89 ms |
23964 KB |
Output is correct |
4 |
Correct |
73 ms |
25332 KB |
Output is correct |
5 |
Correct |
186 ms |
28736 KB |
Output is correct |
6 |
Correct |
134 ms |
28844 KB |
Output is correct |
7 |
Correct |
149 ms |
28744 KB |
Output is correct |
8 |
Correct |
170 ms |
29012 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
212 KB |
Output is correct |
2 |
Correct |
0 ms |
212 KB |
Output is correct |
3 |
Correct |
0 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 |
0 ms |
212 KB |
Output is correct |
7 |
Correct |
0 ms |
212 KB |
Output is correct |
8 |
Correct |
0 ms |
212 KB |
Output is correct |
9 |
Correct |
1 ms |
340 KB |
Output is correct |
10 |
Correct |
2 ms |
580 KB |
Output is correct |
11 |
Correct |
1 ms |
340 KB |
Output is correct |
12 |
Correct |
2 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 |
1 ms |
212 KB |
Output is correct |
2 |
Correct |
0 ms |
212 KB |
Output is correct |
3 |
Correct |
0 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 |
0 ms |
212 KB |
Output is correct |
7 |
Correct |
0 ms |
212 KB |
Output is correct |
8 |
Correct |
0 ms |
212 KB |
Output is correct |
9 |
Correct |
1 ms |
340 KB |
Output is correct |
10 |
Correct |
2 ms |
580 KB |
Output is correct |
11 |
Correct |
1 ms |
340 KB |
Output is correct |
12 |
Correct |
2 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 |
2 ms |
468 KB |
Output is correct |
17 |
Correct |
27 ms |
5192 KB |
Output is correct |
18 |
Correct |
26 ms |
4820 KB |
Output is correct |
19 |
Correct |
31 ms |
4744 KB |
Output is correct |
20 |
Correct |
26 ms |
4288 KB |
Output is correct |
21 |
Correct |
28 ms |
4300 KB |
Output is correct |
22 |
Correct |
58 ms |
8284 KB |
Output is correct |
23 |
Correct |
8 ms |
1876 KB |
Output is correct |
24 |
Correct |
22 ms |
4408 KB |
Output is correct |
25 |
Correct |
2 ms |
468 KB |
Output is correct |
26 |
Correct |
6 ms |
1748 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
212 KB |
Output is correct |
2 |
Correct |
0 ms |
212 KB |
Output is correct |
3 |
Correct |
0 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 |
0 ms |
212 KB |
Output is correct |
7 |
Correct |
0 ms |
212 KB |
Output is correct |
8 |
Correct |
0 ms |
212 KB |
Output is correct |
9 |
Correct |
1 ms |
340 KB |
Output is correct |
10 |
Correct |
2 ms |
580 KB |
Output is correct |
11 |
Correct |
1 ms |
340 KB |
Output is correct |
12 |
Correct |
2 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 |
2 ms |
468 KB |
Output is correct |
17 |
Correct |
27 ms |
5192 KB |
Output is correct |
18 |
Correct |
26 ms |
4820 KB |
Output is correct |
19 |
Correct |
31 ms |
4744 KB |
Output is correct |
20 |
Correct |
26 ms |
4288 KB |
Output is correct |
21 |
Correct |
28 ms |
4300 KB |
Output is correct |
22 |
Correct |
58 ms |
8284 KB |
Output is correct |
23 |
Correct |
8 ms |
1876 KB |
Output is correct |
24 |
Correct |
22 ms |
4408 KB |
Output is correct |
25 |
Correct |
2 ms |
468 KB |
Output is correct |
26 |
Correct |
6 ms |
1748 KB |
Output is correct |
27 |
Correct |
4 ms |
1364 KB |
Output is correct |
28 |
Correct |
139 ms |
21080 KB |
Output is correct |
29 |
Correct |
219 ms |
34060 KB |
Output is correct |
30 |
Correct |
243 ms |
60084 KB |
Output is correct |
31 |
Correct |
278 ms |
59468 KB |
Output is correct |
32 |
Correct |
180 ms |
27888 KB |
Output is correct |
33 |
Correct |
251 ms |
60652 KB |
Output is correct |
34 |
Correct |
278 ms |
60644 KB |
Output is correct |
35 |
Correct |
84 ms |
17656 KB |
Output is correct |
36 |
Correct |
234 ms |
48228 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
41 ms |
22960 KB |
Output is correct |
2 |
Correct |
40 ms |
22868 KB |
Output is correct |
3 |
Correct |
89 ms |
23964 KB |
Output is correct |
4 |
Correct |
73 ms |
25332 KB |
Output is correct |
5 |
Correct |
186 ms |
28736 KB |
Output is correct |
6 |
Correct |
134 ms |
28844 KB |
Output is correct |
7 |
Correct |
149 ms |
28744 KB |
Output is correct |
8 |
Correct |
170 ms |
29012 KB |
Output is correct |
9 |
Correct |
156 ms |
38092 KB |
Output is correct |
10 |
Correct |
100 ms |
18068 KB |
Output is correct |
11 |
Correct |
278 ms |
35796 KB |
Output is correct |
12 |
Correct |
0 ms |
212 KB |
Output is correct |
13 |
Correct |
0 ms |
212 KB |
Output is correct |
14 |
Correct |
0 ms |
212 KB |
Output is correct |
15 |
Correct |
0 ms |
212 KB |
Output is correct |
16 |
Correct |
0 ms |
212 KB |
Output is correct |
17 |
Correct |
1 ms |
212 KB |
Output is correct |
18 |
Correct |
39 ms |
22972 KB |
Output is correct |
19 |
Correct |
40 ms |
22904 KB |
Output is correct |
20 |
Correct |
46 ms |
22932 KB |
Output is correct |
21 |
Correct |
44 ms |
22916 KB |
Output is correct |
22 |
Correct |
182 ms |
38856 KB |
Output is correct |
23 |
Correct |
281 ms |
49912 KB |
Output is correct |
24 |
Correct |
290 ms |
51608 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
86 ms |
35296 KB |
Output is correct |
2 |
Correct |
100 ms |
40936 KB |
Output is correct |
3 |
Correct |
43 ms |
22984 KB |
Output is correct |
4 |
Correct |
53 ms |
22984 KB |
Output is correct |
5 |
Correct |
291 ms |
76220 KB |
Output is correct |
6 |
Correct |
399 ms |
75636 KB |
Output is correct |
7 |
Correct |
0 ms |
212 KB |
Output is correct |
8 |
Correct |
146 ms |
42268 KB |
Output is correct |
9 |
Correct |
171 ms |
48024 KB |
Output is correct |
10 |
Correct |
88 ms |
35252 KB |
Output is correct |
11 |
Correct |
112 ms |
40932 KB |
Output is correct |
12 |
Correct |
0 ms |
212 KB |
Output is correct |
13 |
Correct |
0 ms |
212 KB |
Output is correct |
14 |
Correct |
0 ms |
212 KB |
Output is correct |
15 |
Correct |
0 ms |
212 KB |
Output is correct |
16 |
Correct |
40 ms |
22880 KB |
Output is correct |
17 |
Correct |
39 ms |
22912 KB |
Output is correct |
18 |
Correct |
98 ms |
39104 KB |
Output is correct |
19 |
Correct |
124 ms |
45676 KB |
Output is correct |
20 |
Correct |
96 ms |
37228 KB |
Output is correct |
21 |
Correct |
100 ms |
34956 KB |
Output is correct |
22 |
Correct |
96 ms |
37392 KB |
Output is correct |
23 |
Correct |
125 ms |
41256 KB |
Output is correct |
24 |
Correct |
41 ms |
22960 KB |
Output is correct |
25 |
Correct |
40 ms |
22868 KB |
Output is correct |
26 |
Correct |
89 ms |
23964 KB |
Output is correct |
27 |
Correct |
73 ms |
25332 KB |
Output is correct |
28 |
Correct |
186 ms |
28736 KB |
Output is correct |
29 |
Correct |
134 ms |
28844 KB |
Output is correct |
30 |
Correct |
149 ms |
28744 KB |
Output is correct |
31 |
Correct |
170 ms |
29012 KB |
Output is correct |
32 |
Correct |
1 ms |
212 KB |
Output is correct |
33 |
Correct |
0 ms |
212 KB |
Output is correct |
34 |
Correct |
0 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 |
0 ms |
212 KB |
Output is correct |
38 |
Correct |
0 ms |
212 KB |
Output is correct |
39 |
Correct |
0 ms |
212 KB |
Output is correct |
40 |
Correct |
1 ms |
340 KB |
Output is correct |
41 |
Correct |
2 ms |
580 KB |
Output is correct |
42 |
Correct |
1 ms |
340 KB |
Output is correct |
43 |
Correct |
2 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 |
2 ms |
468 KB |
Output is correct |
48 |
Correct |
27 ms |
5192 KB |
Output is correct |
49 |
Correct |
26 ms |
4820 KB |
Output is correct |
50 |
Correct |
31 ms |
4744 KB |
Output is correct |
51 |
Correct |
26 ms |
4288 KB |
Output is correct |
52 |
Correct |
28 ms |
4300 KB |
Output is correct |
53 |
Correct |
58 ms |
8284 KB |
Output is correct |
54 |
Correct |
8 ms |
1876 KB |
Output is correct |
55 |
Correct |
22 ms |
4408 KB |
Output is correct |
56 |
Correct |
2 ms |
468 KB |
Output is correct |
57 |
Correct |
6 ms |
1748 KB |
Output is correct |
58 |
Correct |
4 ms |
1364 KB |
Output is correct |
59 |
Correct |
139 ms |
21080 KB |
Output is correct |
60 |
Correct |
219 ms |
34060 KB |
Output is correct |
61 |
Correct |
243 ms |
60084 KB |
Output is correct |
62 |
Correct |
278 ms |
59468 KB |
Output is correct |
63 |
Correct |
180 ms |
27888 KB |
Output is correct |
64 |
Correct |
251 ms |
60652 KB |
Output is correct |
65 |
Correct |
278 ms |
60644 KB |
Output is correct |
66 |
Correct |
84 ms |
17656 KB |
Output is correct |
67 |
Correct |
234 ms |
48228 KB |
Output is correct |
68 |
Correct |
156 ms |
38092 KB |
Output is correct |
69 |
Correct |
100 ms |
18068 KB |
Output is correct |
70 |
Correct |
278 ms |
35796 KB |
Output is correct |
71 |
Correct |
0 ms |
212 KB |
Output is correct |
72 |
Correct |
0 ms |
212 KB |
Output is correct |
73 |
Correct |
0 ms |
212 KB |
Output is correct |
74 |
Correct |
0 ms |
212 KB |
Output is correct |
75 |
Correct |
0 ms |
212 KB |
Output is correct |
76 |
Correct |
1 ms |
212 KB |
Output is correct |
77 |
Correct |
39 ms |
22972 KB |
Output is correct |
78 |
Correct |
40 ms |
22904 KB |
Output is correct |
79 |
Correct |
46 ms |
22932 KB |
Output is correct |
80 |
Correct |
44 ms |
22916 KB |
Output is correct |
81 |
Correct |
182 ms |
38856 KB |
Output is correct |
82 |
Correct |
281 ms |
49912 KB |
Output is correct |
83 |
Correct |
290 ms |
51608 KB |
Output is correct |
84 |
Correct |
285 ms |
73624 KB |
Output is correct |
85 |
Correct |
291 ms |
75200 KB |
Output is correct |
86 |
Correct |
409 ms |
79436 KB |
Output is correct |
87 |
Correct |
440 ms |
82092 KB |
Output is correct |
88 |
Correct |
1 ms |
304 KB |
Output is correct |
89 |
Correct |
405 ms |
82180 KB |
Output is correct |
90 |
Correct |
314 ms |
69604 KB |
Output is correct |
91 |
Correct |
252 ms |
56032 KB |
Output is correct |