#include "fish.h"
#include<bits/stdc++.h>
using namespace std;
using lint=long long;
const int lim=1e5+100;
using pii=pair<int,lint>;
int n,m;
vector<int>inds[lim];
vector<lint> dp[lim],up[lim];
vector<pii>pref[lim];
lint p(int i,int r){
if(n<=i)return 0;
auto rp=lower_bound(pref[i].begin(),pref[i].end(),pii{r+1,0});
rp--;
return rp->second;
}
lint max_weights(int N,int M,vector<int>X,vector<int>Y,vector<int>W) {
n=N,m=M;
for(int i=0;i<m;i++){
pref[X[i]].push_back({Y[i]+1,W[i]});
}
for(int i=0;i<n;i++){
pref[i].push_back({0,0});
pref[i].push_back({n+6,0});
sort(pref[i].begin(),pref[i].end());
for(int j=0;j<size(pref[i]);j++){
if(pref[i][j].first)inds[i].push_back(pref[i][j].first-1);
else inds[i].push_back(0);
}
for(int j=1;j<size(pref[i]);j++){
pref[i][j].second+=pref[i][j-1].second;
}
}
dp[0].resize(inds[0].size());
up[0].resize(inds[0].size());
for(int i=0;i<size(dp[0]);i++){
dp[0][i]=p(1,inds[0][i]);
up[0][i]=0;
}
for(int i=1;i<n;i++){
dp[i].resize(inds[i].size());
up[i].resize(inds[i].size());
lint max1=0;
int pp=0;
for(int j=0;j<size(dp[i]);j++){
while(pp<size(up[i-1])&&inds[i-1][pp]<=inds[i][j]){
max1=max(max1,up[i-1][pp]-p(i-1,inds[i-1][pp]));
pp++;
}
dp[i][j]=max(dp[i][j],max1+p(i-1,inds[i][j])+p(i+1,inds[i][j]));
up[i][j]=max(up[i][j],max1+p(i-1,inds[i][j]));
auto p=upper_bound(inds[i-1].begin(),inds[i-1].end(),inds[i][j]);
if(p!=inds[i-1].begin()){
p--;
up[i][j]=max(up[i][j],dp[i-1][p-inds[i-1].begin()]);
}
}
max1=0;
pp=size(up[i-1]);
pp--;
for(int j=int(size(dp[i]))-1;0<=j;j--){
while(0<=pp&&inds[i][j]<=inds[i-1][pp]){
max1=max(max1,dp[i-1][pp]);
pp--;
}
dp[i][j]=max(dp[i][j],max1-p(i,inds[i][j])+p(i+1,inds[i][j]));
}
/*
cerr<<i<<" {\n";
for(int j=0;j<dp[i].size();j++){
cerr<<" "<<inds[i][j]<<" :: "<<dp[i][j]<<"\n";
}cerr<<"}{\n";
for(int j=0;j<dp[i].size();j++){
cerr<<" "<<inds[i][j]<<" :: "<<up[i][j]<<"\n";
}cerr<<"}\n";
*/
}
lint ans=0;
for(int i=0;i<size(dp[n-1]);i++){
ans=max(ans,dp[n-1][i]);
}
return ans;
}
Compilation message
fish.cpp: In function 'lint max_weights(int, int, std::vector<int>, std::vector<int>, std::vector<int>)':
fish.cpp:32:18: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<std::pair<int, long long int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
32 | for(int j=0;j<size(pref[i]);j++){
| ~^~~~~~~~~~~~~~
fish.cpp:36:18: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<std::pair<int, long long int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
36 | for(int j=1;j<size(pref[i]);j++){
| ~^~~~~~~~~~~~~~
fish.cpp:42:16: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<long long int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
42 | for(int i=0;i<size(dp[0]);i++){
| ~^~~~~~~~~~~~
fish.cpp:51:18: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<long long int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
51 | for(int j=0;j<size(dp[i]);j++){
| ~^~~~~~~~~~~~
fish.cpp:52:15: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<long long int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
52 | while(pp<size(up[i-1])&&inds[i-1][pp]<=inds[i][j]){
| ~~^~~~~~~~~~~~~~
fish.cpp:85:16: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<long long int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
85 | for(int i=0;i<size(dp[n-1]);i++){
| ~^~~~~~~~~~~~~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
42 ms |
27076 KB |
Output is correct |
2 |
Correct |
48 ms |
29712 KB |
Output is correct |
3 |
Correct |
22 ms |
23896 KB |
Output is correct |
4 |
Correct |
28 ms |
23892 KB |
Output is correct |
5 |
Correct |
117 ms |
41388 KB |
Output is correct |
6 |
Correct |
99 ms |
43604 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
9832 KB |
Output is correct |
2 |
Correct |
74 ms |
31864 KB |
Output is correct |
3 |
Correct |
91 ms |
35512 KB |
Output is correct |
4 |
Correct |
44 ms |
27016 KB |
Output is correct |
5 |
Correct |
49 ms |
29772 KB |
Output is correct |
6 |
Correct |
2 ms |
9820 KB |
Output is correct |
7 |
Correct |
1 ms |
9820 KB |
Output is correct |
8 |
Correct |
2 ms |
9816 KB |
Output is correct |
9 |
Correct |
1 ms |
9820 KB |
Output is correct |
10 |
Correct |
27 ms |
23784 KB |
Output is correct |
11 |
Correct |
22 ms |
23892 KB |
Output is correct |
12 |
Correct |
45 ms |
27112 KB |
Output is correct |
13 |
Correct |
55 ms |
29632 KB |
Output is correct |
14 |
Correct |
45 ms |
27108 KB |
Output is correct |
15 |
Correct |
53 ms |
29120 KB |
Output is correct |
16 |
Correct |
46 ms |
27072 KB |
Output is correct |
17 |
Correct |
59 ms |
29120 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
21 ms |
23896 KB |
Output is correct |
2 |
Correct |
22 ms |
23900 KB |
Output is correct |
3 |
Correct |
33 ms |
25288 KB |
Output is correct |
4 |
Correct |
30 ms |
25848 KB |
Output is correct |
5 |
Correct |
44 ms |
29268 KB |
Output is correct |
6 |
Correct |
42 ms |
29244 KB |
Output is correct |
7 |
Correct |
44 ms |
29248 KB |
Output is correct |
8 |
Correct |
47 ms |
29196 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
9820 KB |
Output is correct |
2 |
Correct |
1 ms |
9820 KB |
Output is correct |
3 |
Correct |
1 ms |
9820 KB |
Output is correct |
4 |
Correct |
2 ms |
9820 KB |
Output is correct |
5 |
Correct |
1 ms |
9820 KB |
Output is correct |
6 |
Correct |
1 ms |
9820 KB |
Output is correct |
7 |
Correct |
1 ms |
9820 KB |
Output is correct |
8 |
Correct |
2 ms |
9816 KB |
Output is correct |
9 |
Correct |
2 ms |
9816 KB |
Output is correct |
10 |
Correct |
2 ms |
9820 KB |
Output is correct |
11 |
Correct |
2 ms |
9820 KB |
Output is correct |
12 |
Correct |
2 ms |
9820 KB |
Output is correct |
13 |
Correct |
2 ms |
9648 KB |
Output is correct |
14 |
Correct |
2 ms |
9820 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
9820 KB |
Output is correct |
2 |
Correct |
1 ms |
9820 KB |
Output is correct |
3 |
Correct |
1 ms |
9820 KB |
Output is correct |
4 |
Correct |
2 ms |
9820 KB |
Output is correct |
5 |
Correct |
1 ms |
9820 KB |
Output is correct |
6 |
Correct |
1 ms |
9820 KB |
Output is correct |
7 |
Correct |
1 ms |
9820 KB |
Output is correct |
8 |
Correct |
2 ms |
9816 KB |
Output is correct |
9 |
Correct |
2 ms |
9816 KB |
Output is correct |
10 |
Correct |
2 ms |
9820 KB |
Output is correct |
11 |
Correct |
2 ms |
9820 KB |
Output is correct |
12 |
Correct |
2 ms |
9820 KB |
Output is correct |
13 |
Correct |
2 ms |
9648 KB |
Output is correct |
14 |
Correct |
2 ms |
9820 KB |
Output is correct |
15 |
Correct |
2 ms |
9820 KB |
Output is correct |
16 |
Correct |
3 ms |
9820 KB |
Output is correct |
17 |
Correct |
18 ms |
12888 KB |
Output is correct |
18 |
Correct |
18 ms |
13148 KB |
Output is correct |
19 |
Correct |
13 ms |
13148 KB |
Output is correct |
20 |
Correct |
12 ms |
13196 KB |
Output is correct |
21 |
Correct |
13 ms |
13168 KB |
Output is correct |
22 |
Correct |
25 ms |
16472 KB |
Output is correct |
23 |
Correct |
5 ms |
10332 KB |
Output is correct |
24 |
Correct |
14 ms |
11672 KB |
Output is correct |
25 |
Correct |
2 ms |
9820 KB |
Output is correct |
26 |
Correct |
4 ms |
10332 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
9820 KB |
Output is correct |
2 |
Correct |
1 ms |
9820 KB |
Output is correct |
3 |
Correct |
1 ms |
9820 KB |
Output is correct |
4 |
Correct |
2 ms |
9820 KB |
Output is correct |
5 |
Correct |
1 ms |
9820 KB |
Output is correct |
6 |
Correct |
1 ms |
9820 KB |
Output is correct |
7 |
Correct |
1 ms |
9820 KB |
Output is correct |
8 |
Correct |
2 ms |
9816 KB |
Output is correct |
9 |
Correct |
2 ms |
9816 KB |
Output is correct |
10 |
Correct |
2 ms |
9820 KB |
Output is correct |
11 |
Correct |
2 ms |
9820 KB |
Output is correct |
12 |
Correct |
2 ms |
9820 KB |
Output is correct |
13 |
Correct |
2 ms |
9648 KB |
Output is correct |
14 |
Correct |
2 ms |
9820 KB |
Output is correct |
15 |
Correct |
2 ms |
9820 KB |
Output is correct |
16 |
Correct |
3 ms |
9820 KB |
Output is correct |
17 |
Correct |
18 ms |
12888 KB |
Output is correct |
18 |
Correct |
18 ms |
13148 KB |
Output is correct |
19 |
Correct |
13 ms |
13148 KB |
Output is correct |
20 |
Correct |
12 ms |
13196 KB |
Output is correct |
21 |
Correct |
13 ms |
13168 KB |
Output is correct |
22 |
Correct |
25 ms |
16472 KB |
Output is correct |
23 |
Correct |
5 ms |
10332 KB |
Output is correct |
24 |
Correct |
14 ms |
11672 KB |
Output is correct |
25 |
Correct |
2 ms |
9820 KB |
Output is correct |
26 |
Correct |
4 ms |
10332 KB |
Output is correct |
27 |
Correct |
3 ms |
10332 KB |
Output is correct |
28 |
Correct |
90 ms |
24916 KB |
Output is correct |
29 |
Correct |
138 ms |
30136 KB |
Output is correct |
30 |
Correct |
118 ms |
29524 KB |
Output is correct |
31 |
Correct |
105 ms |
29384 KB |
Output is correct |
32 |
Correct |
115 ms |
31568 KB |
Output is correct |
33 |
Correct |
101 ms |
29456 KB |
Output is correct |
34 |
Correct |
101 ms |
29520 KB |
Output is correct |
35 |
Correct |
38 ms |
18004 KB |
Output is correct |
36 |
Correct |
120 ms |
31312 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
21 ms |
23896 KB |
Output is correct |
2 |
Correct |
22 ms |
23900 KB |
Output is correct |
3 |
Correct |
33 ms |
25288 KB |
Output is correct |
4 |
Correct |
30 ms |
25848 KB |
Output is correct |
5 |
Correct |
44 ms |
29268 KB |
Output is correct |
6 |
Correct |
42 ms |
29244 KB |
Output is correct |
7 |
Correct |
44 ms |
29248 KB |
Output is correct |
8 |
Correct |
47 ms |
29196 KB |
Output is correct |
9 |
Correct |
52 ms |
29272 KB |
Output is correct |
10 |
Correct |
38 ms |
22872 KB |
Output is correct |
11 |
Correct |
77 ms |
35920 KB |
Output is correct |
12 |
Correct |
2 ms |
9820 KB |
Output is correct |
13 |
Correct |
1 ms |
9820 KB |
Output is correct |
14 |
Correct |
2 ms |
9820 KB |
Output is correct |
15 |
Correct |
2 ms |
9820 KB |
Output is correct |
16 |
Correct |
1 ms |
9820 KB |
Output is correct |
17 |
Correct |
1 ms |
9644 KB |
Output is correct |
18 |
Correct |
22 ms |
23896 KB |
Output is correct |
19 |
Correct |
21 ms |
23896 KB |
Output is correct |
20 |
Correct |
22 ms |
23900 KB |
Output is correct |
21 |
Correct |
25 ms |
23896 KB |
Output is correct |
22 |
Correct |
48 ms |
29268 KB |
Output is correct |
23 |
Correct |
78 ms |
35912 KB |
Output is correct |
24 |
Correct |
78 ms |
35924 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
42 ms |
27076 KB |
Output is correct |
2 |
Correct |
48 ms |
29712 KB |
Output is correct |
3 |
Correct |
22 ms |
23896 KB |
Output is correct |
4 |
Correct |
28 ms |
23892 KB |
Output is correct |
5 |
Correct |
117 ms |
41388 KB |
Output is correct |
6 |
Correct |
99 ms |
43604 KB |
Output is correct |
7 |
Correct |
1 ms |
9832 KB |
Output is correct |
8 |
Correct |
74 ms |
31864 KB |
Output is correct |
9 |
Correct |
91 ms |
35512 KB |
Output is correct |
10 |
Correct |
44 ms |
27016 KB |
Output is correct |
11 |
Correct |
49 ms |
29772 KB |
Output is correct |
12 |
Correct |
2 ms |
9820 KB |
Output is correct |
13 |
Correct |
1 ms |
9820 KB |
Output is correct |
14 |
Correct |
2 ms |
9816 KB |
Output is correct |
15 |
Correct |
1 ms |
9820 KB |
Output is correct |
16 |
Correct |
27 ms |
23784 KB |
Output is correct |
17 |
Correct |
22 ms |
23892 KB |
Output is correct |
18 |
Correct |
45 ms |
27112 KB |
Output is correct |
19 |
Correct |
55 ms |
29632 KB |
Output is correct |
20 |
Correct |
45 ms |
27108 KB |
Output is correct |
21 |
Correct |
53 ms |
29120 KB |
Output is correct |
22 |
Correct |
46 ms |
27072 KB |
Output is correct |
23 |
Correct |
59 ms |
29120 KB |
Output is correct |
24 |
Correct |
21 ms |
23896 KB |
Output is correct |
25 |
Correct |
22 ms |
23900 KB |
Output is correct |
26 |
Correct |
33 ms |
25288 KB |
Output is correct |
27 |
Correct |
30 ms |
25848 KB |
Output is correct |
28 |
Correct |
44 ms |
29268 KB |
Output is correct |
29 |
Correct |
42 ms |
29244 KB |
Output is correct |
30 |
Correct |
44 ms |
29248 KB |
Output is correct |
31 |
Correct |
47 ms |
29196 KB |
Output is correct |
32 |
Correct |
2 ms |
9820 KB |
Output is correct |
33 |
Correct |
1 ms |
9820 KB |
Output is correct |
34 |
Correct |
1 ms |
9820 KB |
Output is correct |
35 |
Correct |
2 ms |
9820 KB |
Output is correct |
36 |
Correct |
1 ms |
9820 KB |
Output is correct |
37 |
Correct |
1 ms |
9820 KB |
Output is correct |
38 |
Correct |
1 ms |
9820 KB |
Output is correct |
39 |
Correct |
2 ms |
9816 KB |
Output is correct |
40 |
Correct |
2 ms |
9816 KB |
Output is correct |
41 |
Correct |
2 ms |
9820 KB |
Output is correct |
42 |
Correct |
2 ms |
9820 KB |
Output is correct |
43 |
Correct |
2 ms |
9820 KB |
Output is correct |
44 |
Correct |
2 ms |
9648 KB |
Output is correct |
45 |
Correct |
2 ms |
9820 KB |
Output is correct |
46 |
Correct |
2 ms |
9820 KB |
Output is correct |
47 |
Correct |
3 ms |
9820 KB |
Output is correct |
48 |
Correct |
18 ms |
12888 KB |
Output is correct |
49 |
Correct |
18 ms |
13148 KB |
Output is correct |
50 |
Correct |
13 ms |
13148 KB |
Output is correct |
51 |
Correct |
12 ms |
13196 KB |
Output is correct |
52 |
Correct |
13 ms |
13168 KB |
Output is correct |
53 |
Correct |
25 ms |
16472 KB |
Output is correct |
54 |
Correct |
5 ms |
10332 KB |
Output is correct |
55 |
Correct |
14 ms |
11672 KB |
Output is correct |
56 |
Correct |
2 ms |
9820 KB |
Output is correct |
57 |
Correct |
4 ms |
10332 KB |
Output is correct |
58 |
Correct |
3 ms |
10332 KB |
Output is correct |
59 |
Correct |
90 ms |
24916 KB |
Output is correct |
60 |
Correct |
138 ms |
30136 KB |
Output is correct |
61 |
Correct |
118 ms |
29524 KB |
Output is correct |
62 |
Correct |
105 ms |
29384 KB |
Output is correct |
63 |
Correct |
115 ms |
31568 KB |
Output is correct |
64 |
Correct |
101 ms |
29456 KB |
Output is correct |
65 |
Correct |
101 ms |
29520 KB |
Output is correct |
66 |
Correct |
38 ms |
18004 KB |
Output is correct |
67 |
Correct |
120 ms |
31312 KB |
Output is correct |
68 |
Correct |
52 ms |
29272 KB |
Output is correct |
69 |
Correct |
38 ms |
22872 KB |
Output is correct |
70 |
Correct |
77 ms |
35920 KB |
Output is correct |
71 |
Correct |
2 ms |
9820 KB |
Output is correct |
72 |
Correct |
1 ms |
9820 KB |
Output is correct |
73 |
Correct |
2 ms |
9820 KB |
Output is correct |
74 |
Correct |
2 ms |
9820 KB |
Output is correct |
75 |
Correct |
1 ms |
9820 KB |
Output is correct |
76 |
Correct |
1 ms |
9644 KB |
Output is correct |
77 |
Correct |
22 ms |
23896 KB |
Output is correct |
78 |
Correct |
21 ms |
23896 KB |
Output is correct |
79 |
Correct |
22 ms |
23900 KB |
Output is correct |
80 |
Correct |
25 ms |
23896 KB |
Output is correct |
81 |
Correct |
48 ms |
29268 KB |
Output is correct |
82 |
Correct |
78 ms |
35912 KB |
Output is correct |
83 |
Correct |
78 ms |
35924 KB |
Output is correct |
84 |
Correct |
143 ms |
40644 KB |
Output is correct |
85 |
Correct |
143 ms |
41732 KB |
Output is correct |
86 |
Correct |
107 ms |
41272 KB |
Output is correct |
87 |
Correct |
108 ms |
42804 KB |
Output is correct |
88 |
Correct |
2 ms |
9820 KB |
Output is correct |
89 |
Correct |
112 ms |
42764 KB |
Output is correct |
90 |
Correct |
119 ms |
43352 KB |
Output is correct |
91 |
Correct |
106 ms |
44080 KB |
Output is correct |