#include "fish.h"
#include <vector>
#include<bits/stdc++.h>
using namespace std;
#define rep(i,n) for(int i=0; i<(n); i++)
#define rrep(i,n) for(int i=(n)-1; i>=0; i--)
#define rng(i,l,r) for(int i=(l); i<(r); i++)
#define all(x) x.begin(),x.end()
using ll=long long;
constexpr ll INF=1LL<<60;
long long max_weights(int N, int M, std::vector<int> X, std::vector<int> Y,
std::vector<int> W) {
vector<vector<pair<int,ll>>> fish(N);
rep(i,M) {
fish[X[i]].emplace_back(Y[i],W[i]);
}
rep(i,N) {
fish[i].emplace_back(N,0);
sort(all(fish[i]));
}
vector<vector<int>> pos(N);
vector<vector<ll>> cum_lf(N);
vector<vector<ll>> cum_ri(N);
vector<vector<ll>> cum_hr(N);
vector<vector<ll>> updp(N);
vector<vector<ll>> dndp(N);
vector<int> sz(N);
rep(i,N) {
sz[i]=fish[i].size();
updp[i].resize(sz[i],-INF);
dndp[i].resize(sz[i],-INF);
if(i!=0) {
ll cum=0;
int cnt=0;
for(auto &[y,_]:fish[i]){
while(fish[i-1][cnt].first<y){
cum+=fish[i-1][cnt].second;
cnt++;
}
cum_lf[i].emplace_back(cum);
}
}
if(i!=N-1){
ll cum=0;
int cnt=0;
for(auto &[y,_]:fish[i]){
while(fish[i+1][cnt].first<y){
cum+=fish[i+1][cnt].second;
cnt++;
}
cum_ri[i].emplace_back(cum);
}
}
cum_hr[i].emplace_back(0);
for(auto &[_,w]:fish[i]) {
cum_hr[i].emplace_back(cum_hr[i].back()+w);
}
}
rep(i,sz[0]) {
updp[0][i]=0;
dndp[0][i]=0;
}
rng(i,1,N) {
int cnt=0;
ll mx=-INF;
rep(j,sz[i]) {
while(cnt<sz[i-1]&&fish[i-1][cnt].first<=fish[i][j].first) {
mx=max(mx,updp[i-1][cnt]-cum_hr[i-1][cnt]);
cnt++;
}
updp[i][j]=mx+cum_lf[i][j];
}
cnt=sz[i-1]-1;
mx=-INF;
rrep(j,sz[i]) {
while(cnt>=0&&fish[i-1][cnt].first>=fish[i][j].first) {
mx=max(mx,dndp[i-1][cnt]+cum_ri[i-1][cnt]);
cnt--;
}
dndp[i][j]=mx-cum_hr[i][j];
}
if(i>=2) {
cnt=0;
mx=-INF;
ll mxa=-INF;
rep(j,sz[i-2]) {
mx=max(mx,dndp[i-2][j]);
mxa=max(mxa,dndp[i-2][j]+cum_ri[i-2][cnt]);
}
rep(j,sz[i]) {
updp[i][j]=max(updp[i][j],mxa);
updp[i][j]=max(updp[i][j],mx+cum_lf[i][j]);
}
}
rep(j,sz[i]) {
dndp[i][j]=max(dndp[i][j],updp[i][j]);
}
}
ll ans=-INF;
for(ll i:dndp.back())ans=max(ans,i);
return ans;
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
48 ms |
38080 KB |
Output is correct |
2 |
Correct |
54 ms |
42936 KB |
Output is correct |
3 |
Correct |
34 ms |
35924 KB |
Output is correct |
4 |
Correct |
31 ms |
35924 KB |
Output is correct |
5 |
Correct |
109 ms |
59604 KB |
Output is correct |
6 |
Correct |
163 ms |
62680 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
344 KB |
Output is correct |
2 |
Correct |
71 ms |
44372 KB |
Output is correct |
3 |
Correct |
94 ms |
50748 KB |
Output is correct |
4 |
Correct |
55 ms |
38196 KB |
Output is correct |
5 |
Correct |
65 ms |
43108 KB |
Output is correct |
6 |
Correct |
1 ms |
348 KB |
Output is correct |
7 |
Correct |
0 ms |
348 KB |
Output is correct |
8 |
Correct |
0 ms |
348 KB |
Output is correct |
9 |
Correct |
0 ms |
348 KB |
Output is correct |
10 |
Correct |
33 ms |
35836 KB |
Output is correct |
11 |
Correct |
33 ms |
35932 KB |
Output is correct |
12 |
Correct |
47 ms |
38628 KB |
Output is correct |
13 |
Correct |
57 ms |
43716 KB |
Output is correct |
14 |
Correct |
48 ms |
38344 KB |
Output is correct |
15 |
Correct |
57 ms |
42684 KB |
Output is correct |
16 |
Correct |
49 ms |
38600 KB |
Output is correct |
17 |
Correct |
55 ms |
42680 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
33 ms |
35932 KB |
Output is correct |
2 |
Correct |
32 ms |
35932 KB |
Output is correct |
3 |
Correct |
59 ms |
35492 KB |
Output is correct |
4 |
Correct |
46 ms |
38036 KB |
Output is correct |
5 |
Correct |
69 ms |
41812 KB |
Output is correct |
6 |
Correct |
60 ms |
41812 KB |
Output is correct |
7 |
Correct |
67 ms |
41952 KB |
Output is correct |
8 |
Correct |
64 ms |
41984 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
348 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
0 ms |
348 KB |
Output is correct |
4 |
Correct |
0 ms |
348 KB |
Output is correct |
5 |
Correct |
0 ms |
348 KB |
Output is correct |
6 |
Correct |
0 ms |
348 KB |
Output is correct |
7 |
Correct |
0 ms |
348 KB |
Output is correct |
8 |
Correct |
0 ms |
348 KB |
Output is correct |
9 |
Correct |
0 ms |
348 KB |
Output is correct |
10 |
Correct |
1 ms |
600 KB |
Output is correct |
11 |
Correct |
1 ms |
348 KB |
Output is correct |
12 |
Correct |
1 ms |
604 KB |
Output is correct |
13 |
Correct |
0 ms |
344 KB |
Output is correct |
14 |
Correct |
0 ms |
604 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
348 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
0 ms |
348 KB |
Output is correct |
4 |
Correct |
0 ms |
348 KB |
Output is correct |
5 |
Correct |
0 ms |
348 KB |
Output is correct |
6 |
Correct |
0 ms |
348 KB |
Output is correct |
7 |
Correct |
0 ms |
348 KB |
Output is correct |
8 |
Correct |
0 ms |
348 KB |
Output is correct |
9 |
Correct |
0 ms |
348 KB |
Output is correct |
10 |
Correct |
1 ms |
600 KB |
Output is correct |
11 |
Correct |
1 ms |
348 KB |
Output is correct |
12 |
Correct |
1 ms |
604 KB |
Output is correct |
13 |
Correct |
0 ms |
344 KB |
Output is correct |
14 |
Correct |
0 ms |
604 KB |
Output is correct |
15 |
Correct |
0 ms |
348 KB |
Output is correct |
16 |
Correct |
1 ms |
600 KB |
Output is correct |
17 |
Correct |
11 ms |
4832 KB |
Output is correct |
18 |
Correct |
11 ms |
6236 KB |
Output is correct |
19 |
Correct |
11 ms |
5980 KB |
Output is correct |
20 |
Correct |
11 ms |
5980 KB |
Output is correct |
21 |
Correct |
10 ms |
5980 KB |
Output is correct |
22 |
Correct |
20 ms |
11464 KB |
Output is correct |
23 |
Correct |
4 ms |
1416 KB |
Output is correct |
24 |
Correct |
8 ms |
3588 KB |
Output is correct |
25 |
Correct |
1 ms |
604 KB |
Output is correct |
26 |
Correct |
3 ms |
1372 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
348 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
0 ms |
348 KB |
Output is correct |
4 |
Correct |
0 ms |
348 KB |
Output is correct |
5 |
Correct |
0 ms |
348 KB |
Output is correct |
6 |
Correct |
0 ms |
348 KB |
Output is correct |
7 |
Correct |
0 ms |
348 KB |
Output is correct |
8 |
Correct |
0 ms |
348 KB |
Output is correct |
9 |
Correct |
0 ms |
348 KB |
Output is correct |
10 |
Correct |
1 ms |
600 KB |
Output is correct |
11 |
Correct |
1 ms |
348 KB |
Output is correct |
12 |
Correct |
1 ms |
604 KB |
Output is correct |
13 |
Correct |
0 ms |
344 KB |
Output is correct |
14 |
Correct |
0 ms |
604 KB |
Output is correct |
15 |
Correct |
0 ms |
348 KB |
Output is correct |
16 |
Correct |
1 ms |
600 KB |
Output is correct |
17 |
Correct |
11 ms |
4832 KB |
Output is correct |
18 |
Correct |
11 ms |
6236 KB |
Output is correct |
19 |
Correct |
11 ms |
5980 KB |
Output is correct |
20 |
Correct |
11 ms |
5980 KB |
Output is correct |
21 |
Correct |
10 ms |
5980 KB |
Output is correct |
22 |
Correct |
20 ms |
11464 KB |
Output is correct |
23 |
Correct |
4 ms |
1416 KB |
Output is correct |
24 |
Correct |
8 ms |
3588 KB |
Output is correct |
25 |
Correct |
1 ms |
604 KB |
Output is correct |
26 |
Correct |
3 ms |
1372 KB |
Output is correct |
27 |
Correct |
2 ms |
1628 KB |
Output is correct |
28 |
Correct |
84 ms |
26640 KB |
Output is correct |
29 |
Correct |
76 ms |
34076 KB |
Output is correct |
30 |
Correct |
90 ms |
33636 KB |
Output is correct |
31 |
Correct |
85 ms |
33616 KB |
Output is correct |
32 |
Correct |
74 ms |
38356 KB |
Output is correct |
33 |
Correct |
102 ms |
33624 KB |
Output is correct |
34 |
Correct |
101 ms |
33512 KB |
Output is correct |
35 |
Correct |
31 ms |
14596 KB |
Output is correct |
36 |
Correct |
80 ms |
37216 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
33 ms |
35932 KB |
Output is correct |
2 |
Correct |
32 ms |
35932 KB |
Output is correct |
3 |
Correct |
59 ms |
35492 KB |
Output is correct |
4 |
Correct |
46 ms |
38036 KB |
Output is correct |
5 |
Correct |
69 ms |
41812 KB |
Output is correct |
6 |
Correct |
60 ms |
41812 KB |
Output is correct |
7 |
Correct |
67 ms |
41952 KB |
Output is correct |
8 |
Correct |
64 ms |
41984 KB |
Output is correct |
9 |
Correct |
69 ms |
42024 KB |
Output is correct |
10 |
Correct |
43 ms |
25300 KB |
Output is correct |
11 |
Correct |
96 ms |
53848 KB |
Output is correct |
12 |
Correct |
0 ms |
348 KB |
Output is correct |
13 |
Correct |
0 ms |
348 KB |
Output is correct |
14 |
Correct |
0 ms |
348 KB |
Output is correct |
15 |
Correct |
0 ms |
348 KB |
Output is correct |
16 |
Correct |
1 ms |
348 KB |
Output is correct |
17 |
Correct |
0 ms |
348 KB |
Output is correct |
18 |
Correct |
34 ms |
35932 KB |
Output is correct |
19 |
Correct |
31 ms |
35932 KB |
Output is correct |
20 |
Correct |
32 ms |
35840 KB |
Output is correct |
21 |
Correct |
31 ms |
35932 KB |
Output is correct |
22 |
Correct |
73 ms |
44668 KB |
Output is correct |
23 |
Correct |
136 ms |
53848 KB |
Output is correct |
24 |
Correct |
120 ms |
54612 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
48 ms |
38080 KB |
Output is correct |
2 |
Correct |
54 ms |
42936 KB |
Output is correct |
3 |
Correct |
34 ms |
35924 KB |
Output is correct |
4 |
Correct |
31 ms |
35924 KB |
Output is correct |
5 |
Correct |
109 ms |
59604 KB |
Output is correct |
6 |
Correct |
163 ms |
62680 KB |
Output is correct |
7 |
Correct |
1 ms |
344 KB |
Output is correct |
8 |
Correct |
71 ms |
44372 KB |
Output is correct |
9 |
Correct |
94 ms |
50748 KB |
Output is correct |
10 |
Correct |
55 ms |
38196 KB |
Output is correct |
11 |
Correct |
65 ms |
43108 KB |
Output is correct |
12 |
Correct |
1 ms |
348 KB |
Output is correct |
13 |
Correct |
0 ms |
348 KB |
Output is correct |
14 |
Correct |
0 ms |
348 KB |
Output is correct |
15 |
Correct |
0 ms |
348 KB |
Output is correct |
16 |
Correct |
33 ms |
35836 KB |
Output is correct |
17 |
Correct |
33 ms |
35932 KB |
Output is correct |
18 |
Correct |
47 ms |
38628 KB |
Output is correct |
19 |
Correct |
57 ms |
43716 KB |
Output is correct |
20 |
Correct |
48 ms |
38344 KB |
Output is correct |
21 |
Correct |
57 ms |
42684 KB |
Output is correct |
22 |
Correct |
49 ms |
38600 KB |
Output is correct |
23 |
Correct |
55 ms |
42680 KB |
Output is correct |
24 |
Correct |
33 ms |
35932 KB |
Output is correct |
25 |
Correct |
32 ms |
35932 KB |
Output is correct |
26 |
Correct |
59 ms |
35492 KB |
Output is correct |
27 |
Correct |
46 ms |
38036 KB |
Output is correct |
28 |
Correct |
69 ms |
41812 KB |
Output is correct |
29 |
Correct |
60 ms |
41812 KB |
Output is correct |
30 |
Correct |
67 ms |
41952 KB |
Output is correct |
31 |
Correct |
64 ms |
41984 KB |
Output is correct |
32 |
Correct |
0 ms |
348 KB |
Output is correct |
33 |
Correct |
0 ms |
348 KB |
Output is correct |
34 |
Correct |
0 ms |
348 KB |
Output is correct |
35 |
Correct |
0 ms |
348 KB |
Output is correct |
36 |
Correct |
0 ms |
348 KB |
Output is correct |
37 |
Correct |
0 ms |
348 KB |
Output is correct |
38 |
Correct |
0 ms |
348 KB |
Output is correct |
39 |
Correct |
0 ms |
348 KB |
Output is correct |
40 |
Correct |
0 ms |
348 KB |
Output is correct |
41 |
Correct |
1 ms |
600 KB |
Output is correct |
42 |
Correct |
1 ms |
348 KB |
Output is correct |
43 |
Correct |
1 ms |
604 KB |
Output is correct |
44 |
Correct |
0 ms |
344 KB |
Output is correct |
45 |
Correct |
0 ms |
604 KB |
Output is correct |
46 |
Correct |
0 ms |
348 KB |
Output is correct |
47 |
Correct |
1 ms |
600 KB |
Output is correct |
48 |
Correct |
11 ms |
4832 KB |
Output is correct |
49 |
Correct |
11 ms |
6236 KB |
Output is correct |
50 |
Correct |
11 ms |
5980 KB |
Output is correct |
51 |
Correct |
11 ms |
5980 KB |
Output is correct |
52 |
Correct |
10 ms |
5980 KB |
Output is correct |
53 |
Correct |
20 ms |
11464 KB |
Output is correct |
54 |
Correct |
4 ms |
1416 KB |
Output is correct |
55 |
Correct |
8 ms |
3588 KB |
Output is correct |
56 |
Correct |
1 ms |
604 KB |
Output is correct |
57 |
Correct |
3 ms |
1372 KB |
Output is correct |
58 |
Correct |
2 ms |
1628 KB |
Output is correct |
59 |
Correct |
84 ms |
26640 KB |
Output is correct |
60 |
Correct |
76 ms |
34076 KB |
Output is correct |
61 |
Correct |
90 ms |
33636 KB |
Output is correct |
62 |
Correct |
85 ms |
33616 KB |
Output is correct |
63 |
Correct |
74 ms |
38356 KB |
Output is correct |
64 |
Correct |
102 ms |
33624 KB |
Output is correct |
65 |
Correct |
101 ms |
33512 KB |
Output is correct |
66 |
Correct |
31 ms |
14596 KB |
Output is correct |
67 |
Correct |
80 ms |
37216 KB |
Output is correct |
68 |
Correct |
69 ms |
42024 KB |
Output is correct |
69 |
Correct |
43 ms |
25300 KB |
Output is correct |
70 |
Correct |
96 ms |
53848 KB |
Output is correct |
71 |
Correct |
0 ms |
348 KB |
Output is correct |
72 |
Correct |
0 ms |
348 KB |
Output is correct |
73 |
Correct |
0 ms |
348 KB |
Output is correct |
74 |
Correct |
0 ms |
348 KB |
Output is correct |
75 |
Correct |
1 ms |
348 KB |
Output is correct |
76 |
Correct |
0 ms |
348 KB |
Output is correct |
77 |
Correct |
34 ms |
35932 KB |
Output is correct |
78 |
Correct |
31 ms |
35932 KB |
Output is correct |
79 |
Correct |
32 ms |
35840 KB |
Output is correct |
80 |
Correct |
31 ms |
35932 KB |
Output is correct |
81 |
Correct |
73 ms |
44668 KB |
Output is correct |
82 |
Correct |
136 ms |
53848 KB |
Output is correct |
83 |
Correct |
120 ms |
54612 KB |
Output is correct |
84 |
Correct |
119 ms |
62412 KB |
Output is correct |
85 |
Correct |
110 ms |
65280 KB |
Output is correct |
86 |
Correct |
197 ms |
63060 KB |
Output is correct |
87 |
Correct |
160 ms |
66684 KB |
Output is correct |
88 |
Correct |
0 ms |
348 KB |
Output is correct |
89 |
Correct |
153 ms |
66520 KB |
Output is correct |
90 |
Correct |
132 ms |
67664 KB |
Output is correct |
91 |
Correct |
122 ms |
68948 KB |
Output is correct |