#include "fish.h"
#define F first
#define S second
#include <bits/stdc++.h>
typedef long long ll;
const ll INF= 1LL<<60;
using namespace std;
typedef pair<int, ll> pil;
long long max_weights(int N, int M, std::vector<int> X, vector<int> Y,
vector<int> W) {
for(int i=0;i<M;i++){
Y[i]+=1;
}
vector<pil> rs[N];
for(int i=0;i<M;i++){
rs[X[i]].emplace_back(Y[i],W[i]);
}
for(int i=0;i<N;i++){
rs[i].emplace_back(0,0);
sort(rs[i].begin(),rs[i].end());
rs[i].emplace_back(N+2,0);
for(int j=1;j<(int)rs[i].size();j++){
rs[i][j].S+=rs[i][j-1].S;
}
}
ll dp[2][3][N+2]; // (0, h[i-1]), (h[i-1] <= h[i], h[i]), (h[i-1]>h[i], h[i])
memset(dp,0,sizeof dp);
for(int i=0,k=0;i<(int)rs[0].size();i++){
while(k+1<(int)rs[1].size() && rs[0][i].F>rs[1][k+1].F){
k++;
}
dp[1][0][i]=rs[1][k].S;
}
for(int i=0,k=0;i<(int)rs[1].size();i++){
while(k+1<(int)rs[0].size() && rs[1][i].F>rs[0][k+1].F ){
k++;
}
dp[1][1][i]=rs[0][k].S;
dp[1][2][i]=rs[1].back().S-rs[1][max(i-1,0)].S;
}
for(int i=2;i<N;i++){
for(int j=0;j<3;j++){
for(int k=0;k<(int)max(rs[i-1].size(),rs[i].size());k++){
dp[i&1][j][k]=0;
}
}
int rss = rs[i].size();
int ps = rs[i-1].size();
int pps = rs[i-2].size();
for(int j=0, k = 0;j<ps;j++){
while(k+1<rss && rs[i-1][j].F > rs[i][k+1].F){
k++;
}
dp[i&1][0][j]=max(dp[(i&1)^1][1][j],dp[(i&1)^1][2][j])+rs[i][k].S;
}
//0,k -> 1,j
ll cmax = -INF;
for(int j=0, k =0, l=0, l2=0;j<rss;j++){
while(k<pps && rs[i][j].F>rs[i-2][k].F){
while(l+1<ps && rs[i-2][k].F > rs[i-1][l+1].F){
l++;
}
cmax = max(cmax,dp[(i&1)^1][0][k]+rs[i-1][l].S);
k++;
}
while(l2+1<ps && rs[i][j].F > rs[i-1][l2+1].F){
l2++;
}
dp[i&1][1][j]=max(dp[i&1][1][j],cmax-rs[i-1][l2].S);
}
cmax = -INF;
for(int j=rss-1,k=pps-1 ;j>=0;j--){
while(k>=0 && rs[i][j].F<=rs[i-2][k].F){
cmax = max(cmax,dp[(i&1)^1][0][k]);
k--;
}
dp[i&1][1][j]=max(dp[i&1][1][j],cmax);
}
//1,k -> 1,j
cmax = -INF;
for(int j=0, k=0, k2=0;j<rss;j++){
while(k<ps && rs[i][j].F>=rs[i-1][k].F){
cmax = max(cmax, dp[(i&1)^1][1][k]-rs[i-1][max(k-1,0)].S);
k++;
}
while(k2+1<ps && rs[i][j].F > rs[i-1][k2].F)k2++;
dp[i&1][1][j]=max(dp[i&1][1][j],cmax+rs[i-1][max(k2-1,0)].S);
}
//1,k 2,k -> 2,j
cmax = -INF;
for(int j=rss-1, k = ps-1, l = rss-1;j>=0;j--){
while(k>=0 && rs[i][j].F<rs[i-1][k].F){
while(l-1>=0 && rs[i-1][k].F<=rs[i][l].F)l--;
cmax = max(cmax, max(dp[(i&1)^1][1][k],dp[(i&1)^1][2][k])+rs[i][l].S);
k--;
}
dp[i&1][2][j]=max(dp[i&1][2][j],cmax-rs[i][max(j-1,0)].S);
}
}
ll ans = 0;
/*for(int i=0;i<=N;i++){
for(int j=0;j<3;j++){
for(int k=0;k<=N;k++){
cout << dp[i&1][j][k] << ' ';
}
cout << endl;
}
cout << endl;
}*/
for(int k=0;k<(int)rs[N-2].size();k++)ans = max(ans, dp[(N-1)&1][0][k]);
for(int j=1;j<3;j++)
for(int k=0;k<(int)rs[N-1].size();k++)ans = max(ans, dp[(N-1)&1][j][k]);
return ans;
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
50 ms |
14012 KB |
Output is correct |
2 |
Correct |
60 ms |
15876 KB |
Output is correct |
3 |
Correct |
20 ms |
12032 KB |
Output is correct |
4 |
Correct |
26 ms |
12024 KB |
Output is correct |
5 |
Correct |
167 ms |
23736 KB |
Output is correct |
6 |
Correct |
189 ms |
26404 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
212 KB |
Output is correct |
2 |
Correct |
81 ms |
17136 KB |
Output is correct |
3 |
Correct |
101 ms |
19772 KB |
Output is correct |
4 |
Correct |
49 ms |
13904 KB |
Output is correct |
5 |
Correct |
80 ms |
15904 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 |
212 KB |
Output is correct |
9 |
Correct |
1 ms |
212 KB |
Output is correct |
10 |
Correct |
23 ms |
12032 KB |
Output is correct |
11 |
Correct |
27 ms |
11992 KB |
Output is correct |
12 |
Correct |
58 ms |
14048 KB |
Output is correct |
13 |
Correct |
60 ms |
15852 KB |
Output is correct |
14 |
Correct |
66 ms |
13952 KB |
Output is correct |
15 |
Correct |
68 ms |
16308 KB |
Output is correct |
16 |
Correct |
70 ms |
14676 KB |
Output is correct |
17 |
Correct |
71 ms |
16280 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
23 ms |
12036 KB |
Output is correct |
2 |
Correct |
21 ms |
11908 KB |
Output is correct |
3 |
Correct |
57 ms |
14240 KB |
Output is correct |
4 |
Correct |
40 ms |
14280 KB |
Output is correct |
5 |
Correct |
70 ms |
18116 KB |
Output is correct |
6 |
Correct |
59 ms |
18196 KB |
Output is correct |
7 |
Correct |
58 ms |
18176 KB |
Output is correct |
8 |
Correct |
61 ms |
18224 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 |
0 ms |
212 KB |
Output is correct |
6 |
Correct |
0 ms |
212 KB |
Output is correct |
7 |
Correct |
1 ms |
212 KB |
Output is correct |
8 |
Correct |
1 ms |
212 KB |
Output is correct |
9 |
Correct |
1 ms |
340 KB |
Output is correct |
10 |
Correct |
2 ms |
512 KB |
Output is correct |
11 |
Correct |
1 ms |
340 KB |
Output is correct |
12 |
Correct |
1 ms |
340 KB |
Output is correct |
13 |
Correct |
0 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 |
0 ms |
212 KB |
Output is correct |
6 |
Correct |
0 ms |
212 KB |
Output is correct |
7 |
Correct |
1 ms |
212 KB |
Output is correct |
8 |
Correct |
1 ms |
212 KB |
Output is correct |
9 |
Correct |
1 ms |
340 KB |
Output is correct |
10 |
Correct |
2 ms |
512 KB |
Output is correct |
11 |
Correct |
1 ms |
340 KB |
Output is correct |
12 |
Correct |
1 ms |
340 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 |
2 ms |
340 KB |
Output is correct |
17 |
Correct |
25 ms |
2256 KB |
Output is correct |
18 |
Correct |
24 ms |
2872 KB |
Output is correct |
19 |
Correct |
16 ms |
2772 KB |
Output is correct |
20 |
Correct |
16 ms |
2744 KB |
Output is correct |
21 |
Correct |
18 ms |
2644 KB |
Output is correct |
22 |
Correct |
36 ms |
5072 KB |
Output is correct |
23 |
Correct |
5 ms |
724 KB |
Output is correct |
24 |
Correct |
13 ms |
1620 KB |
Output is correct |
25 |
Correct |
2 ms |
340 KB |
Output is correct |
26 |
Correct |
4 ms |
724 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 |
0 ms |
212 KB |
Output is correct |
6 |
Correct |
0 ms |
212 KB |
Output is correct |
7 |
Correct |
1 ms |
212 KB |
Output is correct |
8 |
Correct |
1 ms |
212 KB |
Output is correct |
9 |
Correct |
1 ms |
340 KB |
Output is correct |
10 |
Correct |
2 ms |
512 KB |
Output is correct |
11 |
Correct |
1 ms |
340 KB |
Output is correct |
12 |
Correct |
1 ms |
340 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 |
2 ms |
340 KB |
Output is correct |
17 |
Correct |
25 ms |
2256 KB |
Output is correct |
18 |
Correct |
24 ms |
2872 KB |
Output is correct |
19 |
Correct |
16 ms |
2772 KB |
Output is correct |
20 |
Correct |
16 ms |
2744 KB |
Output is correct |
21 |
Correct |
18 ms |
2644 KB |
Output is correct |
22 |
Correct |
36 ms |
5072 KB |
Output is correct |
23 |
Correct |
5 ms |
724 KB |
Output is correct |
24 |
Correct |
13 ms |
1620 KB |
Output is correct |
25 |
Correct |
2 ms |
340 KB |
Output is correct |
26 |
Correct |
4 ms |
724 KB |
Output is correct |
27 |
Correct |
3 ms |
724 KB |
Output is correct |
28 |
Correct |
84 ms |
11700 KB |
Output is correct |
29 |
Correct |
144 ms |
15456 KB |
Output is correct |
30 |
Correct |
158 ms |
15052 KB |
Output is correct |
31 |
Correct |
130 ms |
14976 KB |
Output is correct |
32 |
Correct |
143 ms |
16732 KB |
Output is correct |
33 |
Correct |
135 ms |
15012 KB |
Output is correct |
34 |
Correct |
125 ms |
15008 KB |
Output is correct |
35 |
Correct |
66 ms |
7148 KB |
Output is correct |
36 |
Correct |
136 ms |
17016 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
23 ms |
12036 KB |
Output is correct |
2 |
Correct |
21 ms |
11908 KB |
Output is correct |
3 |
Correct |
57 ms |
14240 KB |
Output is correct |
4 |
Correct |
40 ms |
14280 KB |
Output is correct |
5 |
Correct |
70 ms |
18116 KB |
Output is correct |
6 |
Correct |
59 ms |
18196 KB |
Output is correct |
7 |
Correct |
58 ms |
18176 KB |
Output is correct |
8 |
Correct |
61 ms |
18224 KB |
Output is correct |
9 |
Correct |
58 ms |
18240 KB |
Output is correct |
10 |
Correct |
56 ms |
10716 KB |
Output is correct |
11 |
Correct |
106 ms |
21100 KB |
Output is correct |
12 |
Correct |
1 ms |
212 KB |
Output is correct |
13 |
Correct |
1 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 |
20 ms |
11920 KB |
Output is correct |
19 |
Correct |
22 ms |
12012 KB |
Output is correct |
20 |
Correct |
19 ms |
12004 KB |
Output is correct |
21 |
Correct |
21 ms |
12028 KB |
Output is correct |
22 |
Correct |
104 ms |
16864 KB |
Output is correct |
23 |
Correct |
104 ms |
21068 KB |
Output is correct |
24 |
Correct |
115 ms |
21088 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
50 ms |
14012 KB |
Output is correct |
2 |
Correct |
60 ms |
15876 KB |
Output is correct |
3 |
Correct |
20 ms |
12032 KB |
Output is correct |
4 |
Correct |
26 ms |
12024 KB |
Output is correct |
5 |
Correct |
167 ms |
23736 KB |
Output is correct |
6 |
Correct |
189 ms |
26404 KB |
Output is correct |
7 |
Correct |
1 ms |
212 KB |
Output is correct |
8 |
Correct |
81 ms |
17136 KB |
Output is correct |
9 |
Correct |
101 ms |
19772 KB |
Output is correct |
10 |
Correct |
49 ms |
13904 KB |
Output is correct |
11 |
Correct |
80 ms |
15904 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 |
23 ms |
12032 KB |
Output is correct |
17 |
Correct |
27 ms |
11992 KB |
Output is correct |
18 |
Correct |
58 ms |
14048 KB |
Output is correct |
19 |
Correct |
60 ms |
15852 KB |
Output is correct |
20 |
Correct |
66 ms |
13952 KB |
Output is correct |
21 |
Correct |
68 ms |
16308 KB |
Output is correct |
22 |
Correct |
70 ms |
14676 KB |
Output is correct |
23 |
Correct |
71 ms |
16280 KB |
Output is correct |
24 |
Correct |
23 ms |
12036 KB |
Output is correct |
25 |
Correct |
21 ms |
11908 KB |
Output is correct |
26 |
Correct |
57 ms |
14240 KB |
Output is correct |
27 |
Correct |
40 ms |
14280 KB |
Output is correct |
28 |
Correct |
70 ms |
18116 KB |
Output is correct |
29 |
Correct |
59 ms |
18196 KB |
Output is correct |
30 |
Correct |
58 ms |
18176 KB |
Output is correct |
31 |
Correct |
61 ms |
18224 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 |
0 ms |
212 KB |
Output is correct |
37 |
Correct |
0 ms |
212 KB |
Output is correct |
38 |
Correct |
1 ms |
212 KB |
Output is correct |
39 |
Correct |
1 ms |
212 KB |
Output is correct |
40 |
Correct |
1 ms |
340 KB |
Output is correct |
41 |
Correct |
2 ms |
512 KB |
Output is correct |
42 |
Correct |
1 ms |
340 KB |
Output is correct |
43 |
Correct |
1 ms |
340 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 |
2 ms |
340 KB |
Output is correct |
48 |
Correct |
25 ms |
2256 KB |
Output is correct |
49 |
Correct |
24 ms |
2872 KB |
Output is correct |
50 |
Correct |
16 ms |
2772 KB |
Output is correct |
51 |
Correct |
16 ms |
2744 KB |
Output is correct |
52 |
Correct |
18 ms |
2644 KB |
Output is correct |
53 |
Correct |
36 ms |
5072 KB |
Output is correct |
54 |
Correct |
5 ms |
724 KB |
Output is correct |
55 |
Correct |
13 ms |
1620 KB |
Output is correct |
56 |
Correct |
2 ms |
340 KB |
Output is correct |
57 |
Correct |
4 ms |
724 KB |
Output is correct |
58 |
Correct |
3 ms |
724 KB |
Output is correct |
59 |
Correct |
84 ms |
11700 KB |
Output is correct |
60 |
Correct |
144 ms |
15456 KB |
Output is correct |
61 |
Correct |
158 ms |
15052 KB |
Output is correct |
62 |
Correct |
130 ms |
14976 KB |
Output is correct |
63 |
Correct |
143 ms |
16732 KB |
Output is correct |
64 |
Correct |
135 ms |
15012 KB |
Output is correct |
65 |
Correct |
125 ms |
15008 KB |
Output is correct |
66 |
Correct |
66 ms |
7148 KB |
Output is correct |
67 |
Correct |
136 ms |
17016 KB |
Output is correct |
68 |
Correct |
58 ms |
18240 KB |
Output is correct |
69 |
Correct |
56 ms |
10716 KB |
Output is correct |
70 |
Correct |
106 ms |
21100 KB |
Output is correct |
71 |
Correct |
1 ms |
212 KB |
Output is correct |
72 |
Correct |
1 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 |
20 ms |
11920 KB |
Output is correct |
78 |
Correct |
22 ms |
12012 KB |
Output is correct |
79 |
Correct |
19 ms |
12004 KB |
Output is correct |
80 |
Correct |
21 ms |
12028 KB |
Output is correct |
81 |
Correct |
104 ms |
16864 KB |
Output is correct |
82 |
Correct |
104 ms |
21068 KB |
Output is correct |
83 |
Correct |
115 ms |
21088 KB |
Output is correct |
84 |
Correct |
147 ms |
23628 KB |
Output is correct |
85 |
Correct |
149 ms |
24776 KB |
Output is correct |
86 |
Correct |
208 ms |
26236 KB |
Output is correct |
87 |
Correct |
173 ms |
27292 KB |
Output is correct |
88 |
Correct |
1 ms |
212 KB |
Output is correct |
89 |
Correct |
213 ms |
27536 KB |
Output is correct |
90 |
Correct |
155 ms |
26336 KB |
Output is correct |
91 |
Correct |
160 ms |
26528 KB |
Output is correct |