#include "bits/stdc++.h"
using namespace std;
#define int int64_t
#define pb push_back
using pii=pair<int,int>;
const int lim=2e5+100;
const int mod=1e9+7;
#ifndef Local
#define pd(x) ;
#else
void pd(priority_queue<int>pq){
while(pq.size()){
cerr<<pq.top()<<' ';
pq.pop();
}
cerr<<'\n';
}
#endif
signed main(){
ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0);
#ifdef Local
freopen(".in","r",stdin);freopen(".out","w",stdout);
#endif
int n,m;
cin>>n>>m;
int N=n+m;
int d[N]{};
vector<int>v[N];
for(int i=1;i<N;i++){
int p;
cin>>p>>d[i];
p--;
v[p].pb(i);
}
priority_queue<int>l[N];
priority_queue<int,vector<int>,greater<int>>r[N];
for(int i=N-1;n<=i;i--){
l[i].push(d[i]);
r[i].push(d[i]);
}
int ans=0;
for(int i=n-1;0<=i;i--){
for(int j:v[i]){
if(!l[i].size()){
l[i].swap(l[j]);
r[i].swap(r[j]);
continue;
}
if(l[i].size()<l[j].size()){
l[i].swap(l[j]);
r[i].swap(r[j]);
}
while(l[j].size()){
int lt=l[j].top(),rt=r[j].top();
l[j].pop(),r[j].pop();
if(rt<l[i].top()){
ans+=l[i].top()-rt;
r[i].push(l[i].top());
l[i].pop();
l[i].push(lt);
l[i].push(rt);
}else if(r[i].top()<lt){
ans+=lt-r[i].top();
l[i].push(r[i].top());
r[i].pop();
r[i].push(lt);
r[i].push(rt);
}else{
l[i].push(lt);
r[i].push(rt);
}
}
}
int k=r[i].top();
while(r[i].size()&&r[i].top()!=LLONG_MAX){
r[i].pop();
}
int tt=l[i].top();
l[i].pop();
l[i].push(tt+d[i]);
r[i].push(k+d[i]);
while(r[i].size()<l[i].size())r[i].push(LLONG_MAX);
}
cout<<ans<<'\n';
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
348 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
0 ms |
348 KB |
Output is correct |
4 |
Correct |
0 ms |
348 KB |
Output is correct |
5 |
Correct |
0 ms |
344 KB |
Output is correct |
6 |
Correct |
0 ms |
348 KB |
Output is correct |
7 |
Correct |
0 ms |
348 KB |
Output is correct |
8 |
Correct |
0 ms |
348 KB |
Output is correct |
9 |
Correct |
0 ms |
348 KB |
Output is correct |
10 |
Correct |
1 ms |
348 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
348 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
0 ms |
348 KB |
Output is correct |
4 |
Correct |
1 ms |
348 KB |
Output is correct |
5 |
Correct |
0 ms |
348 KB |
Output is correct |
6 |
Correct |
0 ms |
348 KB |
Output is correct |
7 |
Correct |
1 ms |
344 KB |
Output is correct |
8 |
Correct |
0 ms |
348 KB |
Output is correct |
9 |
Correct |
0 ms |
348 KB |
Output is correct |
10 |
Correct |
0 ms |
348 KB |
Output is correct |
11 |
Correct |
1 ms |
348 KB |
Output is correct |
12 |
Correct |
0 ms |
348 KB |
Output is correct |
13 |
Correct |
0 ms |
348 KB |
Output is correct |
14 |
Correct |
0 ms |
348 KB |
Output is correct |
15 |
Correct |
0 ms |
348 KB |
Output is correct |
16 |
Correct |
1 ms |
348 KB |
Output is correct |
17 |
Correct |
1 ms |
348 KB |
Output is correct |
18 |
Correct |
0 ms |
348 KB |
Output is correct |
19 |
Correct |
0 ms |
348 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
348 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
0 ms |
348 KB |
Output is correct |
4 |
Correct |
0 ms |
348 KB |
Output is correct |
5 |
Correct |
0 ms |
344 KB |
Output is correct |
6 |
Correct |
0 ms |
348 KB |
Output is correct |
7 |
Correct |
0 ms |
348 KB |
Output is correct |
8 |
Correct |
0 ms |
348 KB |
Output is correct |
9 |
Correct |
0 ms |
348 KB |
Output is correct |
10 |
Correct |
1 ms |
348 KB |
Output is correct |
11 |
Correct |
0 ms |
348 KB |
Output is correct |
12 |
Correct |
0 ms |
348 KB |
Output is correct |
13 |
Correct |
0 ms |
348 KB |
Output is correct |
14 |
Correct |
1 ms |
348 KB |
Output is correct |
15 |
Correct |
0 ms |
348 KB |
Output is correct |
16 |
Correct |
0 ms |
348 KB |
Output is correct |
17 |
Correct |
1 ms |
344 KB |
Output is correct |
18 |
Correct |
0 ms |
348 KB |
Output is correct |
19 |
Correct |
0 ms |
348 KB |
Output is correct |
20 |
Correct |
0 ms |
348 KB |
Output is correct |
21 |
Correct |
1 ms |
348 KB |
Output is correct |
22 |
Correct |
0 ms |
348 KB |
Output is correct |
23 |
Correct |
0 ms |
348 KB |
Output is correct |
24 |
Correct |
0 ms |
348 KB |
Output is correct |
25 |
Correct |
0 ms |
348 KB |
Output is correct |
26 |
Correct |
1 ms |
348 KB |
Output is correct |
27 |
Correct |
1 ms |
348 KB |
Output is correct |
28 |
Correct |
0 ms |
348 KB |
Output is correct |
29 |
Correct |
0 ms |
348 KB |
Output is correct |
30 |
Correct |
1 ms |
344 KB |
Output is correct |
31 |
Correct |
1 ms |
604 KB |
Output is correct |
32 |
Correct |
1 ms |
604 KB |
Output is correct |
33 |
Correct |
1 ms |
728 KB |
Output is correct |
34 |
Correct |
2 ms |
896 KB |
Output is correct |
35 |
Correct |
2 ms |
1116 KB |
Output is correct |
36 |
Correct |
2 ms |
1116 KB |
Output is correct |
37 |
Correct |
2 ms |
1116 KB |
Output is correct |
38 |
Correct |
2 ms |
1372 KB |
Output is correct |
39 |
Correct |
2 ms |
1372 KB |
Output is correct |
40 |
Correct |
2 ms |
1116 KB |
Output is correct |
41 |
Correct |
2 ms |
1116 KB |
Output is correct |
42 |
Correct |
1 ms |
1116 KB |
Output is correct |
43 |
Correct |
3 ms |
1116 KB |
Output is correct |
44 |
Correct |
3 ms |
1120 KB |
Output is correct |
45 |
Correct |
2 ms |
1120 KB |
Output is correct |
46 |
Correct |
3 ms |
1460 KB |
Output is correct |
47 |
Correct |
3 ms |
1464 KB |
Output is correct |
48 |
Correct |
2 ms |
1376 KB |
Output is correct |
49 |
Correct |
3 ms |
1380 KB |
Output is correct |
50 |
Correct |
2 ms |
1120 KB |
Output is correct |
51 |
Correct |
2 ms |
1120 KB |
Output is correct |
52 |
Correct |
3 ms |
1400 KB |
Output is correct |
53 |
Correct |
3 ms |
1420 KB |
Output is correct |
54 |
Correct |
3 ms |
1376 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
348 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
0 ms |
348 KB |
Output is correct |
4 |
Correct |
0 ms |
348 KB |
Output is correct |
5 |
Correct |
0 ms |
344 KB |
Output is correct |
6 |
Correct |
0 ms |
348 KB |
Output is correct |
7 |
Correct |
0 ms |
348 KB |
Output is correct |
8 |
Correct |
0 ms |
348 KB |
Output is correct |
9 |
Correct |
0 ms |
348 KB |
Output is correct |
10 |
Correct |
1 ms |
348 KB |
Output is correct |
11 |
Correct |
0 ms |
348 KB |
Output is correct |
12 |
Correct |
0 ms |
348 KB |
Output is correct |
13 |
Correct |
0 ms |
348 KB |
Output is correct |
14 |
Correct |
1 ms |
348 KB |
Output is correct |
15 |
Correct |
0 ms |
348 KB |
Output is correct |
16 |
Correct |
0 ms |
348 KB |
Output is correct |
17 |
Correct |
1 ms |
344 KB |
Output is correct |
18 |
Correct |
0 ms |
348 KB |
Output is correct |
19 |
Correct |
0 ms |
348 KB |
Output is correct |
20 |
Correct |
0 ms |
348 KB |
Output is correct |
21 |
Correct |
1 ms |
348 KB |
Output is correct |
22 |
Correct |
0 ms |
348 KB |
Output is correct |
23 |
Correct |
0 ms |
348 KB |
Output is correct |
24 |
Correct |
0 ms |
348 KB |
Output is correct |
25 |
Correct |
0 ms |
348 KB |
Output is correct |
26 |
Correct |
1 ms |
348 KB |
Output is correct |
27 |
Correct |
1 ms |
348 KB |
Output is correct |
28 |
Correct |
0 ms |
348 KB |
Output is correct |
29 |
Correct |
0 ms |
348 KB |
Output is correct |
30 |
Correct |
1 ms |
344 KB |
Output is correct |
31 |
Correct |
1 ms |
604 KB |
Output is correct |
32 |
Correct |
1 ms |
604 KB |
Output is correct |
33 |
Correct |
1 ms |
728 KB |
Output is correct |
34 |
Correct |
2 ms |
896 KB |
Output is correct |
35 |
Correct |
2 ms |
1116 KB |
Output is correct |
36 |
Correct |
2 ms |
1116 KB |
Output is correct |
37 |
Correct |
2 ms |
1116 KB |
Output is correct |
38 |
Correct |
2 ms |
1372 KB |
Output is correct |
39 |
Correct |
2 ms |
1372 KB |
Output is correct |
40 |
Correct |
2 ms |
1116 KB |
Output is correct |
41 |
Correct |
2 ms |
1116 KB |
Output is correct |
42 |
Correct |
1 ms |
1116 KB |
Output is correct |
43 |
Correct |
3 ms |
1116 KB |
Output is correct |
44 |
Correct |
3 ms |
1120 KB |
Output is correct |
45 |
Correct |
2 ms |
1120 KB |
Output is correct |
46 |
Correct |
3 ms |
1460 KB |
Output is correct |
47 |
Correct |
3 ms |
1464 KB |
Output is correct |
48 |
Correct |
2 ms |
1376 KB |
Output is correct |
49 |
Correct |
3 ms |
1380 KB |
Output is correct |
50 |
Correct |
2 ms |
1120 KB |
Output is correct |
51 |
Correct |
2 ms |
1120 KB |
Output is correct |
52 |
Correct |
3 ms |
1400 KB |
Output is correct |
53 |
Correct |
3 ms |
1420 KB |
Output is correct |
54 |
Correct |
3 ms |
1376 KB |
Output is correct |
55 |
Correct |
7 ms |
2652 KB |
Output is correct |
56 |
Correct |
24 ms |
9820 KB |
Output is correct |
57 |
Correct |
43 ms |
16272 KB |
Output is correct |
58 |
Correct |
53 ms |
20824 KB |
Output is correct |
59 |
Correct |
71 ms |
27424 KB |
Output is correct |
60 |
Correct |
79 ms |
34128 KB |
Output is correct |
61 |
Correct |
92 ms |
38736 KB |
Output is correct |
62 |
Correct |
113 ms |
43100 KB |
Output is correct |
63 |
Correct |
138 ms |
51124 KB |
Output is correct |
64 |
Correct |
135 ms |
53320 KB |
Output is correct |
65 |
Correct |
65 ms |
42836 KB |
Output is correct |
66 |
Correct |
65 ms |
42836 KB |
Output is correct |
67 |
Correct |
68 ms |
42732 KB |
Output is correct |
68 |
Correct |
110 ms |
50536 KB |
Output is correct |
69 |
Correct |
121 ms |
51136 KB |
Output is correct |
70 |
Correct |
124 ms |
51136 KB |
Output is correct |
71 |
Correct |
184 ms |
75120 KB |
Output is correct |
72 |
Correct |
182 ms |
73972 KB |
Output is correct |
73 |
Correct |
160 ms |
69756 KB |
Output is correct |
74 |
Correct |
162 ms |
68968 KB |
Output is correct |
75 |
Correct |
158 ms |
69340 KB |
Output is correct |
76 |
Correct |
165 ms |
66680 KB |
Output is correct |
77 |
Correct |
176 ms |
66364 KB |
Output is correct |
78 |
Correct |
173 ms |
65024 KB |
Output is correct |
79 |
Correct |
160 ms |
64704 KB |
Output is correct |