#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];
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
71 ms |
35288 KB |
Output is correct |
2 |
Correct |
85 ms |
40936 KB |
Output is correct |
3 |
Correct |
33 ms |
22892 KB |
Output is correct |
4 |
Correct |
32 ms |
22912 KB |
Output is correct |
5 |
Correct |
236 ms |
76108 KB |
Output is correct |
6 |
Correct |
348 ms |
75752 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
336 KB |
Output is correct |
2 |
Correct |
122 ms |
42264 KB |
Output is correct |
3 |
Correct |
151 ms |
48088 KB |
Output is correct |
4 |
Correct |
70 ms |
35212 KB |
Output is correct |
5 |
Correct |
89 ms |
41000 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 |
212 KB |
Output is correct |
10 |
Correct |
32 ms |
22872 KB |
Output is correct |
11 |
Correct |
32 ms |
22888 KB |
Output is correct |
12 |
Correct |
83 ms |
39072 KB |
Output is correct |
13 |
Correct |
100 ms |
45696 KB |
Output is correct |
14 |
Correct |
82 ms |
37280 KB |
Output is correct |
15 |
Correct |
83 ms |
35032 KB |
Output is correct |
16 |
Correct |
77 ms |
37212 KB |
Output is correct |
17 |
Correct |
87 ms |
41320 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
32 ms |
23008 KB |
Output is correct |
2 |
Correct |
32 ms |
22940 KB |
Output is correct |
3 |
Correct |
64 ms |
23936 KB |
Output is correct |
4 |
Correct |
56 ms |
25292 KB |
Output is correct |
5 |
Correct |
109 ms |
28732 KB |
Output is correct |
6 |
Correct |
109 ms |
28808 KB |
Output is correct |
7 |
Correct |
111 ms |
28848 KB |
Output is correct |
8 |
Correct |
105 ms |
28856 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 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 |
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 |
0 ms |
212 KB |
Output is correct |
9 |
Correct |
1 ms |
340 KB |
Output is correct |
10 |
Correct |
2 ms |
468 KB |
Output is correct |
11 |
Correct |
1 ms |
340 KB |
Output is correct |
12 |
Correct |
1 ms |
468 KB |
Output is correct |
13 |
Correct |
0 ms |
212 KB |
Output is correct |
14 |
Correct |
1 ms |
340 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 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 |
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 |
0 ms |
212 KB |
Output is correct |
9 |
Correct |
1 ms |
340 KB |
Output is correct |
10 |
Correct |
2 ms |
468 KB |
Output is correct |
11 |
Correct |
1 ms |
340 KB |
Output is correct |
12 |
Correct |
1 ms |
468 KB |
Output is correct |
13 |
Correct |
0 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 |
5320 KB |
Output is correct |
18 |
Correct |
24 ms |
4824 KB |
Output is correct |
19 |
Correct |
22 ms |
4828 KB |
Output is correct |
20 |
Correct |
22 ms |
4308 KB |
Output is correct |
21 |
Correct |
22 ms |
4308 KB |
Output is correct |
22 |
Correct |
44 ms |
8272 KB |
Output is correct |
23 |
Correct |
6 ms |
1876 KB |
Output is correct |
24 |
Correct |
17 ms |
4384 KB |
Output is correct |
25 |
Correct |
1 ms |
468 KB |
Output is correct |
26 |
Correct |
5 ms |
1748 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 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 |
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 |
0 ms |
212 KB |
Output is correct |
9 |
Correct |
1 ms |
340 KB |
Output is correct |
10 |
Correct |
2 ms |
468 KB |
Output is correct |
11 |
Correct |
1 ms |
340 KB |
Output is correct |
12 |
Correct |
1 ms |
468 KB |
Output is correct |
13 |
Correct |
0 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 |
5320 KB |
Output is correct |
18 |
Correct |
24 ms |
4824 KB |
Output is correct |
19 |
Correct |
22 ms |
4828 KB |
Output is correct |
20 |
Correct |
22 ms |
4308 KB |
Output is correct |
21 |
Correct |
22 ms |
4308 KB |
Output is correct |
22 |
Correct |
44 ms |
8272 KB |
Output is correct |
23 |
Correct |
6 ms |
1876 KB |
Output is correct |
24 |
Correct |
17 ms |
4384 KB |
Output is correct |
25 |
Correct |
1 ms |
468 KB |
Output is correct |
26 |
Correct |
5 ms |
1748 KB |
Output is correct |
27 |
Correct |
3 ms |
1364 KB |
Output is correct |
28 |
Correct |
114 ms |
21124 KB |
Output is correct |
29 |
Correct |
188 ms |
34064 KB |
Output is correct |
30 |
Correct |
213 ms |
60124 KB |
Output is correct |
31 |
Correct |
232 ms |
59488 KB |
Output is correct |
32 |
Correct |
154 ms |
27908 KB |
Output is correct |
33 |
Correct |
215 ms |
60660 KB |
Output is correct |
34 |
Correct |
222 ms |
60620 KB |
Output is correct |
35 |
Correct |
71 ms |
17692 KB |
Output is correct |
36 |
Correct |
196 ms |
48208 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
32 ms |
23008 KB |
Output is correct |
2 |
Correct |
32 ms |
22940 KB |
Output is correct |
3 |
Correct |
64 ms |
23936 KB |
Output is correct |
4 |
Correct |
56 ms |
25292 KB |
Output is correct |
5 |
Correct |
109 ms |
28732 KB |
Output is correct |
6 |
Correct |
109 ms |
28808 KB |
Output is correct |
7 |
Correct |
111 ms |
28848 KB |
Output is correct |
8 |
Correct |
105 ms |
28856 KB |
Output is correct |
9 |
Correct |
115 ms |
38084 KB |
Output is correct |
10 |
Correct |
74 ms |
17988 KB |
Output is correct |
11 |
Correct |
175 ms |
35792 KB |
Output is correct |
12 |
Correct |
1 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 |
0 ms |
212 KB |
Output is correct |
18 |
Correct |
32 ms |
23000 KB |
Output is correct |
19 |
Correct |
32 ms |
22984 KB |
Output is correct |
20 |
Correct |
41 ms |
22948 KB |
Output is correct |
21 |
Correct |
33 ms |
22896 KB |
Output is correct |
22 |
Correct |
145 ms |
38884 KB |
Output is correct |
23 |
Correct |
209 ms |
46376 KB |
Output is correct |
24 |
Correct |
204 ms |
47648 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
71 ms |
35288 KB |
Output is correct |
2 |
Correct |
85 ms |
40936 KB |
Output is correct |
3 |
Correct |
33 ms |
22892 KB |
Output is correct |
4 |
Correct |
32 ms |
22912 KB |
Output is correct |
5 |
Correct |
236 ms |
76108 KB |
Output is correct |
6 |
Correct |
348 ms |
75752 KB |
Output is correct |
7 |
Correct |
0 ms |
336 KB |
Output is correct |
8 |
Correct |
122 ms |
42264 KB |
Output is correct |
9 |
Correct |
151 ms |
48088 KB |
Output is correct |
10 |
Correct |
70 ms |
35212 KB |
Output is correct |
11 |
Correct |
89 ms |
41000 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 |
1 ms |
212 KB |
Output is correct |
16 |
Correct |
32 ms |
22872 KB |
Output is correct |
17 |
Correct |
32 ms |
22888 KB |
Output is correct |
18 |
Correct |
83 ms |
39072 KB |
Output is correct |
19 |
Correct |
100 ms |
45696 KB |
Output is correct |
20 |
Correct |
82 ms |
37280 KB |
Output is correct |
21 |
Correct |
83 ms |
35032 KB |
Output is correct |
22 |
Correct |
77 ms |
37212 KB |
Output is correct |
23 |
Correct |
87 ms |
41320 KB |
Output is correct |
24 |
Correct |
32 ms |
23008 KB |
Output is correct |
25 |
Correct |
32 ms |
22940 KB |
Output is correct |
26 |
Correct |
64 ms |
23936 KB |
Output is correct |
27 |
Correct |
56 ms |
25292 KB |
Output is correct |
28 |
Correct |
109 ms |
28732 KB |
Output is correct |
29 |
Correct |
109 ms |
28808 KB |
Output is correct |
30 |
Correct |
111 ms |
28848 KB |
Output is correct |
31 |
Correct |
105 ms |
28856 KB |
Output is correct |
32 |
Correct |
0 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 |
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 |
0 ms |
212 KB |
Output is correct |
40 |
Correct |
1 ms |
340 KB |
Output is correct |
41 |
Correct |
2 ms |
468 KB |
Output is correct |
42 |
Correct |
1 ms |
340 KB |
Output is correct |
43 |
Correct |
1 ms |
468 KB |
Output is correct |
44 |
Correct |
0 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 |
5320 KB |
Output is correct |
49 |
Correct |
24 ms |
4824 KB |
Output is correct |
50 |
Correct |
22 ms |
4828 KB |
Output is correct |
51 |
Correct |
22 ms |
4308 KB |
Output is correct |
52 |
Correct |
22 ms |
4308 KB |
Output is correct |
53 |
Correct |
44 ms |
8272 KB |
Output is correct |
54 |
Correct |
6 ms |
1876 KB |
Output is correct |
55 |
Correct |
17 ms |
4384 KB |
Output is correct |
56 |
Correct |
1 ms |
468 KB |
Output is correct |
57 |
Correct |
5 ms |
1748 KB |
Output is correct |
58 |
Correct |
3 ms |
1364 KB |
Output is correct |
59 |
Correct |
114 ms |
21124 KB |
Output is correct |
60 |
Correct |
188 ms |
34064 KB |
Output is correct |
61 |
Correct |
213 ms |
60124 KB |
Output is correct |
62 |
Correct |
232 ms |
59488 KB |
Output is correct |
63 |
Correct |
154 ms |
27908 KB |
Output is correct |
64 |
Correct |
215 ms |
60660 KB |
Output is correct |
65 |
Correct |
222 ms |
60620 KB |
Output is correct |
66 |
Correct |
71 ms |
17692 KB |
Output is correct |
67 |
Correct |
196 ms |
48208 KB |
Output is correct |
68 |
Correct |
115 ms |
38084 KB |
Output is correct |
69 |
Correct |
74 ms |
17988 KB |
Output is correct |
70 |
Correct |
175 ms |
35792 KB |
Output is correct |
71 |
Correct |
1 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 |
0 ms |
212 KB |
Output is correct |
77 |
Correct |
32 ms |
23000 KB |
Output is correct |
78 |
Correct |
32 ms |
22984 KB |
Output is correct |
79 |
Correct |
41 ms |
22948 KB |
Output is correct |
80 |
Correct |
33 ms |
22896 KB |
Output is correct |
81 |
Correct |
145 ms |
38884 KB |
Output is correct |
82 |
Correct |
209 ms |
46376 KB |
Output is correct |
83 |
Correct |
204 ms |
47648 KB |
Output is correct |
84 |
Correct |
263 ms |
73608 KB |
Output is correct |
85 |
Correct |
257 ms |
75260 KB |
Output is correct |
86 |
Correct |
319 ms |
79560 KB |
Output is correct |
87 |
Correct |
332 ms |
82124 KB |
Output is correct |
88 |
Correct |
0 ms |
296 KB |
Output is correct |
89 |
Correct |
368 ms |
82144 KB |
Output is correct |
90 |
Correct |
253 ms |
69596 KB |
Output is correct |
91 |
Correct |
211 ms |
56048 KB |
Output is correct |