#include "fish.h"
#include<bits/stdc++.h>
using namespace std;
#define pb push_back
#define ll long long
#define pii pair<int, int>
#define ff first
#define ss second
const int MAXN = 500100;
int n, w[MAXN], m;
pii a[MAXN];
namespace B{
unordered_map<ll, ll> d[200100];
unordered_map<ll, ll> dp[200100];
unordered_map<ll, ll> pref[200100];
unordered_map<ll, ll> ddp[200100];
vector<ll> pos[200100];
ll suf[200100];
ll solve(){
for(ll i=1; i<=m; i++){
d[a[i].ff][a[i].ss] += w[i];
}
for(int i=0; i<=n; i++){
d[i][0] += 0;
d[i][n] += 0;
}
for(int i=0; i<=n; i++){
for(auto h: d[i]){
pos[i].pb(h.ff);
}
}
for(ll i=1; i<=n; i++){
sort(pos[i].begin(), pos[i].end());
}
for(ll i=1; i<=n; i++){
dp[0][i] = -1e18;
ddp[0][i] = -1e18;
}
dp[0][0] = 0;
ddp[0][0] = 0;
for(ll i=1; i<=n; i++){
//cout<<"_____________________\n";
//cout<<i<<'\n';
vector<ll> alpos;
for(auto h: pos[i-1]) alpos.pb(h);
for(auto h: pos[i+1]) alpos.pb(h);
sort(alpos.begin(), alpos.end());
int gls = 0;
for(ll h: pos[i]){
ll j = h;
pref[i][j] = pref[i][gls] + d[i][j];
gls = j;
}
vector<ll> dpos;
for(auto h: dp[i-1]){
dpos.pb(h.ff);
}
sort(dpos.begin(), dpos.end());
ll mx = -1e18;
ll ls = pos[i].size()-1;
for(ll h = dpos.size()-1; h>=0; h--){
ll j = dpos[h];
while(ls > 0 and pos[i][ls] > j) ls--;
mx = max(mx, dp[i-1][j] + pref[i][j]);
suf[j] = mx;
}
ll pddp = -1e18, pdp = -1e18;
mx = -1e18;
for(ll h = dpos.size()-1; h>=0; h--){
ll j = dpos[h];
mx = max(mx, dp[i-1][j]);
}
ll pls=-1, dpls=-1;
ls = -1;
ll lls = -1;
for(ll j: alpos){
//cout<<j<<' ';
while(ls + 1 < dpos.size() and dpos[ls + 1] <= j){
ls += 1;
while(lls + 1 < pos[i-1].size() and pos[i-1][lls + 1] <= dpos[ls]) lls++;
pdp = max(pdp, ddp[i-1][dpos[ls]]-pref[i-1][pos[i-1][lls]]);
}
while(pls + 1 < pos[i].size() and pos[i][pls + 1] <= j) pls ++;
while(dpls + 1 < pos[i-1].size() and pos[i-1][dpls + 1] <= j) dpls ++;
ddp[i][j] = mx;
dp[i][j] = 0;
//if(dpls < 0){
// cout<<"asdas\n";
// cout<<j<<'\n';
// cout<<dpls+1<<'\n';
// continue;
//}
ddp[i][j] = max(ddp[i][j], pdp + pref[i-1][pos[i-1][dpls]]);
dp[i][j] = ddp[i][j];
if(dpos.size() > ls + 1) dp[i][j] = max(dp[i][j], suf[dpos[ls + 1]]-pref[i][pos[i][pls]]);
else dp[i][j] = max(dp[i][j], suf[dpos[ls]]-pref[i][pos[i][pls]]);
dp[i][j] = max(dp[i][j], pdp+pref[i-1][pos[i-1][dpls]]);
}
//cout<<'\n';
}
//for(ll i=1; i<=n; i++){
// for(ll j=0; j<=n; j++){
// cout<<pref[i][j]<<' ';
// }
// cout<<'\n';
//}
ll ans = 0;
for(ll i=0; i<=n; i++) ans = max(ans, dp[n][i]);
return ans;
}
}
long long max_weights(int N, int M, vector<int> X, vector<int> Y,
vector<int> W) {
n = N;
m = M;
for(int i=1; i<=m; i++){
a[i] = {X[i-1]+1, Y[i-1]+1};
}
for(int i=1; i<=m; i++){
w[i] = W[i-1];
}
return B::solve();
}
Compilation message
fish.cpp: In function 'long long int B::solve()':
fish.cpp:80:30: warning: comparison of integer expressions of different signedness: 'long long int' and 'std::vector<long long int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
80 | while(ls + 1 < dpos.size() and dpos[ls + 1] <= j){
| ~~~~~~~^~~~~~~~~~~~~
fish.cpp:82:35: warning: comparison of integer expressions of different signedness: 'long long int' and 'std::vector<long long int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
82 | while(lls + 1 < pos[i-1].size() and pos[i-1][lls + 1] <= dpos[ls]) lls++;
| ~~~~~~~~^~~~~~~~~~~~~~~~~
fish.cpp:85:31: warning: comparison of integer expressions of different signedness: 'long long int' and 'std::vector<long long int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
85 | while(pls + 1 < pos[i].size() and pos[i][pls + 1] <= j) pls ++;
| ~~~~~~~~^~~~~~~~~~~~~~~
fish.cpp:86:32: warning: comparison of integer expressions of different signedness: 'long long int' and 'std::vector<long long int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
86 | while(dpls + 1 < pos[i-1].size() and pos[i-1][dpls + 1] <= j) dpls ++;
| ~~~~~~~~~^~~~~~~~~~~~~~~~~
fish.cpp:97:32: warning: comparison of integer expressions of different signedness: 'std::vector<long long int>::size_type' {aka 'long unsigned int'} and 'long long int' [-Wsign-compare]
97 | if(dpos.size() > ls + 1) dp[i][j] = max(dp[i][j], suf[dpos[ls + 1]]-pref[i][pos[i][pls]]);
| ~~~~~~~~~~~~^~~~~~~~
fish.cpp:69:16: warning: unused variable 'pddp' [-Wunused-variable]
69 | ll pddp = -1e18, pdp = -1e18;
| ^~~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
225 ms |
148580 KB |
Output is correct |
2 |
Correct |
305 ms |
162572 KB |
Output is correct |
3 |
Correct |
166 ms |
141088 KB |
Output is correct |
4 |
Correct |
166 ms |
141092 KB |
Output is correct |
5 |
Correct |
673 ms |
249188 KB |
Output is correct |
6 |
Correct |
666 ms |
229928 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
17 ms |
50268 KB |
Output is correct |
2 |
Correct |
306 ms |
173944 KB |
Output is correct |
3 |
Correct |
321 ms |
197916 KB |
Output is correct |
4 |
Correct |
287 ms |
148580 KB |
Output is correct |
5 |
Correct |
241 ms |
162860 KB |
Output is correct |
6 |
Correct |
19 ms |
50348 KB |
Output is correct |
7 |
Correct |
25 ms |
50268 KB |
Output is correct |
8 |
Correct |
18 ms |
50264 KB |
Output is correct |
9 |
Correct |
18 ms |
50356 KB |
Output is correct |
10 |
Correct |
169 ms |
141084 KB |
Output is correct |
11 |
Correct |
236 ms |
141096 KB |
Output is correct |
12 |
Correct |
334 ms |
158368 KB |
Output is correct |
13 |
Correct |
345 ms |
176176 KB |
Output is correct |
14 |
Correct |
265 ms |
153472 KB |
Output is correct |
15 |
Correct |
291 ms |
167192 KB |
Output is correct |
16 |
Correct |
260 ms |
153524 KB |
Output is correct |
17 |
Correct |
286 ms |
167208 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
178 ms |
141108 KB |
Output is correct |
2 |
Correct |
239 ms |
141104 KB |
Output is correct |
3 |
Correct |
252 ms |
142456 KB |
Output is correct |
4 |
Correct |
228 ms |
147756 KB |
Output is correct |
5 |
Correct |
270 ms |
157996 KB |
Output is correct |
6 |
Correct |
356 ms |
157924 KB |
Output is correct |
7 |
Correct |
270 ms |
158248 KB |
Output is correct |
8 |
Correct |
315 ms |
157984 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
18 ms |
50268 KB |
Output is correct |
2 |
Correct |
20 ms |
50128 KB |
Output is correct |
3 |
Correct |
19 ms |
50340 KB |
Output is correct |
4 |
Correct |
19 ms |
50268 KB |
Output is correct |
5 |
Correct |
25 ms |
50368 KB |
Output is correct |
6 |
Correct |
20 ms |
50340 KB |
Output is correct |
7 |
Correct |
19 ms |
50268 KB |
Output is correct |
8 |
Correct |
19 ms |
50368 KB |
Output is correct |
9 |
Correct |
22 ms |
50520 KB |
Output is correct |
10 |
Correct |
21 ms |
50780 KB |
Output is correct |
11 |
Correct |
19 ms |
50500 KB |
Output is correct |
12 |
Correct |
23 ms |
50780 KB |
Output is correct |
13 |
Correct |
20 ms |
50392 KB |
Output is correct |
14 |
Correct |
20 ms |
50620 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
18 ms |
50268 KB |
Output is correct |
2 |
Correct |
20 ms |
50128 KB |
Output is correct |
3 |
Correct |
19 ms |
50340 KB |
Output is correct |
4 |
Correct |
19 ms |
50268 KB |
Output is correct |
5 |
Correct |
25 ms |
50368 KB |
Output is correct |
6 |
Correct |
20 ms |
50340 KB |
Output is correct |
7 |
Correct |
19 ms |
50268 KB |
Output is correct |
8 |
Correct |
19 ms |
50368 KB |
Output is correct |
9 |
Correct |
22 ms |
50520 KB |
Output is correct |
10 |
Correct |
21 ms |
50780 KB |
Output is correct |
11 |
Correct |
19 ms |
50500 KB |
Output is correct |
12 |
Correct |
23 ms |
50780 KB |
Output is correct |
13 |
Correct |
20 ms |
50392 KB |
Output is correct |
14 |
Correct |
20 ms |
50620 KB |
Output is correct |
15 |
Correct |
17 ms |
50524 KB |
Output is correct |
16 |
Correct |
20 ms |
50920 KB |
Output is correct |
17 |
Correct |
55 ms |
61780 KB |
Output is correct |
18 |
Correct |
60 ms |
61268 KB |
Output is correct |
19 |
Correct |
59 ms |
61520 KB |
Output is correct |
20 |
Correct |
54 ms |
60496 KB |
Output is correct |
21 |
Correct |
55 ms |
60356 KB |
Output is correct |
22 |
Correct |
99 ms |
70484 KB |
Output is correct |
23 |
Correct |
31 ms |
53336 KB |
Output is correct |
24 |
Correct |
52 ms |
59104 KB |
Output is correct |
25 |
Correct |
20 ms |
50776 KB |
Output is correct |
26 |
Correct |
30 ms |
52816 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
18 ms |
50268 KB |
Output is correct |
2 |
Correct |
20 ms |
50128 KB |
Output is correct |
3 |
Correct |
19 ms |
50340 KB |
Output is correct |
4 |
Correct |
19 ms |
50268 KB |
Output is correct |
5 |
Correct |
25 ms |
50368 KB |
Output is correct |
6 |
Correct |
20 ms |
50340 KB |
Output is correct |
7 |
Correct |
19 ms |
50268 KB |
Output is correct |
8 |
Correct |
19 ms |
50368 KB |
Output is correct |
9 |
Correct |
22 ms |
50520 KB |
Output is correct |
10 |
Correct |
21 ms |
50780 KB |
Output is correct |
11 |
Correct |
19 ms |
50500 KB |
Output is correct |
12 |
Correct |
23 ms |
50780 KB |
Output is correct |
13 |
Correct |
20 ms |
50392 KB |
Output is correct |
14 |
Correct |
20 ms |
50620 KB |
Output is correct |
15 |
Correct |
17 ms |
50524 KB |
Output is correct |
16 |
Correct |
20 ms |
50920 KB |
Output is correct |
17 |
Correct |
55 ms |
61780 KB |
Output is correct |
18 |
Correct |
60 ms |
61268 KB |
Output is correct |
19 |
Correct |
59 ms |
61520 KB |
Output is correct |
20 |
Correct |
54 ms |
60496 KB |
Output is correct |
21 |
Correct |
55 ms |
60356 KB |
Output is correct |
22 |
Correct |
99 ms |
70484 KB |
Output is correct |
23 |
Correct |
31 ms |
53336 KB |
Output is correct |
24 |
Correct |
52 ms |
59104 KB |
Output is correct |
25 |
Correct |
20 ms |
50776 KB |
Output is correct |
26 |
Correct |
30 ms |
52816 KB |
Output is correct |
27 |
Correct |
28 ms |
53864 KB |
Output is correct |
28 |
Correct |
263 ms |
103820 KB |
Output is correct |
29 |
Correct |
374 ms |
143188 KB |
Output is correct |
30 |
Correct |
562 ms |
149328 KB |
Output is correct |
31 |
Correct |
562 ms |
149104 KB |
Output is correct |
32 |
Correct |
320 ms |
120660 KB |
Output is correct |
33 |
Correct |
541 ms |
149864 KB |
Output is correct |
34 |
Correct |
518 ms |
149840 KB |
Output is correct |
35 |
Correct |
192 ms |
87636 KB |
Output is correct |
36 |
Correct |
478 ms |
144208 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
178 ms |
141108 KB |
Output is correct |
2 |
Correct |
239 ms |
141104 KB |
Output is correct |
3 |
Correct |
252 ms |
142456 KB |
Output is correct |
4 |
Correct |
228 ms |
147756 KB |
Output is correct |
5 |
Correct |
270 ms |
157996 KB |
Output is correct |
6 |
Correct |
356 ms |
157924 KB |
Output is correct |
7 |
Correct |
270 ms |
158248 KB |
Output is correct |
8 |
Correct |
315 ms |
157984 KB |
Output is correct |
9 |
Correct |
312 ms |
167464 KB |
Output is correct |
10 |
Correct |
186 ms |
111848 KB |
Output is correct |
11 |
Correct |
386 ms |
173744 KB |
Output is correct |
12 |
Correct |
21 ms |
50264 KB |
Output is correct |
13 |
Correct |
20 ms |
50268 KB |
Output is correct |
14 |
Correct |
18 ms |
50268 KB |
Output is correct |
15 |
Correct |
18 ms |
50268 KB |
Output is correct |
16 |
Correct |
19 ms |
50264 KB |
Output is correct |
17 |
Correct |
19 ms |
50364 KB |
Output is correct |
18 |
Correct |
180 ms |
141096 KB |
Output is correct |
19 |
Correct |
178 ms |
141092 KB |
Output is correct |
20 |
Correct |
189 ms |
141124 KB |
Output is correct |
21 |
Correct |
243 ms |
141092 KB |
Output is correct |
22 |
Correct |
409 ms |
166928 KB |
Output is correct |
23 |
Correct |
447 ms |
186928 KB |
Output is correct |
24 |
Correct |
431 ms |
189984 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
225 ms |
148580 KB |
Output is correct |
2 |
Correct |
305 ms |
162572 KB |
Output is correct |
3 |
Correct |
166 ms |
141088 KB |
Output is correct |
4 |
Correct |
166 ms |
141092 KB |
Output is correct |
5 |
Correct |
673 ms |
249188 KB |
Output is correct |
6 |
Correct |
666 ms |
229928 KB |
Output is correct |
7 |
Correct |
17 ms |
50268 KB |
Output is correct |
8 |
Correct |
306 ms |
173944 KB |
Output is correct |
9 |
Correct |
321 ms |
197916 KB |
Output is correct |
10 |
Correct |
287 ms |
148580 KB |
Output is correct |
11 |
Correct |
241 ms |
162860 KB |
Output is correct |
12 |
Correct |
19 ms |
50348 KB |
Output is correct |
13 |
Correct |
25 ms |
50268 KB |
Output is correct |
14 |
Correct |
18 ms |
50264 KB |
Output is correct |
15 |
Correct |
18 ms |
50356 KB |
Output is correct |
16 |
Correct |
169 ms |
141084 KB |
Output is correct |
17 |
Correct |
236 ms |
141096 KB |
Output is correct |
18 |
Correct |
334 ms |
158368 KB |
Output is correct |
19 |
Correct |
345 ms |
176176 KB |
Output is correct |
20 |
Correct |
265 ms |
153472 KB |
Output is correct |
21 |
Correct |
291 ms |
167192 KB |
Output is correct |
22 |
Correct |
260 ms |
153524 KB |
Output is correct |
23 |
Correct |
286 ms |
167208 KB |
Output is correct |
24 |
Correct |
178 ms |
141108 KB |
Output is correct |
25 |
Correct |
239 ms |
141104 KB |
Output is correct |
26 |
Correct |
252 ms |
142456 KB |
Output is correct |
27 |
Correct |
228 ms |
147756 KB |
Output is correct |
28 |
Correct |
270 ms |
157996 KB |
Output is correct |
29 |
Correct |
356 ms |
157924 KB |
Output is correct |
30 |
Correct |
270 ms |
158248 KB |
Output is correct |
31 |
Correct |
315 ms |
157984 KB |
Output is correct |
32 |
Correct |
18 ms |
50268 KB |
Output is correct |
33 |
Correct |
20 ms |
50128 KB |
Output is correct |
34 |
Correct |
19 ms |
50340 KB |
Output is correct |
35 |
Correct |
19 ms |
50268 KB |
Output is correct |
36 |
Correct |
25 ms |
50368 KB |
Output is correct |
37 |
Correct |
20 ms |
50340 KB |
Output is correct |
38 |
Correct |
19 ms |
50268 KB |
Output is correct |
39 |
Correct |
19 ms |
50368 KB |
Output is correct |
40 |
Correct |
22 ms |
50520 KB |
Output is correct |
41 |
Correct |
21 ms |
50780 KB |
Output is correct |
42 |
Correct |
19 ms |
50500 KB |
Output is correct |
43 |
Correct |
23 ms |
50780 KB |
Output is correct |
44 |
Correct |
20 ms |
50392 KB |
Output is correct |
45 |
Correct |
20 ms |
50620 KB |
Output is correct |
46 |
Correct |
17 ms |
50524 KB |
Output is correct |
47 |
Correct |
20 ms |
50920 KB |
Output is correct |
48 |
Correct |
55 ms |
61780 KB |
Output is correct |
49 |
Correct |
60 ms |
61268 KB |
Output is correct |
50 |
Correct |
59 ms |
61520 KB |
Output is correct |
51 |
Correct |
54 ms |
60496 KB |
Output is correct |
52 |
Correct |
55 ms |
60356 KB |
Output is correct |
53 |
Correct |
99 ms |
70484 KB |
Output is correct |
54 |
Correct |
31 ms |
53336 KB |
Output is correct |
55 |
Correct |
52 ms |
59104 KB |
Output is correct |
56 |
Correct |
20 ms |
50776 KB |
Output is correct |
57 |
Correct |
30 ms |
52816 KB |
Output is correct |
58 |
Correct |
28 ms |
53864 KB |
Output is correct |
59 |
Correct |
263 ms |
103820 KB |
Output is correct |
60 |
Correct |
374 ms |
143188 KB |
Output is correct |
61 |
Correct |
562 ms |
149328 KB |
Output is correct |
62 |
Correct |
562 ms |
149104 KB |
Output is correct |
63 |
Correct |
320 ms |
120660 KB |
Output is correct |
64 |
Correct |
541 ms |
149864 KB |
Output is correct |
65 |
Correct |
518 ms |
149840 KB |
Output is correct |
66 |
Correct |
192 ms |
87636 KB |
Output is correct |
67 |
Correct |
478 ms |
144208 KB |
Output is correct |
68 |
Correct |
312 ms |
167464 KB |
Output is correct |
69 |
Correct |
186 ms |
111848 KB |
Output is correct |
70 |
Correct |
386 ms |
173744 KB |
Output is correct |
71 |
Correct |
21 ms |
50264 KB |
Output is correct |
72 |
Correct |
20 ms |
50268 KB |
Output is correct |
73 |
Correct |
18 ms |
50268 KB |
Output is correct |
74 |
Correct |
18 ms |
50268 KB |
Output is correct |
75 |
Correct |
19 ms |
50264 KB |
Output is correct |
76 |
Correct |
19 ms |
50364 KB |
Output is correct |
77 |
Correct |
180 ms |
141096 KB |
Output is correct |
78 |
Correct |
178 ms |
141092 KB |
Output is correct |
79 |
Correct |
189 ms |
141124 KB |
Output is correct |
80 |
Correct |
243 ms |
141092 KB |
Output is correct |
81 |
Correct |
409 ms |
166928 KB |
Output is correct |
82 |
Correct |
447 ms |
186928 KB |
Output is correct |
83 |
Correct |
431 ms |
189984 KB |
Output is correct |
84 |
Correct |
708 ms |
226848 KB |
Output is correct |
85 |
Correct |
721 ms |
232412 KB |
Output is correct |
86 |
Correct |
647 ms |
211316 KB |
Output is correct |
87 |
Correct |
674 ms |
220464 KB |
Output is correct |
88 |
Correct |
20 ms |
50356 KB |
Output is correct |
89 |
Correct |
714 ms |
220460 KB |
Output is correct |
90 |
Correct |
586 ms |
220412 KB |
Output is correct |
91 |
Correct |
545 ms |
207144 KB |
Output is correct |