#include "fish.h"
#include <bits/stdc++.h>
#pragma GCC optimize("Ofast")
using namespace std;
#define ll long long
#define pb push_back
#define F first
#define S second
#define all(v) v.begin(),v.end()
#define sz(s) (int)s.size()
#define lb lower_bound
#define ub upper_bound
const int MAX=3e5+10;
vector<pair<ll,ll>> y[MAX];
vector<pair<int,int>> vec[MAX];
vector<ll> p[MAX];
vector<ll> dp[MAX][2];
vector<ll> can[MAX];
ll getR(int x,ll r){
if(r<0)return 0;
int R=ub(all(y[x]),make_pair(r+1,-1ll))-y[x].begin()-1;
if(R<0)return 0;
return p[x][R];
}
long long max_weights(int N, int M, vector<int> X, vector<int> Y,vector<int> W) {
for(int i=0;i<M;i++){
X[i]++;
Y[i]++;
y[X[i]].pb({Y[i],i});
vec[Y[i]].pb({X[i],W[i]});
}
for(int i=1;i<=N;i++){
if(y[i].empty())continue;
sort(all(y[i]));
p[i].pb(W[y[i][0].S]);
for(int j=1;j<sz(y[i]);j++){
p[i].pb(p[i].back()+W[y[i][j].S]);
}
}
can[0].pb(0);
for(int i=1;i<=N;i++){
can[i].pb(0);
if(i>0){
for(auto [x,b]:y[i-1])can[i].pb(x);
}
if(i+1<=N){
for(auto [x,b]:y[i+1])can[i].pb(x);
}
sort(all(can[i]));
can[i].erase(unique(all(can[i])),can[i].end());
}
for(int i=0;i<=N+1;i++){
dp[i][0].resize(sz(can[i]));
dp[i][1].resize(sz(can[i]));
for(int j=0;j<sz(can[i]);j++){
dp[i][0][j]=dp[i][1][j]=0;
}
}
for(int i=1;i<=N;i++){
{
int r=sz(can[i-1]);
ll mx=0;
for(int j=sz(can[i])-1;j>=0;j--){
while(r-1>=0&&can[i-1][r-1]>=can[i][j]){
mx=max(mx,max(dp[i-1][0][r-1],dp[i-1][1][r-1])+getR(i,can[i-1][r-1]));
r--;
}
dp[i][0][j]=mx-getR(i,can[i][j]);
}
}
{
int r=0;
ll mx=0;
for(int j=0;j<sz(can[i]);j++){
while(r<sz(can[i-1])&&can[i-1][r]<=can[i][j]){
mx=max(mx,dp[i-1][1][r]-getR(i-1,can[i-1][r]));
r++;
}
dp[i][1][j]=mx+getR(i-1,can[i][j]);
dp[i][1][j]=max(dp[i][1][j],dp[i-1][0][0]);
}
}
}
ll ans=0;
for(int j=0;j<sz(can[N]);j++)ans=max(ans,max(dp[N][0][j],dp[N][1][j]));
return ans;
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
67 ms |
60352 KB |
Output is correct |
2 |
Correct |
79 ms |
63168 KB |
Output is correct |
3 |
Correct |
33 ms |
52048 KB |
Output is correct |
4 |
Correct |
34 ms |
52060 KB |
Output is correct |
5 |
Correct |
189 ms |
88228 KB |
Output is correct |
6 |
Correct |
197 ms |
95608 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
19 ms |
42588 KB |
Output is correct |
2 |
Correct |
119 ms |
68768 KB |
Output is correct |
3 |
Correct |
137 ms |
74968 KB |
Output is correct |
4 |
Correct |
70 ms |
60740 KB |
Output is correct |
5 |
Correct |
80 ms |
63952 KB |
Output is correct |
6 |
Correct |
19 ms |
42584 KB |
Output is correct |
7 |
Correct |
19 ms |
42476 KB |
Output is correct |
8 |
Correct |
19 ms |
42588 KB |
Output is correct |
9 |
Correct |
19 ms |
42584 KB |
Output is correct |
10 |
Correct |
33 ms |
52060 KB |
Output is correct |
11 |
Correct |
34 ms |
52060 KB |
Output is correct |
12 |
Correct |
89 ms |
62660 KB |
Output is correct |
13 |
Correct |
87 ms |
66240 KB |
Output is correct |
14 |
Correct |
71 ms |
61756 KB |
Output is correct |
15 |
Correct |
81 ms |
62584 KB |
Output is correct |
16 |
Correct |
71 ms |
61804 KB |
Output is correct |
17 |
Correct |
81 ms |
63664 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
32 ms |
52048 KB |
Output is correct |
2 |
Correct |
32 ms |
52060 KB |
Output is correct |
3 |
Correct |
55 ms |
57548 KB |
Output is correct |
4 |
Correct |
49 ms |
56496 KB |
Output is correct |
5 |
Correct |
76 ms |
64452 KB |
Output is correct |
6 |
Correct |
72 ms |
64004 KB |
Output is correct |
7 |
Correct |
74 ms |
64456 KB |
Output is correct |
8 |
Correct |
75 ms |
64452 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
19 ms |
42584 KB |
Output is correct |
2 |
Correct |
19 ms |
42588 KB |
Output is correct |
3 |
Correct |
18 ms |
42708 KB |
Output is correct |
4 |
Correct |
19 ms |
42588 KB |
Output is correct |
5 |
Correct |
18 ms |
42620 KB |
Output is correct |
6 |
Correct |
19 ms |
42588 KB |
Output is correct |
7 |
Correct |
24 ms |
42584 KB |
Output is correct |
8 |
Correct |
21 ms |
42584 KB |
Output is correct |
9 |
Correct |
23 ms |
42588 KB |
Output is correct |
10 |
Correct |
19 ms |
42840 KB |
Output is correct |
11 |
Correct |
19 ms |
42588 KB |
Output is correct |
12 |
Correct |
19 ms |
42844 KB |
Output is correct |
13 |
Correct |
19 ms |
42584 KB |
Output is correct |
14 |
Correct |
19 ms |
42844 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
19 ms |
42584 KB |
Output is correct |
2 |
Correct |
19 ms |
42588 KB |
Output is correct |
3 |
Correct |
18 ms |
42708 KB |
Output is correct |
4 |
Correct |
19 ms |
42588 KB |
Output is correct |
5 |
Correct |
18 ms |
42620 KB |
Output is correct |
6 |
Correct |
19 ms |
42588 KB |
Output is correct |
7 |
Correct |
24 ms |
42584 KB |
Output is correct |
8 |
Correct |
21 ms |
42584 KB |
Output is correct |
9 |
Correct |
23 ms |
42588 KB |
Output is correct |
10 |
Correct |
19 ms |
42840 KB |
Output is correct |
11 |
Correct |
19 ms |
42588 KB |
Output is correct |
12 |
Correct |
19 ms |
42844 KB |
Output is correct |
13 |
Correct |
19 ms |
42584 KB |
Output is correct |
14 |
Correct |
19 ms |
42844 KB |
Output is correct |
15 |
Correct |
19 ms |
42588 KB |
Output is correct |
16 |
Correct |
19 ms |
42844 KB |
Output is correct |
17 |
Correct |
39 ms |
48096 KB |
Output is correct |
18 |
Correct |
38 ms |
49244 KB |
Output is correct |
19 |
Correct |
35 ms |
48988 KB |
Output is correct |
20 |
Correct |
35 ms |
48720 KB |
Output is correct |
21 |
Correct |
34 ms |
48732 KB |
Output is correct |
22 |
Correct |
51 ms |
54884 KB |
Output is correct |
23 |
Correct |
22 ms |
43952 KB |
Output is correct |
24 |
Correct |
33 ms |
46516 KB |
Output is correct |
25 |
Correct |
18 ms |
42844 KB |
Output is correct |
26 |
Correct |
22 ms |
43612 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
19 ms |
42584 KB |
Output is correct |
2 |
Correct |
19 ms |
42588 KB |
Output is correct |
3 |
Correct |
18 ms |
42708 KB |
Output is correct |
4 |
Correct |
19 ms |
42588 KB |
Output is correct |
5 |
Correct |
18 ms |
42620 KB |
Output is correct |
6 |
Correct |
19 ms |
42588 KB |
Output is correct |
7 |
Correct |
24 ms |
42584 KB |
Output is correct |
8 |
Correct |
21 ms |
42584 KB |
Output is correct |
9 |
Correct |
23 ms |
42588 KB |
Output is correct |
10 |
Correct |
19 ms |
42840 KB |
Output is correct |
11 |
Correct |
19 ms |
42588 KB |
Output is correct |
12 |
Correct |
19 ms |
42844 KB |
Output is correct |
13 |
Correct |
19 ms |
42584 KB |
Output is correct |
14 |
Correct |
19 ms |
42844 KB |
Output is correct |
15 |
Correct |
19 ms |
42588 KB |
Output is correct |
16 |
Correct |
19 ms |
42844 KB |
Output is correct |
17 |
Correct |
39 ms |
48096 KB |
Output is correct |
18 |
Correct |
38 ms |
49244 KB |
Output is correct |
19 |
Correct |
35 ms |
48988 KB |
Output is correct |
20 |
Correct |
35 ms |
48720 KB |
Output is correct |
21 |
Correct |
34 ms |
48732 KB |
Output is correct |
22 |
Correct |
51 ms |
54884 KB |
Output is correct |
23 |
Correct |
22 ms |
43952 KB |
Output is correct |
24 |
Correct |
33 ms |
46516 KB |
Output is correct |
25 |
Correct |
18 ms |
42844 KB |
Output is correct |
26 |
Correct |
22 ms |
43612 KB |
Output is correct |
27 |
Correct |
20 ms |
43356 KB |
Output is correct |
28 |
Correct |
124 ms |
71964 KB |
Output is correct |
29 |
Correct |
166 ms |
80724 KB |
Output is correct |
30 |
Correct |
162 ms |
83024 KB |
Output is correct |
31 |
Correct |
159 ms |
83024 KB |
Output is correct |
32 |
Correct |
128 ms |
82772 KB |
Output is correct |
33 |
Correct |
165 ms |
83324 KB |
Output is correct |
34 |
Correct |
181 ms |
83332 KB |
Output is correct |
35 |
Correct |
70 ms |
58708 KB |
Output is correct |
36 |
Correct |
189 ms |
85500 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
32 ms |
52048 KB |
Output is correct |
2 |
Correct |
32 ms |
52060 KB |
Output is correct |
3 |
Correct |
55 ms |
57548 KB |
Output is correct |
4 |
Correct |
49 ms |
56496 KB |
Output is correct |
5 |
Correct |
76 ms |
64452 KB |
Output is correct |
6 |
Correct |
72 ms |
64004 KB |
Output is correct |
7 |
Correct |
74 ms |
64456 KB |
Output is correct |
8 |
Correct |
75 ms |
64452 KB |
Output is correct |
9 |
Correct |
96 ms |
66640 KB |
Output is correct |
10 |
Correct |
65 ms |
58452 KB |
Output is correct |
11 |
Correct |
129 ms |
74556 KB |
Output is correct |
12 |
Correct |
23 ms |
42844 KB |
Output is correct |
13 |
Correct |
21 ms |
42564 KB |
Output is correct |
14 |
Correct |
19 ms |
42588 KB |
Output is correct |
15 |
Correct |
18 ms |
42588 KB |
Output is correct |
16 |
Correct |
19 ms |
42520 KB |
Output is correct |
17 |
Correct |
19 ms |
42584 KB |
Output is correct |
18 |
Correct |
33 ms |
51928 KB |
Output is correct |
19 |
Correct |
35 ms |
52048 KB |
Output is correct |
20 |
Correct |
32 ms |
52060 KB |
Output is correct |
21 |
Correct |
32 ms |
51872 KB |
Output is correct |
22 |
Correct |
109 ms |
65724 KB |
Output is correct |
23 |
Correct |
124 ms |
77148 KB |
Output is correct |
24 |
Correct |
143 ms |
79184 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
67 ms |
60352 KB |
Output is correct |
2 |
Correct |
79 ms |
63168 KB |
Output is correct |
3 |
Correct |
33 ms |
52048 KB |
Output is correct |
4 |
Correct |
34 ms |
52060 KB |
Output is correct |
5 |
Correct |
189 ms |
88228 KB |
Output is correct |
6 |
Correct |
197 ms |
95608 KB |
Output is correct |
7 |
Correct |
19 ms |
42588 KB |
Output is correct |
8 |
Correct |
119 ms |
68768 KB |
Output is correct |
9 |
Correct |
137 ms |
74968 KB |
Output is correct |
10 |
Correct |
70 ms |
60740 KB |
Output is correct |
11 |
Correct |
80 ms |
63952 KB |
Output is correct |
12 |
Correct |
19 ms |
42584 KB |
Output is correct |
13 |
Correct |
19 ms |
42476 KB |
Output is correct |
14 |
Correct |
19 ms |
42588 KB |
Output is correct |
15 |
Correct |
19 ms |
42584 KB |
Output is correct |
16 |
Correct |
33 ms |
52060 KB |
Output is correct |
17 |
Correct |
34 ms |
52060 KB |
Output is correct |
18 |
Correct |
89 ms |
62660 KB |
Output is correct |
19 |
Correct |
87 ms |
66240 KB |
Output is correct |
20 |
Correct |
71 ms |
61756 KB |
Output is correct |
21 |
Correct |
81 ms |
62584 KB |
Output is correct |
22 |
Correct |
71 ms |
61804 KB |
Output is correct |
23 |
Correct |
81 ms |
63664 KB |
Output is correct |
24 |
Correct |
32 ms |
52048 KB |
Output is correct |
25 |
Correct |
32 ms |
52060 KB |
Output is correct |
26 |
Correct |
55 ms |
57548 KB |
Output is correct |
27 |
Correct |
49 ms |
56496 KB |
Output is correct |
28 |
Correct |
76 ms |
64452 KB |
Output is correct |
29 |
Correct |
72 ms |
64004 KB |
Output is correct |
30 |
Correct |
74 ms |
64456 KB |
Output is correct |
31 |
Correct |
75 ms |
64452 KB |
Output is correct |
32 |
Correct |
19 ms |
42584 KB |
Output is correct |
33 |
Correct |
19 ms |
42588 KB |
Output is correct |
34 |
Correct |
18 ms |
42708 KB |
Output is correct |
35 |
Correct |
19 ms |
42588 KB |
Output is correct |
36 |
Correct |
18 ms |
42620 KB |
Output is correct |
37 |
Correct |
19 ms |
42588 KB |
Output is correct |
38 |
Correct |
24 ms |
42584 KB |
Output is correct |
39 |
Correct |
21 ms |
42584 KB |
Output is correct |
40 |
Correct |
23 ms |
42588 KB |
Output is correct |
41 |
Correct |
19 ms |
42840 KB |
Output is correct |
42 |
Correct |
19 ms |
42588 KB |
Output is correct |
43 |
Correct |
19 ms |
42844 KB |
Output is correct |
44 |
Correct |
19 ms |
42584 KB |
Output is correct |
45 |
Correct |
19 ms |
42844 KB |
Output is correct |
46 |
Correct |
19 ms |
42588 KB |
Output is correct |
47 |
Correct |
19 ms |
42844 KB |
Output is correct |
48 |
Correct |
39 ms |
48096 KB |
Output is correct |
49 |
Correct |
38 ms |
49244 KB |
Output is correct |
50 |
Correct |
35 ms |
48988 KB |
Output is correct |
51 |
Correct |
35 ms |
48720 KB |
Output is correct |
52 |
Correct |
34 ms |
48732 KB |
Output is correct |
53 |
Correct |
51 ms |
54884 KB |
Output is correct |
54 |
Correct |
22 ms |
43952 KB |
Output is correct |
55 |
Correct |
33 ms |
46516 KB |
Output is correct |
56 |
Correct |
18 ms |
42844 KB |
Output is correct |
57 |
Correct |
22 ms |
43612 KB |
Output is correct |
58 |
Correct |
20 ms |
43356 KB |
Output is correct |
59 |
Correct |
124 ms |
71964 KB |
Output is correct |
60 |
Correct |
166 ms |
80724 KB |
Output is correct |
61 |
Correct |
162 ms |
83024 KB |
Output is correct |
62 |
Correct |
159 ms |
83024 KB |
Output is correct |
63 |
Correct |
128 ms |
82772 KB |
Output is correct |
64 |
Correct |
165 ms |
83324 KB |
Output is correct |
65 |
Correct |
181 ms |
83332 KB |
Output is correct |
66 |
Correct |
70 ms |
58708 KB |
Output is correct |
67 |
Correct |
189 ms |
85500 KB |
Output is correct |
68 |
Correct |
96 ms |
66640 KB |
Output is correct |
69 |
Correct |
65 ms |
58452 KB |
Output is correct |
70 |
Correct |
129 ms |
74556 KB |
Output is correct |
71 |
Correct |
23 ms |
42844 KB |
Output is correct |
72 |
Correct |
21 ms |
42564 KB |
Output is correct |
73 |
Correct |
19 ms |
42588 KB |
Output is correct |
74 |
Correct |
18 ms |
42588 KB |
Output is correct |
75 |
Correct |
19 ms |
42520 KB |
Output is correct |
76 |
Correct |
19 ms |
42584 KB |
Output is correct |
77 |
Correct |
33 ms |
51928 KB |
Output is correct |
78 |
Correct |
35 ms |
52048 KB |
Output is correct |
79 |
Correct |
32 ms |
52060 KB |
Output is correct |
80 |
Correct |
32 ms |
51872 KB |
Output is correct |
81 |
Correct |
109 ms |
65724 KB |
Output is correct |
82 |
Correct |
124 ms |
77148 KB |
Output is correct |
83 |
Correct |
143 ms |
79184 KB |
Output is correct |
84 |
Correct |
234 ms |
89956 KB |
Output is correct |
85 |
Correct |
240 ms |
90016 KB |
Output is correct |
86 |
Correct |
196 ms |
93296 KB |
Output is correct |
87 |
Correct |
206 ms |
94544 KB |
Output is correct |
88 |
Correct |
19 ms |
42664 KB |
Output is correct |
89 |
Correct |
270 ms |
94584 KB |
Output is correct |
90 |
Correct |
180 ms |
90876 KB |
Output is correct |
91 |
Correct |
158 ms |
88804 KB |
Output is correct |