#include "fish.h"
#include <bits/stdc++.h>
using namespace std;
#ifdef DEBUG
#define dbg(...) printf(__VA_ARGS__);
#define getchar_unlocked getchar
#else
#define dbg(...)
#endif
#define sf scanf
#define pf printf
#define fi first
#define se second
#define pb push_back
#define sz(x) (int)x.size()
#define mnto(x,y) x=min(x,(__typeof__(x))y)
#define mxto(x,y) x=max(x,(__typeof__(x))y)
#define INF 1023456789
#define LINF 1023456789123456789
#define all(x) x.begin(), x.end()
#define lb(x,v) (lower_bound(all(x),v)-x.begin())
#define ub(x,v) (upper_bound(all(x),v)-x.begin())
#define disc(x) sort(all(x));x.resize(unique(all(x))-x.begin());
typedef long long ll;
typedef long double ld;
typedef pair<int,int> ii;
typedef pair<ll,ll> pll;
typedef tuple<int,int,int> iii;
typedef tuple<int,int,int,int> iiii;
typedef vector<int> vi;
typedef vector<ll> vll;
typedef vector<ii> vii;
mt19937 rng(time(0));
#define mod 1000000007
#define maxn 100005
int num[maxn];
vector<int> yval[maxn],impt[maxn];
vector<ii> ywpr[maxn];
vector<ll> wpfx[maxn];
vector<ll> dp[2][maxn],dppfx[maxn],dpsfx[maxn],dppfx2[maxn];
ll max_weights(int N,int M,vi X,vi Y,vi W){
for(int i=0;i<M;++i){
++X[i],++Y[i];
yval[X[i]].pb(Y[i]);
ywpr[X[i]].pb({Y[i],W[i]});
if(X[i]-1>=1)impt[X[i]-1].pb(Y[i]);
if(X[i]+1<=N)impt[X[i]+1].pb(Y[i]);
}
for(int i=0;i<=N+1;++i){
yval[i].pb(0);
ywpr[i].pb({0,0});
sort(all(yval[i]));
sort(all(ywpr[i]));
wpfx[i].resize(yval[i].size()+5,0);
for(int j=1;j<yval[i].size();++j){
wpfx[i][j]+=wpfx[i][j-1]+ywpr[i][j].se;
}
impt[i].pb(0);
disc(impt[i]);
num[i]=impt[i].size();
for(int x=0;x<2;++x){
dp[x][i].resize(num[i]+5,0);
dppfx[i].resize(num[i]+5,0);
dpsfx[i].resize(num[i]+5,0);
dppfx2[i].resize(num[i]+5,0);
}
}
for(int pos=N;pos>=0;--pos){
for(int id=0;id<num[pos];++id){
int h=impt[pos][id];
dbg("%d\n",h);
//transition strictly downwards
{
int i=lb(impt[pos+1],h)-1;
int y=ub(yval[pos+1],h)-1;
dbg("down i=%d y=%d\n",i,y);
if(h!=0){
mxto(dp[1][pos][id],dppfx[pos+1][i]+wpfx[pos+1][y]);
if(pos<=N-2){
int i2=ub(impt[pos+2],h)-1;
int y2=ub(yval[pos+1],h)-1;
dbg("i2=%d, y2=%d\n",i2,y2);
mxto(dp[1][pos][id],dppfx2[pos+2][i2]+wpfx[pos+1][y2]);//x 0 y<=x
mxto(dp[1][pos][id],dpsfx[pos+2][i2+1]);//x 0 y>x
}
}
}
//transition upwards
{
int i=lb(impt[pos+1],h);
int y=ub(yval[pos],h)-1;
dbg("up i=%d y=%d\n",i,y);
if(i!=impt[pos+1].size()){
mxto(dp[0][pos][id],dpsfx[pos+1][i]-wpfx[pos][y]);
}
if(h!=0){
mxto(dp[0][pos][id],dp[1][pos][id]);
}
}
dbg("dp[%d][%d][%d]: %d\n",0,pos,id,dp[0][pos][id]);
dbg("dp[%d][%d][%d]: %d\n",1,pos,id,dp[1][pos][id]);
dbg("\n");
if(pos!=0){
int i=ub(yval[pos],h)-1;
if(id!=0)dppfx[pos][id]=dp[1][pos][id]-wpfx[pos][i];
dppfx2[pos][id]=dp[0][pos][id];
i=ub(yval[pos-1],h)-1;
dpsfx[pos][id]=dp[0][pos][id]+wpfx[pos-1][i];
}
}
for(int id=1;id<num[pos];++id){
mxto(dppfx[pos][id],dppfx[pos][id-1]);
mxto(dppfx2[pos][id],dppfx2[pos][id-1]);
}
for(int id=num[pos]-2;id>=0;--id){
mxto(dpsfx[pos][id],dpsfx[pos][id+1]);
}
#ifdef DEBUG
pf("dppfx[%d]: ",pos);
for(int id=0;id<num[pos];++id)pf("%d ",dppfx[pos][id]);
pf("\n");
pf("dppfx2[%d]: ",pos);
for(int id=0;id<num[pos];++id)pf("%d ",dppfx2[pos][id]);
pf("\n");
pf("dpsfx[%d]: ",pos);
for(int id=0;id<num[pos];++id)pf("%d ",dpsfx[pos][id]);
pf("\n\n");
#endif
}
return dp[0][0][0];
}
Compilation message
fish.cpp: In function 'll max_weights(int, int, vi, vi, vi)':
fish.cpp:60:16: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
60 | for(int j=1;j<yval[i].size();++j){
| ~^~~~~~~~~~~~~~~
fish.cpp:101:9: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
101 | if(i!=impt[pos+1].size()){
| ~^~~~~~~~~~~~~~~~~~~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
153 ms |
70976 KB |
Output is correct |
2 |
Correct |
175 ms |
77392 KB |
Output is correct |
3 |
Correct |
83 ms |
68684 KB |
Output is correct |
4 |
Correct |
83 ms |
68676 KB |
Output is correct |
5 |
Correct |
351 ms |
107996 KB |
Output is correct |
6 |
Correct |
515 ms |
107648 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
11 ms |
21332 KB |
Output is correct |
2 |
Correct |
249 ms |
81832 KB |
Output is correct |
3 |
Correct |
282 ms |
90652 KB |
Output is correct |
4 |
Correct |
160 ms |
71032 KB |
Output is correct |
5 |
Correct |
173 ms |
77348 KB |
Output is correct |
6 |
Correct |
10 ms |
21332 KB |
Output is correct |
7 |
Correct |
11 ms |
21332 KB |
Output is correct |
8 |
Correct |
11 ms |
21460 KB |
Output is correct |
9 |
Correct |
11 ms |
21332 KB |
Output is correct |
10 |
Correct |
82 ms |
68732 KB |
Output is correct |
11 |
Correct |
82 ms |
68704 KB |
Output is correct |
12 |
Correct |
176 ms |
74884 KB |
Output is correct |
13 |
Correct |
213 ms |
81884 KB |
Output is correct |
14 |
Correct |
165 ms |
72948 KB |
Output is correct |
15 |
Correct |
214 ms |
78764 KB |
Output is correct |
16 |
Correct |
174 ms |
72908 KB |
Output is correct |
17 |
Correct |
182 ms |
78540 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
83 ms |
68780 KB |
Output is correct |
2 |
Correct |
88 ms |
68820 KB |
Output is correct |
3 |
Correct |
136 ms |
65228 KB |
Output is correct |
4 |
Correct |
126 ms |
69704 KB |
Output is correct |
5 |
Correct |
185 ms |
71188 KB |
Output is correct |
6 |
Correct |
182 ms |
71756 KB |
Output is correct |
7 |
Correct |
204 ms |
72428 KB |
Output is correct |
8 |
Correct |
189 ms |
72460 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
11 ms |
21420 KB |
Output is correct |
2 |
Correct |
11 ms |
21348 KB |
Output is correct |
3 |
Correct |
10 ms |
21332 KB |
Output is correct |
4 |
Correct |
11 ms |
21404 KB |
Output is correct |
5 |
Correct |
11 ms |
21392 KB |
Output is correct |
6 |
Correct |
11 ms |
21460 KB |
Output is correct |
7 |
Correct |
11 ms |
21332 KB |
Output is correct |
8 |
Correct |
11 ms |
21456 KB |
Output is correct |
9 |
Correct |
12 ms |
21588 KB |
Output is correct |
10 |
Correct |
12 ms |
21844 KB |
Output is correct |
11 |
Correct |
11 ms |
21604 KB |
Output is correct |
12 |
Correct |
11 ms |
21716 KB |
Output is correct |
13 |
Correct |
11 ms |
21460 KB |
Output is correct |
14 |
Correct |
12 ms |
21588 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
11 ms |
21420 KB |
Output is correct |
2 |
Correct |
11 ms |
21348 KB |
Output is correct |
3 |
Correct |
10 ms |
21332 KB |
Output is correct |
4 |
Correct |
11 ms |
21404 KB |
Output is correct |
5 |
Correct |
11 ms |
21392 KB |
Output is correct |
6 |
Correct |
11 ms |
21460 KB |
Output is correct |
7 |
Correct |
11 ms |
21332 KB |
Output is correct |
8 |
Correct |
11 ms |
21456 KB |
Output is correct |
9 |
Correct |
12 ms |
21588 KB |
Output is correct |
10 |
Correct |
12 ms |
21844 KB |
Output is correct |
11 |
Correct |
11 ms |
21604 KB |
Output is correct |
12 |
Correct |
11 ms |
21716 KB |
Output is correct |
13 |
Correct |
11 ms |
21460 KB |
Output is correct |
14 |
Correct |
12 ms |
21588 KB |
Output is correct |
15 |
Correct |
11 ms |
21620 KB |
Output is correct |
16 |
Correct |
12 ms |
21596 KB |
Output is correct |
17 |
Correct |
49 ms |
26928 KB |
Output is correct |
18 |
Correct |
52 ms |
27412 KB |
Output is correct |
19 |
Correct |
48 ms |
27340 KB |
Output is correct |
20 |
Correct |
46 ms |
26960 KB |
Output is correct |
21 |
Correct |
41 ms |
26924 KB |
Output is correct |
22 |
Correct |
73 ms |
32464 KB |
Output is correct |
23 |
Correct |
20 ms |
22728 KB |
Output is correct |
24 |
Correct |
45 ms |
25620 KB |
Output is correct |
25 |
Correct |
12 ms |
21612 KB |
Output is correct |
26 |
Correct |
19 ms |
22588 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
11 ms |
21420 KB |
Output is correct |
2 |
Correct |
11 ms |
21348 KB |
Output is correct |
3 |
Correct |
10 ms |
21332 KB |
Output is correct |
4 |
Correct |
11 ms |
21404 KB |
Output is correct |
5 |
Correct |
11 ms |
21392 KB |
Output is correct |
6 |
Correct |
11 ms |
21460 KB |
Output is correct |
7 |
Correct |
11 ms |
21332 KB |
Output is correct |
8 |
Correct |
11 ms |
21456 KB |
Output is correct |
9 |
Correct |
12 ms |
21588 KB |
Output is correct |
10 |
Correct |
12 ms |
21844 KB |
Output is correct |
11 |
Correct |
11 ms |
21604 KB |
Output is correct |
12 |
Correct |
11 ms |
21716 KB |
Output is correct |
13 |
Correct |
11 ms |
21460 KB |
Output is correct |
14 |
Correct |
12 ms |
21588 KB |
Output is correct |
15 |
Correct |
11 ms |
21620 KB |
Output is correct |
16 |
Correct |
12 ms |
21596 KB |
Output is correct |
17 |
Correct |
49 ms |
26928 KB |
Output is correct |
18 |
Correct |
52 ms |
27412 KB |
Output is correct |
19 |
Correct |
48 ms |
27340 KB |
Output is correct |
20 |
Correct |
46 ms |
26960 KB |
Output is correct |
21 |
Correct |
41 ms |
26924 KB |
Output is correct |
22 |
Correct |
73 ms |
32464 KB |
Output is correct |
23 |
Correct |
20 ms |
22728 KB |
Output is correct |
24 |
Correct |
45 ms |
25620 KB |
Output is correct |
25 |
Correct |
12 ms |
21612 KB |
Output is correct |
26 |
Correct |
19 ms |
22588 KB |
Output is correct |
27 |
Correct |
15 ms |
23080 KB |
Output is correct |
28 |
Correct |
208 ms |
48904 KB |
Output is correct |
29 |
Correct |
294 ms |
61116 KB |
Output is correct |
30 |
Correct |
312 ms |
65736 KB |
Output is correct |
31 |
Correct |
312 ms |
65528 KB |
Output is correct |
32 |
Correct |
230 ms |
56744 KB |
Output is correct |
33 |
Correct |
321 ms |
65996 KB |
Output is correct |
34 |
Correct |
317 ms |
65996 KB |
Output is correct |
35 |
Correct |
116 ms |
39188 KB |
Output is correct |
36 |
Correct |
320 ms |
65408 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
83 ms |
68780 KB |
Output is correct |
2 |
Correct |
88 ms |
68820 KB |
Output is correct |
3 |
Correct |
136 ms |
65228 KB |
Output is correct |
4 |
Correct |
126 ms |
69704 KB |
Output is correct |
5 |
Correct |
185 ms |
71188 KB |
Output is correct |
6 |
Correct |
182 ms |
71756 KB |
Output is correct |
7 |
Correct |
204 ms |
72428 KB |
Output is correct |
8 |
Correct |
189 ms |
72460 KB |
Output is correct |
9 |
Correct |
193 ms |
79528 KB |
Output is correct |
10 |
Correct |
137 ms |
55892 KB |
Output is correct |
11 |
Correct |
298 ms |
90896 KB |
Output is correct |
12 |
Correct |
11 ms |
21376 KB |
Output is correct |
13 |
Correct |
11 ms |
21452 KB |
Output is correct |
14 |
Correct |
11 ms |
21460 KB |
Output is correct |
15 |
Correct |
11 ms |
21460 KB |
Output is correct |
16 |
Correct |
11 ms |
21332 KB |
Output is correct |
17 |
Correct |
10 ms |
21372 KB |
Output is correct |
18 |
Correct |
82 ms |
68684 KB |
Output is correct |
19 |
Correct |
82 ms |
68792 KB |
Output is correct |
20 |
Correct |
82 ms |
68684 KB |
Output is correct |
21 |
Correct |
82 ms |
68904 KB |
Output is correct |
22 |
Correct |
253 ms |
79780 KB |
Output is correct |
23 |
Correct |
367 ms |
93644 KB |
Output is correct |
24 |
Correct |
347 ms |
96804 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
153 ms |
70976 KB |
Output is correct |
2 |
Correct |
175 ms |
77392 KB |
Output is correct |
3 |
Correct |
83 ms |
68684 KB |
Output is correct |
4 |
Correct |
83 ms |
68676 KB |
Output is correct |
5 |
Correct |
351 ms |
107996 KB |
Output is correct |
6 |
Correct |
515 ms |
107648 KB |
Output is correct |
7 |
Correct |
11 ms |
21332 KB |
Output is correct |
8 |
Correct |
249 ms |
81832 KB |
Output is correct |
9 |
Correct |
282 ms |
90652 KB |
Output is correct |
10 |
Correct |
160 ms |
71032 KB |
Output is correct |
11 |
Correct |
173 ms |
77348 KB |
Output is correct |
12 |
Correct |
10 ms |
21332 KB |
Output is correct |
13 |
Correct |
11 ms |
21332 KB |
Output is correct |
14 |
Correct |
11 ms |
21460 KB |
Output is correct |
15 |
Correct |
11 ms |
21332 KB |
Output is correct |
16 |
Correct |
82 ms |
68732 KB |
Output is correct |
17 |
Correct |
82 ms |
68704 KB |
Output is correct |
18 |
Correct |
176 ms |
74884 KB |
Output is correct |
19 |
Correct |
213 ms |
81884 KB |
Output is correct |
20 |
Correct |
165 ms |
72948 KB |
Output is correct |
21 |
Correct |
214 ms |
78764 KB |
Output is correct |
22 |
Correct |
174 ms |
72908 KB |
Output is correct |
23 |
Correct |
182 ms |
78540 KB |
Output is correct |
24 |
Correct |
83 ms |
68780 KB |
Output is correct |
25 |
Correct |
88 ms |
68820 KB |
Output is correct |
26 |
Correct |
136 ms |
65228 KB |
Output is correct |
27 |
Correct |
126 ms |
69704 KB |
Output is correct |
28 |
Correct |
185 ms |
71188 KB |
Output is correct |
29 |
Correct |
182 ms |
71756 KB |
Output is correct |
30 |
Correct |
204 ms |
72428 KB |
Output is correct |
31 |
Correct |
189 ms |
72460 KB |
Output is correct |
32 |
Correct |
11 ms |
21420 KB |
Output is correct |
33 |
Correct |
11 ms |
21348 KB |
Output is correct |
34 |
Correct |
10 ms |
21332 KB |
Output is correct |
35 |
Correct |
11 ms |
21404 KB |
Output is correct |
36 |
Correct |
11 ms |
21392 KB |
Output is correct |
37 |
Correct |
11 ms |
21460 KB |
Output is correct |
38 |
Correct |
11 ms |
21332 KB |
Output is correct |
39 |
Correct |
11 ms |
21456 KB |
Output is correct |
40 |
Correct |
12 ms |
21588 KB |
Output is correct |
41 |
Correct |
12 ms |
21844 KB |
Output is correct |
42 |
Correct |
11 ms |
21604 KB |
Output is correct |
43 |
Correct |
11 ms |
21716 KB |
Output is correct |
44 |
Correct |
11 ms |
21460 KB |
Output is correct |
45 |
Correct |
12 ms |
21588 KB |
Output is correct |
46 |
Correct |
11 ms |
21620 KB |
Output is correct |
47 |
Correct |
12 ms |
21596 KB |
Output is correct |
48 |
Correct |
49 ms |
26928 KB |
Output is correct |
49 |
Correct |
52 ms |
27412 KB |
Output is correct |
50 |
Correct |
48 ms |
27340 KB |
Output is correct |
51 |
Correct |
46 ms |
26960 KB |
Output is correct |
52 |
Correct |
41 ms |
26924 KB |
Output is correct |
53 |
Correct |
73 ms |
32464 KB |
Output is correct |
54 |
Correct |
20 ms |
22728 KB |
Output is correct |
55 |
Correct |
45 ms |
25620 KB |
Output is correct |
56 |
Correct |
12 ms |
21612 KB |
Output is correct |
57 |
Correct |
19 ms |
22588 KB |
Output is correct |
58 |
Correct |
15 ms |
23080 KB |
Output is correct |
59 |
Correct |
208 ms |
48904 KB |
Output is correct |
60 |
Correct |
294 ms |
61116 KB |
Output is correct |
61 |
Correct |
312 ms |
65736 KB |
Output is correct |
62 |
Correct |
312 ms |
65528 KB |
Output is correct |
63 |
Correct |
230 ms |
56744 KB |
Output is correct |
64 |
Correct |
321 ms |
65996 KB |
Output is correct |
65 |
Correct |
317 ms |
65996 KB |
Output is correct |
66 |
Correct |
116 ms |
39188 KB |
Output is correct |
67 |
Correct |
320 ms |
65408 KB |
Output is correct |
68 |
Correct |
193 ms |
79528 KB |
Output is correct |
69 |
Correct |
137 ms |
55892 KB |
Output is correct |
70 |
Correct |
298 ms |
90896 KB |
Output is correct |
71 |
Correct |
11 ms |
21376 KB |
Output is correct |
72 |
Correct |
11 ms |
21452 KB |
Output is correct |
73 |
Correct |
11 ms |
21460 KB |
Output is correct |
74 |
Correct |
11 ms |
21460 KB |
Output is correct |
75 |
Correct |
11 ms |
21332 KB |
Output is correct |
76 |
Correct |
10 ms |
21372 KB |
Output is correct |
77 |
Correct |
82 ms |
68684 KB |
Output is correct |
78 |
Correct |
82 ms |
68792 KB |
Output is correct |
79 |
Correct |
82 ms |
68684 KB |
Output is correct |
80 |
Correct |
82 ms |
68904 KB |
Output is correct |
81 |
Correct |
253 ms |
79780 KB |
Output is correct |
82 |
Correct |
367 ms |
93644 KB |
Output is correct |
83 |
Correct |
347 ms |
96804 KB |
Output is correct |
84 |
Correct |
442 ms |
107508 KB |
Output is correct |
85 |
Correct |
449 ms |
110492 KB |
Output is correct |
86 |
Correct |
471 ms |
103472 KB |
Output is correct |
87 |
Correct |
499 ms |
106444 KB |
Output is correct |
88 |
Correct |
11 ms |
21376 KB |
Output is correct |
89 |
Correct |
511 ms |
107052 KB |
Output is correct |
90 |
Correct |
441 ms |
104396 KB |
Output is correct |
91 |
Correct |
357 ms |
100296 KB |
Output is correct |