#include"fish.h"
#include<vector>
#include<iostream>
#include<algorithm>
#include<limits.h>
using namespace std;
typedef long long ll;
int n;
vector<pair<int,int>>fish[100001];
vector<ll>sum_down[100001];
vector<ll>sum_right_pos[100001];
vector<ll>sum_left_pos[100001];
vector<ll>next_right[100001];
vector<ll>next_left[100001];
int h[100001];
ll full_sum[100001];
vector<ll>slope_down_dp[1000001];
vector<ll>slope_down_dp_max[1000001];
vector<ll>slope_up_dp[1000001];
vector<ll>slope_up_dp_max[1000001];
vector<ll>dp_max1[1000001];
vector<ll>dp_max2[1000001];
vector<ll>gap_dp[1000001];
ll full_dp[1000001];
ll get_sum_right(int x,int y){
if(x==n-1)
return 0;
return sum_down[x+1][sum_right_pos[x][y]];
}
ll get_sum_left(int x,int y){
if(x==0)
return 0;
return sum_down[x-1][sum_left_pos[x][y]];
}
ll max_weights(int N,int M,vector<int>X,vector<int>Y,vector<int>W){
n=N+1;
for(int i=0;i<M;i++){
fish[X[i]+1].push_back({Y[i],W[i]});
full_sum[X[i]+1]+=W[i];
}
for(int i=0;i<n;i++)
sort(fish[i].begin(),fish[i].end());
for(int i=0;i<n;i++)
fish[i].push_back({n+1,0});
for(int i=0;i<n;i++)
h[i]=(int)fish[i].size();
for(int i=0;i<n;i++){
sum_down[i].resize(h[i]);
for(int j=1;j<h[i];j++){
sum_down[i][j]=sum_down[i][j-1]+fish[i][j-1].second;
}
}
for(int i=0;i<n;i++){
slope_down_dp[i].resize(h[i]);
slope_down_dp_max[i].resize(h[i]+1);
slope_up_dp[i].resize(h[i]);
slope_up_dp_max[i].resize(h[i]+1);
gap_dp[i].resize(h[i]);
dp_max1[i].resize(h[i]+1);
dp_max2[i].resize(h[i]+1);
}
sum_right_pos[n-1].resize(h[n-1]);
for(int i=0;i<n-1;i++){
sum_right_pos[i].resize(h[i]);
next_right[i].resize(h[i]);
int i2=0;
for(int i1=0;i1<h[i];i1++){
while(i2<h[i+1]-1&&fish[i][i1].first>fish[i+1][i2].first){
i2++;
}
sum_right_pos[i][i1]=i2;
}
i2=0;
for(int i1=0;i1<h[i];i1++){
while(i2<h[i+1]&&fish[i][i1].first>=fish[i+1][i2].first)
i2++;
next_right[i][i1]=i2;
}
}
sum_left_pos[0].resize(h[0]);
for(int i=1;i<n;i++){
sum_left_pos[i].resize(h[i]);
next_left[i].resize(h[i]);
int i2=0;
for(int i1=0;i1<h[i];i1++){
while(i2<h[i-1]-1&&fish[i][i1].first>fish[i-1][i2].first){
i2++;
}
sum_left_pos[i][i1]=i2;
}
i2=0;
for(int i1=0;i1<h[i];i1++){
while(i2<h[i-1]&&fish[i][i1].first>=fish[i-1][i2].first)
i2++;
next_left[i][i1]=i2;
}
}
for(int y=0;y<h[n-1];y++){
slope_up_dp[n-1][y]=get_sum_left(n-1,y);
gap_dp[n-1][y]=sum_down[n-1][y];
}
for(int x=n-2;x>=0;x--){
slope_down_dp_max[x+1][0]=-1e17;
for(int y=1;y<=h[x+1];y++){
slope_down_dp_max[x+1][y]=max(slope_down_dp_max[x+1][y-1],slope_down_dp[x+1][y-1]);
}
slope_up_dp_max[x+1][h[x+1]]=-1e17;
for(int y=h[x+1]-1;y>=0;y--){
slope_up_dp_max[x+1][y]=max(slope_up_dp_max[x+1][y+1],slope_up_dp[x+1][y]);
}
dp_max1[x+1][0]=-1e17;
for(int y=1;y<=h[x+1];y++){
dp_max1[x+1][y]=max(dp_max1[x+1][y-1],slope_up_dp[x+1][y-1]-get_sum_left(x+1,y-1));
}
dp_max2[x+1][h[x+1]]=-1e17;
for(int y=h[x+1]-1;y>=0;y--){
dp_max2[x+1][y]=max(dp_max2[x+1][y+1],slope_up_dp[x+1][y]+get_sum_left(x+1,y)-get_sum_left(x+1,y));
}
for(int y=0;y<h[x];y++){
//slope down
{
ll res=slope_down_dp_max[x+1][next_right[x][y]];
res+=-sum_down[x][y]+get_sum_right(x,y);
ll res2=gap_dp[x+1][sum_right_pos[x][y]]-sum_down[x][y];
slope_down_dp[x][y]=max(res,res2);
}
// slope up
{
ll res=slope_up_dp_max[x+1][next_right[x][y]]-sum_down[x][y]+get_sum_left(x,y);
ll res2=full_dp[x+1]+full_sum[x]-sum_down[x][y]+get_sum_left(x,y);
slope_up_dp[x][y]=max(res,res2);
}
// gap
{
ll res=full_dp[x+1]+full_sum[x];
res=max(res,gap_dp[x+1][0]+sum_down[x][y]);
int l=-1,r=h[x+1];
while(l<r-1){
int m=(l+r)/2;
if(get_sum_left(x+1,m)>sum_down[x][y]){
r=m;
}else{
l=m;
}
}
int y_point=r;
res=max(res,dp_max1[x+1][y_point]+sum_down[x][y]);
/*for(int y2=0;y2<y_point;y2++){
ll res2=slope_up_dp[x+1][y2]-get_sum_left(x+1,y2);
res=max(res,res2);
}*/
res=max(res,dp_max2[x+1][y_point]);
/*for(int y2=y_point;y2<h[x+1];y2++){
ll res2=slope_up_dp[x+1][y2]+get_sum_left(x+1,y2)-get_sum_left(x+1,y2);
res=max(res,res2);
}*/
gap_dp[x][y]=res;
}
}
// full
{
ll res=gap_dp[x+1][h[x+1]-1];
res=max(res,full_dp[x+1]);
for(int y2=0;y2<h[x+1];y2++){
ll res2=slope_down_dp[x+1][y2]+full_sum[x+1];
res=max(res,res2);
}
full_dp[x]=res;
}
}
return gap_dp[0][0];
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
169 ms |
226496 KB |
Output is correct |
2 |
Correct |
172 ms |
233152 KB |
Output is correct |
3 |
Correct |
142 ms |
220680 KB |
Output is correct |
4 |
Correct |
176 ms |
220620 KB |
Output is correct |
5 |
Correct |
265 ms |
258204 KB |
Output is correct |
6 |
Correct |
376 ms |
254236 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
78 ms |
178764 KB |
Output is correct |
2 |
Correct |
196 ms |
236816 KB |
Output is correct |
3 |
Correct |
227 ms |
245776 KB |
Output is correct |
4 |
Correct |
180 ms |
226500 KB |
Output is correct |
5 |
Correct |
180 ms |
233128 KB |
Output is correct |
6 |
Correct |
81 ms |
178736 KB |
Output is correct |
7 |
Correct |
82 ms |
178764 KB |
Output is correct |
8 |
Correct |
82 ms |
178676 KB |
Output is correct |
9 |
Correct |
82 ms |
178764 KB |
Output is correct |
10 |
Correct |
142 ms |
220528 KB |
Output is correct |
11 |
Correct |
143 ms |
220620 KB |
Output is correct |
12 |
Correct |
166 ms |
226596 KB |
Output is correct |
13 |
Correct |
177 ms |
233152 KB |
Output is correct |
14 |
Correct |
169 ms |
226680 KB |
Output is correct |
15 |
Correct |
186 ms |
232032 KB |
Output is correct |
16 |
Correct |
167 ms |
226656 KB |
Output is correct |
17 |
Correct |
177 ms |
231872 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
146 ms |
220584 KB |
Output is correct |
2 |
Correct |
147 ms |
220620 KB |
Output is correct |
3 |
Correct |
155 ms |
218444 KB |
Output is correct |
4 |
Correct |
161 ms |
221928 KB |
Output is correct |
5 |
Correct |
223 ms |
223692 KB |
Output is correct |
6 |
Correct |
174 ms |
223720 KB |
Output is correct |
7 |
Correct |
187 ms |
223732 KB |
Output is correct |
8 |
Correct |
177 ms |
223692 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
85 ms |
178848 KB |
Output is correct |
2 |
Correct |
81 ms |
178772 KB |
Output is correct |
3 |
Correct |
81 ms |
178728 KB |
Output is correct |
4 |
Correct |
80 ms |
178672 KB |
Output is correct |
5 |
Correct |
88 ms |
178860 KB |
Output is correct |
6 |
Correct |
84 ms |
178764 KB |
Output is correct |
7 |
Correct |
83 ms |
178764 KB |
Output is correct |
8 |
Correct |
90 ms |
178768 KB |
Output is correct |
9 |
Correct |
81 ms |
178836 KB |
Output is correct |
10 |
Correct |
83 ms |
179044 KB |
Output is correct |
11 |
Correct |
81 ms |
178800 KB |
Output is correct |
12 |
Correct |
83 ms |
178892 KB |
Output is correct |
13 |
Correct |
81 ms |
178804 KB |
Output is correct |
14 |
Correct |
82 ms |
179052 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
85 ms |
178848 KB |
Output is correct |
2 |
Correct |
81 ms |
178772 KB |
Output is correct |
3 |
Correct |
81 ms |
178728 KB |
Output is correct |
4 |
Correct |
80 ms |
178672 KB |
Output is correct |
5 |
Correct |
88 ms |
178860 KB |
Output is correct |
6 |
Correct |
84 ms |
178764 KB |
Output is correct |
7 |
Correct |
83 ms |
178764 KB |
Output is correct |
8 |
Correct |
90 ms |
178768 KB |
Output is correct |
9 |
Correct |
81 ms |
178836 KB |
Output is correct |
10 |
Correct |
83 ms |
179044 KB |
Output is correct |
11 |
Correct |
81 ms |
178800 KB |
Output is correct |
12 |
Correct |
83 ms |
178892 KB |
Output is correct |
13 |
Correct |
81 ms |
178804 KB |
Output is correct |
14 |
Correct |
82 ms |
179052 KB |
Output is correct |
15 |
Correct |
85 ms |
178900 KB |
Output is correct |
16 |
Correct |
81 ms |
179072 KB |
Output is correct |
17 |
Correct |
104 ms |
184652 KB |
Output is correct |
18 |
Correct |
98 ms |
184824 KB |
Output is correct |
19 |
Correct |
98 ms |
184764 KB |
Output is correct |
20 |
Correct |
97 ms |
184788 KB |
Output is correct |
21 |
Correct |
97 ms |
184812 KB |
Output is correct |
22 |
Correct |
113 ms |
190728 KB |
Output is correct |
23 |
Correct |
91 ms |
179900 KB |
Output is correct |
24 |
Correct |
95 ms |
182724 KB |
Output is correct |
25 |
Correct |
85 ms |
178924 KB |
Output is correct |
26 |
Correct |
89 ms |
179940 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
85 ms |
178848 KB |
Output is correct |
2 |
Correct |
81 ms |
178772 KB |
Output is correct |
3 |
Correct |
81 ms |
178728 KB |
Output is correct |
4 |
Correct |
80 ms |
178672 KB |
Output is correct |
5 |
Correct |
88 ms |
178860 KB |
Output is correct |
6 |
Correct |
84 ms |
178764 KB |
Output is correct |
7 |
Correct |
83 ms |
178764 KB |
Output is correct |
8 |
Correct |
90 ms |
178768 KB |
Output is correct |
9 |
Correct |
81 ms |
178836 KB |
Output is correct |
10 |
Correct |
83 ms |
179044 KB |
Output is correct |
11 |
Correct |
81 ms |
178800 KB |
Output is correct |
12 |
Correct |
83 ms |
178892 KB |
Output is correct |
13 |
Correct |
81 ms |
178804 KB |
Output is correct |
14 |
Correct |
82 ms |
179052 KB |
Output is correct |
15 |
Correct |
85 ms |
178900 KB |
Output is correct |
16 |
Correct |
81 ms |
179072 KB |
Output is correct |
17 |
Correct |
104 ms |
184652 KB |
Output is correct |
18 |
Correct |
98 ms |
184824 KB |
Output is correct |
19 |
Correct |
98 ms |
184764 KB |
Output is correct |
20 |
Correct |
97 ms |
184788 KB |
Output is correct |
21 |
Correct |
97 ms |
184812 KB |
Output is correct |
22 |
Correct |
113 ms |
190728 KB |
Output is correct |
23 |
Correct |
91 ms |
179900 KB |
Output is correct |
24 |
Correct |
95 ms |
182724 KB |
Output is correct |
25 |
Correct |
85 ms |
178924 KB |
Output is correct |
26 |
Correct |
89 ms |
179940 KB |
Output is correct |
27 |
Correct |
83 ms |
180044 KB |
Output is correct |
28 |
Correct |
166 ms |
207380 KB |
Output is correct |
29 |
Correct |
198 ms |
218744 KB |
Output is correct |
30 |
Correct |
200 ms |
218036 KB |
Output is correct |
31 |
Correct |
204 ms |
218064 KB |
Output is correct |
32 |
Correct |
186 ms |
218776 KB |
Output is correct |
33 |
Correct |
209 ms |
218120 KB |
Output is correct |
34 |
Correct |
212 ms |
218008 KB |
Output is correct |
35 |
Correct |
136 ms |
194636 KB |
Output is correct |
36 |
Correct |
209 ms |
218784 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
146 ms |
220584 KB |
Output is correct |
2 |
Correct |
147 ms |
220620 KB |
Output is correct |
3 |
Correct |
155 ms |
218444 KB |
Output is correct |
4 |
Correct |
161 ms |
221928 KB |
Output is correct |
5 |
Correct |
223 ms |
223692 KB |
Output is correct |
6 |
Correct |
174 ms |
223720 KB |
Output is correct |
7 |
Correct |
187 ms |
223732 KB |
Output is correct |
8 |
Correct |
177 ms |
223692 KB |
Output is correct |
9 |
Correct |
192 ms |
223736 KB |
Output is correct |
10 |
Correct |
150 ms |
206584 KB |
Output is correct |
11 |
Correct |
268 ms |
234448 KB |
Output is correct |
12 |
Correct |
84 ms |
178764 KB |
Output is correct |
13 |
Correct |
84 ms |
178764 KB |
Output is correct |
14 |
Correct |
84 ms |
178768 KB |
Output is correct |
15 |
Correct |
82 ms |
178768 KB |
Output is correct |
16 |
Correct |
82 ms |
178772 KB |
Output is correct |
17 |
Correct |
80 ms |
178748 KB |
Output is correct |
18 |
Correct |
145 ms |
220620 KB |
Output is correct |
19 |
Correct |
166 ms |
220648 KB |
Output is correct |
20 |
Correct |
143 ms |
220624 KB |
Output is correct |
21 |
Correct |
145 ms |
220644 KB |
Output is correct |
22 |
Correct |
212 ms |
226292 KB |
Output is correct |
23 |
Correct |
234 ms |
234480 KB |
Output is correct |
24 |
Correct |
227 ms |
234444 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
169 ms |
226496 KB |
Output is correct |
2 |
Correct |
172 ms |
233152 KB |
Output is correct |
3 |
Correct |
142 ms |
220680 KB |
Output is correct |
4 |
Correct |
176 ms |
220620 KB |
Output is correct |
5 |
Correct |
265 ms |
258204 KB |
Output is correct |
6 |
Correct |
376 ms |
254236 KB |
Output is correct |
7 |
Correct |
78 ms |
178764 KB |
Output is correct |
8 |
Correct |
196 ms |
236816 KB |
Output is correct |
9 |
Correct |
227 ms |
245776 KB |
Output is correct |
10 |
Correct |
180 ms |
226500 KB |
Output is correct |
11 |
Correct |
180 ms |
233128 KB |
Output is correct |
12 |
Correct |
81 ms |
178736 KB |
Output is correct |
13 |
Correct |
82 ms |
178764 KB |
Output is correct |
14 |
Correct |
82 ms |
178676 KB |
Output is correct |
15 |
Correct |
82 ms |
178764 KB |
Output is correct |
16 |
Correct |
142 ms |
220528 KB |
Output is correct |
17 |
Correct |
143 ms |
220620 KB |
Output is correct |
18 |
Correct |
166 ms |
226596 KB |
Output is correct |
19 |
Correct |
177 ms |
233152 KB |
Output is correct |
20 |
Correct |
169 ms |
226680 KB |
Output is correct |
21 |
Correct |
186 ms |
232032 KB |
Output is correct |
22 |
Correct |
167 ms |
226656 KB |
Output is correct |
23 |
Correct |
177 ms |
231872 KB |
Output is correct |
24 |
Correct |
146 ms |
220584 KB |
Output is correct |
25 |
Correct |
147 ms |
220620 KB |
Output is correct |
26 |
Correct |
155 ms |
218444 KB |
Output is correct |
27 |
Correct |
161 ms |
221928 KB |
Output is correct |
28 |
Correct |
223 ms |
223692 KB |
Output is correct |
29 |
Correct |
174 ms |
223720 KB |
Output is correct |
30 |
Correct |
187 ms |
223732 KB |
Output is correct |
31 |
Correct |
177 ms |
223692 KB |
Output is correct |
32 |
Correct |
85 ms |
178848 KB |
Output is correct |
33 |
Correct |
81 ms |
178772 KB |
Output is correct |
34 |
Correct |
81 ms |
178728 KB |
Output is correct |
35 |
Correct |
80 ms |
178672 KB |
Output is correct |
36 |
Correct |
88 ms |
178860 KB |
Output is correct |
37 |
Correct |
84 ms |
178764 KB |
Output is correct |
38 |
Correct |
83 ms |
178764 KB |
Output is correct |
39 |
Correct |
90 ms |
178768 KB |
Output is correct |
40 |
Correct |
81 ms |
178836 KB |
Output is correct |
41 |
Correct |
83 ms |
179044 KB |
Output is correct |
42 |
Correct |
81 ms |
178800 KB |
Output is correct |
43 |
Correct |
83 ms |
178892 KB |
Output is correct |
44 |
Correct |
81 ms |
178804 KB |
Output is correct |
45 |
Correct |
82 ms |
179052 KB |
Output is correct |
46 |
Correct |
85 ms |
178900 KB |
Output is correct |
47 |
Correct |
81 ms |
179072 KB |
Output is correct |
48 |
Correct |
104 ms |
184652 KB |
Output is correct |
49 |
Correct |
98 ms |
184824 KB |
Output is correct |
50 |
Correct |
98 ms |
184764 KB |
Output is correct |
51 |
Correct |
97 ms |
184788 KB |
Output is correct |
52 |
Correct |
97 ms |
184812 KB |
Output is correct |
53 |
Correct |
113 ms |
190728 KB |
Output is correct |
54 |
Correct |
91 ms |
179900 KB |
Output is correct |
55 |
Correct |
95 ms |
182724 KB |
Output is correct |
56 |
Correct |
85 ms |
178924 KB |
Output is correct |
57 |
Correct |
89 ms |
179940 KB |
Output is correct |
58 |
Correct |
83 ms |
180044 KB |
Output is correct |
59 |
Correct |
166 ms |
207380 KB |
Output is correct |
60 |
Correct |
198 ms |
218744 KB |
Output is correct |
61 |
Correct |
200 ms |
218036 KB |
Output is correct |
62 |
Correct |
204 ms |
218064 KB |
Output is correct |
63 |
Correct |
186 ms |
218776 KB |
Output is correct |
64 |
Correct |
209 ms |
218120 KB |
Output is correct |
65 |
Correct |
212 ms |
218008 KB |
Output is correct |
66 |
Correct |
136 ms |
194636 KB |
Output is correct |
67 |
Correct |
209 ms |
218784 KB |
Output is correct |
68 |
Correct |
192 ms |
223736 KB |
Output is correct |
69 |
Correct |
150 ms |
206584 KB |
Output is correct |
70 |
Correct |
268 ms |
234448 KB |
Output is correct |
71 |
Correct |
84 ms |
178764 KB |
Output is correct |
72 |
Correct |
84 ms |
178764 KB |
Output is correct |
73 |
Correct |
84 ms |
178768 KB |
Output is correct |
74 |
Correct |
82 ms |
178768 KB |
Output is correct |
75 |
Correct |
82 ms |
178772 KB |
Output is correct |
76 |
Correct |
80 ms |
178748 KB |
Output is correct |
77 |
Correct |
145 ms |
220620 KB |
Output is correct |
78 |
Correct |
166 ms |
220648 KB |
Output is correct |
79 |
Correct |
143 ms |
220624 KB |
Output is correct |
80 |
Correct |
145 ms |
220644 KB |
Output is correct |
81 |
Correct |
212 ms |
226292 KB |
Output is correct |
82 |
Correct |
234 ms |
234480 KB |
Output is correct |
83 |
Correct |
227 ms |
234444 KB |
Output is correct |
84 |
Correct |
317 ms |
255272 KB |
Output is correct |
85 |
Correct |
295 ms |
259024 KB |
Output is correct |
86 |
Correct |
300 ms |
245388 KB |
Output is correct |
87 |
Correct |
320 ms |
249164 KB |
Output is correct |
88 |
Correct |
82 ms |
178764 KB |
Output is correct |
89 |
Correct |
311 ms |
249180 KB |
Output is correct |
90 |
Correct |
338 ms |
254284 KB |
Output is correct |
91 |
Correct |
295 ms |
256648 KB |
Output is correct |