#include <bits/stdc++.h>
#define pb push_back
#define ll long long
using namespace std;
const int N = 1e5 + 5;
const long long LL = 1e18;
ll ans;
map<ll, ll> dp[N][2];
vector<pair<ll, ll>> pref[N];
vector<int> wys[N];
bool ok[N];
ll max_weights(int n, int m, vector<int>X, vector<int>Y, vector<int>Z){
cin>>n>>m;
for(int i=0;i<m;i++){
pref[X[i]].pb({Y[i], Z[i]});
if(Y[i]==0) ok[X[i]]=true;
wys[X[i]].pb(Y[i]);
}
for(int i = 0; i < n; ++i) {
wys[i].pb(n);
if(!ok[i]) wys[i].pb(0);
sort(wys[i].begin(), wys[i].end());
}
for(int i=0;i<n;i++){
sort(pref[i].begin(), pref[i].end());
ll suma=0;
for(int j=0;j<(int)pref[i].size();j++) {
suma+=pref[i][j].second;
pref[i][j].second = suma;
}
pref[i].insert(pref[i].begin(), make_pair(-1, 0));
}
for(int i=1;i<n;i++){
vector<pair<ll, ll>> spr1, spr2;
for(ll pom : wys[i-1]){
dp[i][0][0]=max({dp[i][0][0], dp[i-1][0][pom], dp[i-1][1][pom]});
dp[i][1][0]=max({dp[i][1][0], dp[i-1][0][pom], dp[i-1][1][pom]});
ll res=0;
if(!spr1.empty()) res=spr1.back().second;
auto it = upper_bound(pref[i-1].begin(), pref[i-1].end(), make_pair(pom-1, LL));
it--;
res=max(res, dp[i-1][0][pom]-it->second);
spr1.pb({pom, res});
}
reverse(wys[i-1].begin(), wys[i-1].end());
for(ll pom : wys[i-1]){
ll res=0;
if(!spr2.empty()) res=spr2.back().second;
auto it = upper_bound(pref[i].begin(), pref[i].end(), make_pair(pom-1, LL));
it--;
res = max(res, dp[i - 1][0][pom] + it->second);
res = max(res, dp[i - 1][1][pom] + it->second);
spr2.pb({pom, res});
}
reverse(spr2.begin(), spr2.end());
for(ll pom : wys[i]){
auto op1 = upper_bound(spr1.begin(), spr1.end(), make_pair(pom, LL));
op1--;
auto op2 = lower_bound(spr2.begin(), spr2.end(), make_pair(pom, -LL));
auto tmp1 = upper_bound(pref[i-1].begin(), pref[i-1].end(), make_pair(pom-1, LL));
tmp1--;
dp[i][0][pom]=max(dp[i][0][pom], op1->second + tmp1->second);
auto tmp2 = upper_bound(pref[i].begin(), pref[i].end(), make_pair(pom-1, LL));
tmp2--;
dp[i][1][pom]=max(dp[i][1][pom], op2->second - tmp2->second);
}
}
for(int i=0;i<=n;i++){
ans = max({ans, dp[n-1][0][i], dp[n-1][1][i]});
}
return ans;
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
220 ms |
67412 KB |
Output is correct |
2 |
Correct |
267 ms |
74992 KB |
Output is correct |
3 |
Correct |
114 ms |
58376 KB |
Output is correct |
4 |
Correct |
111 ms |
58208 KB |
Output is correct |
5 |
Correct |
738 ms |
108940 KB |
Output is correct |
6 |
Correct |
415 ms |
110920 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
7 ms |
14292 KB |
Output is correct |
2 |
Correct |
387 ms |
81400 KB |
Output is correct |
3 |
Correct |
472 ms |
91896 KB |
Output is correct |
4 |
Correct |
219 ms |
67484 KB |
Output is correct |
5 |
Correct |
272 ms |
75020 KB |
Output is correct |
6 |
Correct |
7 ms |
14320 KB |
Output is correct |
7 |
Correct |
7 ms |
14292 KB |
Output is correct |
8 |
Correct |
7 ms |
14284 KB |
Output is correct |
9 |
Correct |
7 ms |
14292 KB |
Output is correct |
10 |
Correct |
122 ms |
58284 KB |
Output is correct |
11 |
Correct |
120 ms |
58136 KB |
Output is correct |
12 |
Correct |
264 ms |
67600 KB |
Output is correct |
13 |
Correct |
306 ms |
75064 KB |
Output is correct |
14 |
Correct |
232 ms |
67580 KB |
Output is correct |
15 |
Correct |
277 ms |
73496 KB |
Output is correct |
16 |
Correct |
247 ms |
67552 KB |
Output is correct |
17 |
Correct |
268 ms |
73308 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
116 ms |
58272 KB |
Output is correct |
2 |
Correct |
119 ms |
58220 KB |
Output is correct |
3 |
Correct |
136 ms |
56532 KB |
Output is correct |
4 |
Correct |
141 ms |
59672 KB |
Output is correct |
5 |
Correct |
187 ms |
65256 KB |
Output is correct |
6 |
Correct |
183 ms |
65276 KB |
Output is correct |
7 |
Correct |
189 ms |
65256 KB |
Output is correct |
8 |
Correct |
184 ms |
65240 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
7 ms |
14292 KB |
Output is correct |
2 |
Correct |
7 ms |
14292 KB |
Output is correct |
3 |
Correct |
7 ms |
14292 KB |
Output is correct |
4 |
Correct |
7 ms |
14292 KB |
Output is correct |
5 |
Correct |
7 ms |
14292 KB |
Output is correct |
6 |
Correct |
7 ms |
14292 KB |
Output is correct |
7 |
Correct |
7 ms |
14396 KB |
Output is correct |
8 |
Correct |
7 ms |
14292 KB |
Output is correct |
9 |
Correct |
8 ms |
14548 KB |
Output is correct |
10 |
Correct |
9 ms |
14872 KB |
Output is correct |
11 |
Correct |
9 ms |
14548 KB |
Output is correct |
12 |
Correct |
8 ms |
14676 KB |
Output is correct |
13 |
Correct |
7 ms |
14420 KB |
Output is correct |
14 |
Correct |
8 ms |
14648 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
7 ms |
14292 KB |
Output is correct |
2 |
Correct |
7 ms |
14292 KB |
Output is correct |
3 |
Correct |
7 ms |
14292 KB |
Output is correct |
4 |
Correct |
7 ms |
14292 KB |
Output is correct |
5 |
Correct |
7 ms |
14292 KB |
Output is correct |
6 |
Correct |
7 ms |
14292 KB |
Output is correct |
7 |
Correct |
7 ms |
14396 KB |
Output is correct |
8 |
Correct |
7 ms |
14292 KB |
Output is correct |
9 |
Correct |
8 ms |
14548 KB |
Output is correct |
10 |
Correct |
9 ms |
14872 KB |
Output is correct |
11 |
Correct |
9 ms |
14548 KB |
Output is correct |
12 |
Correct |
8 ms |
14676 KB |
Output is correct |
13 |
Correct |
7 ms |
14420 KB |
Output is correct |
14 |
Correct |
8 ms |
14648 KB |
Output is correct |
15 |
Correct |
8 ms |
14548 KB |
Output is correct |
16 |
Correct |
12 ms |
14776 KB |
Output is correct |
17 |
Correct |
58 ms |
22132 KB |
Output is correct |
18 |
Correct |
58 ms |
22708 KB |
Output is correct |
19 |
Correct |
52 ms |
22652 KB |
Output is correct |
20 |
Correct |
54 ms |
22636 KB |
Output is correct |
21 |
Correct |
52 ms |
22548 KB |
Output is correct |
22 |
Correct |
104 ms |
30824 KB |
Output is correct |
23 |
Correct |
15 ms |
16084 KB |
Output is correct |
24 |
Correct |
41 ms |
19640 KB |
Output is correct |
25 |
Correct |
8 ms |
14612 KB |
Output is correct |
26 |
Correct |
15 ms |
15956 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
7 ms |
14292 KB |
Output is correct |
2 |
Correct |
7 ms |
14292 KB |
Output is correct |
3 |
Correct |
7 ms |
14292 KB |
Output is correct |
4 |
Correct |
7 ms |
14292 KB |
Output is correct |
5 |
Correct |
7 ms |
14292 KB |
Output is correct |
6 |
Correct |
7 ms |
14292 KB |
Output is correct |
7 |
Correct |
7 ms |
14396 KB |
Output is correct |
8 |
Correct |
7 ms |
14292 KB |
Output is correct |
9 |
Correct |
8 ms |
14548 KB |
Output is correct |
10 |
Correct |
9 ms |
14872 KB |
Output is correct |
11 |
Correct |
9 ms |
14548 KB |
Output is correct |
12 |
Correct |
8 ms |
14676 KB |
Output is correct |
13 |
Correct |
7 ms |
14420 KB |
Output is correct |
14 |
Correct |
8 ms |
14648 KB |
Output is correct |
15 |
Correct |
8 ms |
14548 KB |
Output is correct |
16 |
Correct |
12 ms |
14776 KB |
Output is correct |
17 |
Correct |
58 ms |
22132 KB |
Output is correct |
18 |
Correct |
58 ms |
22708 KB |
Output is correct |
19 |
Correct |
52 ms |
22652 KB |
Output is correct |
20 |
Correct |
54 ms |
22636 KB |
Output is correct |
21 |
Correct |
52 ms |
22548 KB |
Output is correct |
22 |
Correct |
104 ms |
30824 KB |
Output is correct |
23 |
Correct |
15 ms |
16084 KB |
Output is correct |
24 |
Correct |
41 ms |
19640 KB |
Output is correct |
25 |
Correct |
8 ms |
14612 KB |
Output is correct |
26 |
Correct |
15 ms |
15956 KB |
Output is correct |
27 |
Correct |
12 ms |
16192 KB |
Output is correct |
28 |
Correct |
269 ms |
53168 KB |
Output is correct |
29 |
Correct |
424 ms |
68032 KB |
Output is correct |
30 |
Correct |
316 ms |
67788 KB |
Output is correct |
31 |
Correct |
322 ms |
67868 KB |
Output is correct |
32 |
Correct |
350 ms |
68556 KB |
Output is correct |
33 |
Correct |
328 ms |
67812 KB |
Output is correct |
34 |
Correct |
319 ms |
67828 KB |
Output is correct |
35 |
Correct |
119 ms |
36156 KB |
Output is correct |
36 |
Correct |
361 ms |
69652 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
116 ms |
58272 KB |
Output is correct |
2 |
Correct |
119 ms |
58220 KB |
Output is correct |
3 |
Correct |
136 ms |
56532 KB |
Output is correct |
4 |
Correct |
141 ms |
59672 KB |
Output is correct |
5 |
Correct |
187 ms |
65256 KB |
Output is correct |
6 |
Correct |
183 ms |
65276 KB |
Output is correct |
7 |
Correct |
189 ms |
65256 KB |
Output is correct |
8 |
Correct |
184 ms |
65240 KB |
Output is correct |
9 |
Correct |
226 ms |
77788 KB |
Output is correct |
10 |
Correct |
128 ms |
48216 KB |
Output is correct |
11 |
Correct |
282 ms |
82020 KB |
Output is correct |
12 |
Correct |
7 ms |
14292 KB |
Output is correct |
13 |
Correct |
8 ms |
14292 KB |
Output is correct |
14 |
Correct |
7 ms |
14292 KB |
Output is correct |
15 |
Correct |
7 ms |
14292 KB |
Output is correct |
16 |
Correct |
7 ms |
14336 KB |
Output is correct |
17 |
Correct |
7 ms |
14352 KB |
Output is correct |
18 |
Correct |
123 ms |
58152 KB |
Output is correct |
19 |
Correct |
119 ms |
58160 KB |
Output is correct |
20 |
Correct |
119 ms |
58116 KB |
Output is correct |
21 |
Correct |
118 ms |
58196 KB |
Output is correct |
22 |
Correct |
224 ms |
75124 KB |
Output is correct |
23 |
Correct |
305 ms |
93808 KB |
Output is correct |
24 |
Correct |
302 ms |
94472 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
220 ms |
67412 KB |
Output is correct |
2 |
Correct |
267 ms |
74992 KB |
Output is correct |
3 |
Correct |
114 ms |
58376 KB |
Output is correct |
4 |
Correct |
111 ms |
58208 KB |
Output is correct |
5 |
Correct |
738 ms |
108940 KB |
Output is correct |
6 |
Correct |
415 ms |
110920 KB |
Output is correct |
7 |
Correct |
7 ms |
14292 KB |
Output is correct |
8 |
Correct |
387 ms |
81400 KB |
Output is correct |
9 |
Correct |
472 ms |
91896 KB |
Output is correct |
10 |
Correct |
219 ms |
67484 KB |
Output is correct |
11 |
Correct |
272 ms |
75020 KB |
Output is correct |
12 |
Correct |
7 ms |
14320 KB |
Output is correct |
13 |
Correct |
7 ms |
14292 KB |
Output is correct |
14 |
Correct |
7 ms |
14284 KB |
Output is correct |
15 |
Correct |
7 ms |
14292 KB |
Output is correct |
16 |
Correct |
122 ms |
58284 KB |
Output is correct |
17 |
Correct |
120 ms |
58136 KB |
Output is correct |
18 |
Correct |
264 ms |
67600 KB |
Output is correct |
19 |
Correct |
306 ms |
75064 KB |
Output is correct |
20 |
Correct |
232 ms |
67580 KB |
Output is correct |
21 |
Correct |
277 ms |
73496 KB |
Output is correct |
22 |
Correct |
247 ms |
67552 KB |
Output is correct |
23 |
Correct |
268 ms |
73308 KB |
Output is correct |
24 |
Correct |
116 ms |
58272 KB |
Output is correct |
25 |
Correct |
119 ms |
58220 KB |
Output is correct |
26 |
Correct |
136 ms |
56532 KB |
Output is correct |
27 |
Correct |
141 ms |
59672 KB |
Output is correct |
28 |
Correct |
187 ms |
65256 KB |
Output is correct |
29 |
Correct |
183 ms |
65276 KB |
Output is correct |
30 |
Correct |
189 ms |
65256 KB |
Output is correct |
31 |
Correct |
184 ms |
65240 KB |
Output is correct |
32 |
Correct |
7 ms |
14292 KB |
Output is correct |
33 |
Correct |
7 ms |
14292 KB |
Output is correct |
34 |
Correct |
7 ms |
14292 KB |
Output is correct |
35 |
Correct |
7 ms |
14292 KB |
Output is correct |
36 |
Correct |
7 ms |
14292 KB |
Output is correct |
37 |
Correct |
7 ms |
14292 KB |
Output is correct |
38 |
Correct |
7 ms |
14396 KB |
Output is correct |
39 |
Correct |
7 ms |
14292 KB |
Output is correct |
40 |
Correct |
8 ms |
14548 KB |
Output is correct |
41 |
Correct |
9 ms |
14872 KB |
Output is correct |
42 |
Correct |
9 ms |
14548 KB |
Output is correct |
43 |
Correct |
8 ms |
14676 KB |
Output is correct |
44 |
Correct |
7 ms |
14420 KB |
Output is correct |
45 |
Correct |
8 ms |
14648 KB |
Output is correct |
46 |
Correct |
8 ms |
14548 KB |
Output is correct |
47 |
Correct |
12 ms |
14776 KB |
Output is correct |
48 |
Correct |
58 ms |
22132 KB |
Output is correct |
49 |
Correct |
58 ms |
22708 KB |
Output is correct |
50 |
Correct |
52 ms |
22652 KB |
Output is correct |
51 |
Correct |
54 ms |
22636 KB |
Output is correct |
52 |
Correct |
52 ms |
22548 KB |
Output is correct |
53 |
Correct |
104 ms |
30824 KB |
Output is correct |
54 |
Correct |
15 ms |
16084 KB |
Output is correct |
55 |
Correct |
41 ms |
19640 KB |
Output is correct |
56 |
Correct |
8 ms |
14612 KB |
Output is correct |
57 |
Correct |
15 ms |
15956 KB |
Output is correct |
58 |
Correct |
12 ms |
16192 KB |
Output is correct |
59 |
Correct |
269 ms |
53168 KB |
Output is correct |
60 |
Correct |
424 ms |
68032 KB |
Output is correct |
61 |
Correct |
316 ms |
67788 KB |
Output is correct |
62 |
Correct |
322 ms |
67868 KB |
Output is correct |
63 |
Correct |
350 ms |
68556 KB |
Output is correct |
64 |
Correct |
328 ms |
67812 KB |
Output is correct |
65 |
Correct |
319 ms |
67828 KB |
Output is correct |
66 |
Correct |
119 ms |
36156 KB |
Output is correct |
67 |
Correct |
361 ms |
69652 KB |
Output is correct |
68 |
Correct |
226 ms |
77788 KB |
Output is correct |
69 |
Correct |
128 ms |
48216 KB |
Output is correct |
70 |
Correct |
282 ms |
82020 KB |
Output is correct |
71 |
Correct |
7 ms |
14292 KB |
Output is correct |
72 |
Correct |
8 ms |
14292 KB |
Output is correct |
73 |
Correct |
7 ms |
14292 KB |
Output is correct |
74 |
Correct |
7 ms |
14292 KB |
Output is correct |
75 |
Correct |
7 ms |
14336 KB |
Output is correct |
76 |
Correct |
7 ms |
14352 KB |
Output is correct |
77 |
Correct |
123 ms |
58152 KB |
Output is correct |
78 |
Correct |
119 ms |
58160 KB |
Output is correct |
79 |
Correct |
119 ms |
58116 KB |
Output is correct |
80 |
Correct |
118 ms |
58196 KB |
Output is correct |
81 |
Correct |
224 ms |
75124 KB |
Output is correct |
82 |
Correct |
305 ms |
93808 KB |
Output is correct |
83 |
Correct |
302 ms |
94472 KB |
Output is correct |
84 |
Correct |
630 ms |
105032 KB |
Output is correct |
85 |
Correct |
644 ms |
109124 KB |
Output is correct |
86 |
Correct |
415 ms |
106456 KB |
Output is correct |
87 |
Correct |
454 ms |
111212 KB |
Output is correct |
88 |
Correct |
7 ms |
14292 KB |
Output is correct |
89 |
Correct |
480 ms |
111292 KB |
Output is correct |
90 |
Correct |
405 ms |
108976 KB |
Output is correct |
91 |
Correct |
396 ms |
108492 KB |
Output is correct |