#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define vll vector<ll>
#define all(x) begin(x),end(x)
const int N=1e5+10;
vll pre[N];
ll sz[N];
vll dp[N][3];
vll possible[N];
vector<pair<ll,ll>> values[N];
ll solve(ll&n,ll&m,vll&x,vll&y,vll&w)
{
ll ans=0;
for(int j=0;j<m;j++)
{
values[x[j]].push_back({y[j]+1,w[j]});
possible[x[j]].push_back(y[j]+1);
possible[x[j]+1].push_back(y[j]+1);
if(x[j]>0)
possible[x[j]-1].push_back(y[j]+1);
// cout<<"AT "<<x[j]<<' '<<y[j]+1<<endl;
}
for(int i=0;i<=n;i++)
{
possible[i].push_back(0);
possible[i].push_back(n);
sort(begin(possible[i]),end(possible[i]));
sort(begin(values[i]),end(values[i]));
sz[i]=possible[i].size();
pre[i].resize(sz[i]+1,0);
ll sm=0;
int pp=0;
int rp=0;
for(auto&j:possible[i])
{
while(pp<values[i].size() and values[i][pp].first<=j)
{
sm+=values[i][pp].second;
pp++;
}
pre[i][rp]=sm;
rp++;
}
dp[i][0].resize(sz[i]+1,0);
dp[i][1].resize(sz[i]+1,0);
}
for(int i=1;i<n;i++)
{
// ll sum=0;
ll mx1=-1e18;
ll mx2=-1e18;
ll mx11=0,mx22=0;
for(int k=0;k<sz[i-1];k++)
{
int hp=possible[i-1][k];
mx11=max(mx11,dp[i-1][0][k]);
mx1=max(mx1,dp[i-1][0][k]-pre[i-1][k]);
}
for(int k=0;k<sz[i-1];k++)
{
int hp=possible[i-1][k];
mx22=max(mx22,dp[i-1][1][k]);
auto index=lower_bound(all(possible[i]),hp);
if(index!=end(possible[i]) and (*index)==hp)
{
int ind=index-begin(possible[i]);
mx2=max(mx2,dp[i-1][1][k]+pre[i][ind]);
}
else
{
mx2=max(mx2,dp[i-1][1][k]);
}
}
// cout<<"For "<<i<<' '<<mx1<<' '<<mx2<<' '<<mx11<<' '<<mx22<<endl;
int pp=0;
for(auto&h:possible[i])
{
auto index=lower_bound(all(possible[i-1]),h);
// if(possible[i-1][index]!=h)
// {
// cout<<"Fudge\n";
// cout<<i-1<<' '<<index<<' '<<h<<endl;
// for(auto jp:possible[i-1])
// {
// cout<<jp<<' ';
// }
// cout<<endl;
// }
if(index!=end(possible[i-1]) and (*index)==h)
{
int ind=index-begin(possible[i-1]);
dp[i][0][pp]=max(dp[i][0][pp],mx1+pre[i-1][ind]);
dp[i][1][pp]=max(dp[i][1][pp],mx1+pre[i-1][ind]);
}
else
{
dp[i][0][pp]=max(dp[i][0][pp],mx1);
dp[i][1][pp]=max(dp[i][1][pp],mx1);
}
dp[i][1][pp]=max(dp[i][1][pp],mx2-pre[i][pp]);
dp[i][0][pp]=max(dp[i][0][pp],mx11);
dp[i][0][pp]=max(dp[i][0][pp],mx22);
dp[i][1][pp]=max(dp[i][1][pp],mx11);
dp[i][1][pp]=max(dp[i][1][pp],mx22);
pp++;
}
}
// cout<<"Then\n";
for(int i=0;i<n;i++)
{
for(int j=0;j<sz[i];j++)
{
ans=max(ans,dp[i][0][j]);
ans=max(ans,dp[i][1][j]);
// cout<<i<<' '<<possible[i][j]<<' '<<0<<' '<<dp[i][0][j]<<endl;
// cout<<i<<' '<<possible[i][j]<<' '<<1<<' '<<dp[i][1][j]<<endl;
}
}
return ans;
}
// long long max_weights(int N, int M, std::vector<ll> X, std::vector<ll> Y,std::vector<ll> W)
long long max_weights(int N, int M, std::vector<int> X, std::vector<int> Y,std::vector<int> W)
{
ll n=N,m=M,ans=0;
vector<ll> x(all(X)),y(all(Y)),w(all(W));
return solve(n,m,x,y,w);
}
Compilation message
fish.cpp: In function 'long long int solve(long long int&, long long int&, std::vector<long long int>&, std::vector<long long int>&, std::vector<long long int>&)':
fish.cpp:37:21: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<std::pair<long long int, long long int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
37 | while(pp<values[i].size() and values[i][pp].first<=j)
| ~~^~~~~~~~~~~~~~~~~
fish.cpp:56:17: warning: unused variable 'hp' [-Wunused-variable]
56 | int hp=possible[i-1][k];
| ^~
fish.cpp: In function 'long long int max_weights(int, int, std::vector<int>, std::vector<int>, std::vector<int>)':
fish.cpp:127:16: warning: unused variable 'ans' [-Wunused-variable]
127 | ll n=N,m=M,ans=0;
| ^~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
76 ms |
37860 KB |
Output is correct |
2 |
Correct |
93 ms |
42040 KB |
Output is correct |
3 |
Correct |
40 ms |
27728 KB |
Output is correct |
4 |
Correct |
31 ms |
27740 KB |
Output is correct |
5 |
Correct |
250 ms |
82728 KB |
Output is correct |
6 |
Correct |
245 ms |
88404 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
6 ms |
14424 KB |
Output is correct |
2 |
Correct |
133 ms |
52088 KB |
Output is correct |
3 |
Correct |
162 ms |
59352 KB |
Output is correct |
4 |
Correct |
70 ms |
37828 KB |
Output is correct |
5 |
Correct |
82 ms |
41916 KB |
Output is correct |
6 |
Correct |
7 ms |
14428 KB |
Output is correct |
7 |
Correct |
6 ms |
14768 KB |
Output is correct |
8 |
Correct |
6 ms |
14428 KB |
Output is correct |
9 |
Correct |
7 ms |
14424 KB |
Output is correct |
10 |
Correct |
30 ms |
27604 KB |
Output is correct |
11 |
Correct |
28 ms |
27728 KB |
Output is correct |
12 |
Correct |
83 ms |
40900 KB |
Output is correct |
13 |
Correct |
100 ms |
45436 KB |
Output is correct |
14 |
Correct |
82 ms |
39148 KB |
Output is correct |
15 |
Correct |
85 ms |
42068 KB |
Output is correct |
16 |
Correct |
81 ms |
39144 KB |
Output is correct |
17 |
Correct |
106 ms |
41908 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
28 ms |
27740 KB |
Output is correct |
2 |
Correct |
32 ms |
27736 KB |
Output is correct |
3 |
Correct |
58 ms |
38736 KB |
Output is correct |
4 |
Correct |
50 ms |
36460 KB |
Output is correct |
5 |
Correct |
111 ms |
52468 KB |
Output is correct |
6 |
Correct |
84 ms |
52048 KB |
Output is correct |
7 |
Correct |
90 ms |
52560 KB |
Output is correct |
8 |
Correct |
100 ms |
52568 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
6 ms |
14424 KB |
Output is correct |
2 |
Correct |
7 ms |
14680 KB |
Output is correct |
3 |
Correct |
7 ms |
14428 KB |
Output is correct |
4 |
Correct |
6 ms |
14428 KB |
Output is correct |
5 |
Correct |
7 ms |
14428 KB |
Output is correct |
6 |
Correct |
6 ms |
14428 KB |
Output is correct |
7 |
Correct |
7 ms |
14536 KB |
Output is correct |
8 |
Correct |
6 ms |
14428 KB |
Output is correct |
9 |
Correct |
7 ms |
14500 KB |
Output is correct |
10 |
Correct |
7 ms |
14936 KB |
Output is correct |
11 |
Correct |
6 ms |
14684 KB |
Output is correct |
12 |
Correct |
9 ms |
14812 KB |
Output is correct |
13 |
Correct |
6 ms |
14428 KB |
Output is correct |
14 |
Correct |
8 ms |
14684 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
6 ms |
14424 KB |
Output is correct |
2 |
Correct |
7 ms |
14680 KB |
Output is correct |
3 |
Correct |
7 ms |
14428 KB |
Output is correct |
4 |
Correct |
6 ms |
14428 KB |
Output is correct |
5 |
Correct |
7 ms |
14428 KB |
Output is correct |
6 |
Correct |
6 ms |
14428 KB |
Output is correct |
7 |
Correct |
7 ms |
14536 KB |
Output is correct |
8 |
Correct |
6 ms |
14428 KB |
Output is correct |
9 |
Correct |
7 ms |
14500 KB |
Output is correct |
10 |
Correct |
7 ms |
14936 KB |
Output is correct |
11 |
Correct |
6 ms |
14684 KB |
Output is correct |
12 |
Correct |
9 ms |
14812 KB |
Output is correct |
13 |
Correct |
6 ms |
14428 KB |
Output is correct |
14 |
Correct |
8 ms |
14684 KB |
Output is correct |
15 |
Correct |
7 ms |
14428 KB |
Output is correct |
16 |
Correct |
8 ms |
14932 KB |
Output is correct |
17 |
Correct |
36 ms |
22872 KB |
Output is correct |
18 |
Correct |
36 ms |
23324 KB |
Output is correct |
19 |
Correct |
33 ms |
23120 KB |
Output is correct |
20 |
Correct |
29 ms |
23132 KB |
Output is correct |
21 |
Correct |
35 ms |
23124 KB |
Output is correct |
22 |
Correct |
50 ms |
31792 KB |
Output is correct |
23 |
Correct |
12 ms |
16220 KB |
Output is correct |
24 |
Correct |
31 ms |
20560 KB |
Output is correct |
25 |
Correct |
7 ms |
14684 KB |
Output is correct |
26 |
Correct |
12 ms |
16244 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
6 ms |
14424 KB |
Output is correct |
2 |
Correct |
7 ms |
14680 KB |
Output is correct |
3 |
Correct |
7 ms |
14428 KB |
Output is correct |
4 |
Correct |
6 ms |
14428 KB |
Output is correct |
5 |
Correct |
7 ms |
14428 KB |
Output is correct |
6 |
Correct |
6 ms |
14428 KB |
Output is correct |
7 |
Correct |
7 ms |
14536 KB |
Output is correct |
8 |
Correct |
6 ms |
14428 KB |
Output is correct |
9 |
Correct |
7 ms |
14500 KB |
Output is correct |
10 |
Correct |
7 ms |
14936 KB |
Output is correct |
11 |
Correct |
6 ms |
14684 KB |
Output is correct |
12 |
Correct |
9 ms |
14812 KB |
Output is correct |
13 |
Correct |
6 ms |
14428 KB |
Output is correct |
14 |
Correct |
8 ms |
14684 KB |
Output is correct |
15 |
Correct |
7 ms |
14428 KB |
Output is correct |
16 |
Correct |
8 ms |
14932 KB |
Output is correct |
17 |
Correct |
36 ms |
22872 KB |
Output is correct |
18 |
Correct |
36 ms |
23324 KB |
Output is correct |
19 |
Correct |
33 ms |
23120 KB |
Output is correct |
20 |
Correct |
29 ms |
23132 KB |
Output is correct |
21 |
Correct |
35 ms |
23124 KB |
Output is correct |
22 |
Correct |
50 ms |
31792 KB |
Output is correct |
23 |
Correct |
12 ms |
16220 KB |
Output is correct |
24 |
Correct |
31 ms |
20560 KB |
Output is correct |
25 |
Correct |
7 ms |
14684 KB |
Output is correct |
26 |
Correct |
12 ms |
16244 KB |
Output is correct |
27 |
Correct |
9 ms |
15452 KB |
Output is correct |
28 |
Correct |
149 ms |
55380 KB |
Output is correct |
29 |
Correct |
253 ms |
71156 KB |
Output is correct |
30 |
Correct |
202 ms |
75244 KB |
Output is correct |
31 |
Correct |
194 ms |
75004 KB |
Output is correct |
32 |
Correct |
160 ms |
72788 KB |
Output is correct |
33 |
Correct |
193 ms |
75004 KB |
Output is correct |
34 |
Correct |
200 ms |
75088 KB |
Output is correct |
35 |
Correct |
73 ms |
37580 KB |
Output is correct |
36 |
Correct |
229 ms |
73552 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
28 ms |
27740 KB |
Output is correct |
2 |
Correct |
32 ms |
27736 KB |
Output is correct |
3 |
Correct |
58 ms |
38736 KB |
Output is correct |
4 |
Correct |
50 ms |
36460 KB |
Output is correct |
5 |
Correct |
111 ms |
52468 KB |
Output is correct |
6 |
Correct |
84 ms |
52048 KB |
Output is correct |
7 |
Correct |
90 ms |
52560 KB |
Output is correct |
8 |
Correct |
100 ms |
52568 KB |
Output is correct |
9 |
Correct |
87 ms |
52304 KB |
Output is correct |
10 |
Correct |
85 ms |
39760 KB |
Output is correct |
11 |
Correct |
140 ms |
64852 KB |
Output is correct |
12 |
Correct |
7 ms |
14424 KB |
Output is correct |
13 |
Correct |
6 ms |
14428 KB |
Output is correct |
14 |
Correct |
6 ms |
14428 KB |
Output is correct |
15 |
Correct |
6 ms |
14428 KB |
Output is correct |
16 |
Correct |
6 ms |
14428 KB |
Output is correct |
17 |
Correct |
7 ms |
14428 KB |
Output is correct |
18 |
Correct |
30 ms |
27704 KB |
Output is correct |
19 |
Correct |
30 ms |
27740 KB |
Output is correct |
20 |
Correct |
30 ms |
27740 KB |
Output is correct |
21 |
Correct |
28 ms |
27740 KB |
Output is correct |
22 |
Correct |
109 ms |
49380 KB |
Output is correct |
23 |
Correct |
156 ms |
64848 KB |
Output is correct |
24 |
Correct |
154 ms |
65384 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
76 ms |
37860 KB |
Output is correct |
2 |
Correct |
93 ms |
42040 KB |
Output is correct |
3 |
Correct |
40 ms |
27728 KB |
Output is correct |
4 |
Correct |
31 ms |
27740 KB |
Output is correct |
5 |
Correct |
250 ms |
82728 KB |
Output is correct |
6 |
Correct |
245 ms |
88404 KB |
Output is correct |
7 |
Correct |
6 ms |
14424 KB |
Output is correct |
8 |
Correct |
133 ms |
52088 KB |
Output is correct |
9 |
Correct |
162 ms |
59352 KB |
Output is correct |
10 |
Correct |
70 ms |
37828 KB |
Output is correct |
11 |
Correct |
82 ms |
41916 KB |
Output is correct |
12 |
Correct |
7 ms |
14428 KB |
Output is correct |
13 |
Correct |
6 ms |
14768 KB |
Output is correct |
14 |
Correct |
6 ms |
14428 KB |
Output is correct |
15 |
Correct |
7 ms |
14424 KB |
Output is correct |
16 |
Correct |
30 ms |
27604 KB |
Output is correct |
17 |
Correct |
28 ms |
27728 KB |
Output is correct |
18 |
Correct |
83 ms |
40900 KB |
Output is correct |
19 |
Correct |
100 ms |
45436 KB |
Output is correct |
20 |
Correct |
82 ms |
39148 KB |
Output is correct |
21 |
Correct |
85 ms |
42068 KB |
Output is correct |
22 |
Correct |
81 ms |
39144 KB |
Output is correct |
23 |
Correct |
106 ms |
41908 KB |
Output is correct |
24 |
Correct |
28 ms |
27740 KB |
Output is correct |
25 |
Correct |
32 ms |
27736 KB |
Output is correct |
26 |
Correct |
58 ms |
38736 KB |
Output is correct |
27 |
Correct |
50 ms |
36460 KB |
Output is correct |
28 |
Correct |
111 ms |
52468 KB |
Output is correct |
29 |
Correct |
84 ms |
52048 KB |
Output is correct |
30 |
Correct |
90 ms |
52560 KB |
Output is correct |
31 |
Correct |
100 ms |
52568 KB |
Output is correct |
32 |
Correct |
6 ms |
14424 KB |
Output is correct |
33 |
Correct |
7 ms |
14680 KB |
Output is correct |
34 |
Correct |
7 ms |
14428 KB |
Output is correct |
35 |
Correct |
6 ms |
14428 KB |
Output is correct |
36 |
Correct |
7 ms |
14428 KB |
Output is correct |
37 |
Correct |
6 ms |
14428 KB |
Output is correct |
38 |
Correct |
7 ms |
14536 KB |
Output is correct |
39 |
Correct |
6 ms |
14428 KB |
Output is correct |
40 |
Correct |
7 ms |
14500 KB |
Output is correct |
41 |
Correct |
7 ms |
14936 KB |
Output is correct |
42 |
Correct |
6 ms |
14684 KB |
Output is correct |
43 |
Correct |
9 ms |
14812 KB |
Output is correct |
44 |
Correct |
6 ms |
14428 KB |
Output is correct |
45 |
Correct |
8 ms |
14684 KB |
Output is correct |
46 |
Correct |
7 ms |
14428 KB |
Output is correct |
47 |
Correct |
8 ms |
14932 KB |
Output is correct |
48 |
Correct |
36 ms |
22872 KB |
Output is correct |
49 |
Correct |
36 ms |
23324 KB |
Output is correct |
50 |
Correct |
33 ms |
23120 KB |
Output is correct |
51 |
Correct |
29 ms |
23132 KB |
Output is correct |
52 |
Correct |
35 ms |
23124 KB |
Output is correct |
53 |
Correct |
50 ms |
31792 KB |
Output is correct |
54 |
Correct |
12 ms |
16220 KB |
Output is correct |
55 |
Correct |
31 ms |
20560 KB |
Output is correct |
56 |
Correct |
7 ms |
14684 KB |
Output is correct |
57 |
Correct |
12 ms |
16244 KB |
Output is correct |
58 |
Correct |
9 ms |
15452 KB |
Output is correct |
59 |
Correct |
149 ms |
55380 KB |
Output is correct |
60 |
Correct |
253 ms |
71156 KB |
Output is correct |
61 |
Correct |
202 ms |
75244 KB |
Output is correct |
62 |
Correct |
194 ms |
75004 KB |
Output is correct |
63 |
Correct |
160 ms |
72788 KB |
Output is correct |
64 |
Correct |
193 ms |
75004 KB |
Output is correct |
65 |
Correct |
200 ms |
75088 KB |
Output is correct |
66 |
Correct |
73 ms |
37580 KB |
Output is correct |
67 |
Correct |
229 ms |
73552 KB |
Output is correct |
68 |
Correct |
87 ms |
52304 KB |
Output is correct |
69 |
Correct |
85 ms |
39760 KB |
Output is correct |
70 |
Correct |
140 ms |
64852 KB |
Output is correct |
71 |
Correct |
7 ms |
14424 KB |
Output is correct |
72 |
Correct |
6 ms |
14428 KB |
Output is correct |
73 |
Correct |
6 ms |
14428 KB |
Output is correct |
74 |
Correct |
6 ms |
14428 KB |
Output is correct |
75 |
Correct |
6 ms |
14428 KB |
Output is correct |
76 |
Correct |
7 ms |
14428 KB |
Output is correct |
77 |
Correct |
30 ms |
27704 KB |
Output is correct |
78 |
Correct |
30 ms |
27740 KB |
Output is correct |
79 |
Correct |
30 ms |
27740 KB |
Output is correct |
80 |
Correct |
28 ms |
27740 KB |
Output is correct |
81 |
Correct |
109 ms |
49380 KB |
Output is correct |
82 |
Correct |
156 ms |
64848 KB |
Output is correct |
83 |
Correct |
154 ms |
65384 KB |
Output is correct |
84 |
Correct |
249 ms |
80280 KB |
Output is correct |
85 |
Correct |
264 ms |
80704 KB |
Output is correct |
86 |
Correct |
248 ms |
86376 KB |
Output is correct |
87 |
Correct |
272 ms |
88656 KB |
Output is correct |
88 |
Correct |
7 ms |
14424 KB |
Output is correct |
89 |
Correct |
272 ms |
88660 KB |
Output is correct |
90 |
Correct |
227 ms |
86268 KB |
Output is correct |
91 |
Correct |
210 ms |
85480 KB |
Output is correct |