#include <bits/stdc++.h>
#pragma GCC optimize("O3")
#pragma GCC target("sse4")
using namespace std;
#define FOR(a,b) for(int a=0;a<b;a++)
#define F0R(a,b,c) for(int a = b; a<=c; a++)
#define f first
#define s second
#define m0(x) memset(x,0,sizeof(x))
#define all(x) x.begin(), x.end()
typedef pair<int,int> pi;
typedef long long ll;
typedef vector<int> vi;
typedef vector<pi> vpi;
struct hull{
priority_queue<ll> lhull;
priority_queue<ll,vector<ll>,greater<ll>> rhull;
ll height = 0;
void putpar(ll len){
if(lhull.empty()){
lhull.push(len);
rhull.push(len);
return;
}
ll a = lhull.top(); lhull.pop(); a += len;
lhull.push(a);
a = rhull.top();
while(!rhull.empty()) rhull.pop();
rhull.push(a+len);
}
void plu(ll num){
lhull.push(num);
ll a = lhull.top(); lhull.pop();
height += a-num;
rhull.push(a);
}
void pld(ll num){
rhull.push(num);
ll a = rhull.top(); rhull.pop();
height += num - a;
lhull.push(a);
}
void ins(ll num){
plu(num);
pld(num);
}
void combine(hull & other){
while(!other.lhull.empty()){
ll a = other.lhull.top();
other.lhull.pop();
pld(a);
}
while(!other.rhull.empty()){
ll a = other.rhull.top();
other.rhull.pop();
plu(a);
}
height += other.height;
}
};
vector<ll> children, root;
const int large = 3e5+1;
vi edges[large];
int dfs1(int child){
int ccnt = 1;
for(auto &a: edges[child]) ccnt += dfs1(a);
children[child] = ccnt;
return ccnt;
}
void dfs2(int child, hull &ans){
const int sz = edges[child].size();
vi &e = edges[child];
FOR(i,sz){
if(children[e[i]] > children[e[0]])
swap(e[i], e[0]);
}
if(sz != 0){
dfs2(e[0],ans);
}
F0R(i,1,sz-1){
hull cur;
dfs2(e[i],cur);
ans.combine(cur);
}
ans.putpar(root[child]);
}
int main()
{
ios_base::sync_with_stdio(0); cin.tie(0);
int n,m; cin >> n >> m;
hull ans;
children.resize(n+m+1);
root.resize(n+m+1);
F0R(i,2,n+m){
int par;
cin >> par >> root[i];
edges[par].push_back(i);
}
dfs1(1);
dfs2(1,ans);
cout << ans.height << "\n";
return 0;
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
5 ms |
7424 KB |
Output is correct |
2 |
Correct |
5 ms |
7424 KB |
Output is correct |
3 |
Correct |
5 ms |
7424 KB |
Output is correct |
4 |
Correct |
5 ms |
7424 KB |
Output is correct |
5 |
Correct |
6 ms |
7424 KB |
Output is correct |
6 |
Correct |
5 ms |
7424 KB |
Output is correct |
7 |
Correct |
5 ms |
7424 KB |
Output is correct |
8 |
Correct |
5 ms |
7424 KB |
Output is correct |
9 |
Correct |
5 ms |
7424 KB |
Output is correct |
10 |
Correct |
5 ms |
7424 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
5 ms |
7424 KB |
Output is correct |
2 |
Correct |
5 ms |
7424 KB |
Output is correct |
3 |
Correct |
5 ms |
7424 KB |
Output is correct |
4 |
Correct |
5 ms |
7424 KB |
Output is correct |
5 |
Correct |
5 ms |
7424 KB |
Output is correct |
6 |
Correct |
5 ms |
7424 KB |
Output is correct |
7 |
Correct |
5 ms |
7424 KB |
Output is correct |
8 |
Correct |
5 ms |
7424 KB |
Output is correct |
9 |
Correct |
5 ms |
7424 KB |
Output is correct |
10 |
Correct |
5 ms |
7424 KB |
Output is correct |
11 |
Correct |
5 ms |
7424 KB |
Output is correct |
12 |
Correct |
5 ms |
7424 KB |
Output is correct |
13 |
Correct |
5 ms |
7424 KB |
Output is correct |
14 |
Correct |
5 ms |
7424 KB |
Output is correct |
15 |
Correct |
5 ms |
7424 KB |
Output is correct |
16 |
Correct |
5 ms |
7424 KB |
Output is correct |
17 |
Correct |
5 ms |
7424 KB |
Output is correct |
18 |
Correct |
5 ms |
7424 KB |
Output is correct |
19 |
Correct |
5 ms |
7424 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
5 ms |
7424 KB |
Output is correct |
2 |
Correct |
5 ms |
7424 KB |
Output is correct |
3 |
Correct |
5 ms |
7424 KB |
Output is correct |
4 |
Correct |
5 ms |
7424 KB |
Output is correct |
5 |
Correct |
6 ms |
7424 KB |
Output is correct |
6 |
Correct |
5 ms |
7424 KB |
Output is correct |
7 |
Correct |
5 ms |
7424 KB |
Output is correct |
8 |
Correct |
5 ms |
7424 KB |
Output is correct |
9 |
Correct |
5 ms |
7424 KB |
Output is correct |
10 |
Correct |
5 ms |
7424 KB |
Output is correct |
11 |
Correct |
5 ms |
7424 KB |
Output is correct |
12 |
Correct |
5 ms |
7424 KB |
Output is correct |
13 |
Correct |
5 ms |
7424 KB |
Output is correct |
14 |
Correct |
5 ms |
7424 KB |
Output is correct |
15 |
Correct |
5 ms |
7424 KB |
Output is correct |
16 |
Correct |
5 ms |
7424 KB |
Output is correct |
17 |
Correct |
5 ms |
7424 KB |
Output is correct |
18 |
Correct |
5 ms |
7424 KB |
Output is correct |
19 |
Correct |
5 ms |
7424 KB |
Output is correct |
20 |
Correct |
5 ms |
7424 KB |
Output is correct |
21 |
Correct |
5 ms |
7424 KB |
Output is correct |
22 |
Correct |
5 ms |
7424 KB |
Output is correct |
23 |
Correct |
5 ms |
7424 KB |
Output is correct |
24 |
Correct |
5 ms |
7424 KB |
Output is correct |
25 |
Correct |
5 ms |
7424 KB |
Output is correct |
26 |
Correct |
5 ms |
7424 KB |
Output is correct |
27 |
Correct |
5 ms |
7424 KB |
Output is correct |
28 |
Correct |
5 ms |
7424 KB |
Output is correct |
29 |
Correct |
5 ms |
7424 KB |
Output is correct |
30 |
Correct |
5 ms |
7456 KB |
Output is correct |
31 |
Correct |
5 ms |
7424 KB |
Output is correct |
32 |
Correct |
6 ms |
7552 KB |
Output is correct |
33 |
Correct |
6 ms |
7552 KB |
Output is correct |
34 |
Correct |
6 ms |
7552 KB |
Output is correct |
35 |
Correct |
7 ms |
7552 KB |
Output is correct |
36 |
Correct |
7 ms |
7680 KB |
Output is correct |
37 |
Correct |
7 ms |
7680 KB |
Output is correct |
38 |
Correct |
8 ms |
7680 KB |
Output is correct |
39 |
Correct |
7 ms |
7680 KB |
Output is correct |
40 |
Correct |
11 ms |
8576 KB |
Output is correct |
41 |
Correct |
8 ms |
8576 KB |
Output is correct |
42 |
Correct |
7 ms |
7680 KB |
Output is correct |
43 |
Correct |
9 ms |
8192 KB |
Output is correct |
44 |
Correct |
8 ms |
7936 KB |
Output is correct |
45 |
Correct |
9 ms |
7968 KB |
Output is correct |
46 |
Correct |
9 ms |
7680 KB |
Output is correct |
47 |
Correct |
9 ms |
7680 KB |
Output is correct |
48 |
Correct |
9 ms |
7680 KB |
Output is correct |
49 |
Correct |
9 ms |
7680 KB |
Output is correct |
50 |
Correct |
7 ms |
7680 KB |
Output is correct |
51 |
Correct |
7 ms |
7712 KB |
Output is correct |
52 |
Correct |
8 ms |
7680 KB |
Output is correct |
53 |
Correct |
8 ms |
7680 KB |
Output is correct |
54 |
Correct |
8 ms |
7680 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
5 ms |
7424 KB |
Output is correct |
2 |
Correct |
5 ms |
7424 KB |
Output is correct |
3 |
Correct |
5 ms |
7424 KB |
Output is correct |
4 |
Correct |
5 ms |
7424 KB |
Output is correct |
5 |
Correct |
6 ms |
7424 KB |
Output is correct |
6 |
Correct |
5 ms |
7424 KB |
Output is correct |
7 |
Correct |
5 ms |
7424 KB |
Output is correct |
8 |
Correct |
5 ms |
7424 KB |
Output is correct |
9 |
Correct |
5 ms |
7424 KB |
Output is correct |
10 |
Correct |
5 ms |
7424 KB |
Output is correct |
11 |
Correct |
5 ms |
7424 KB |
Output is correct |
12 |
Correct |
5 ms |
7424 KB |
Output is correct |
13 |
Correct |
5 ms |
7424 KB |
Output is correct |
14 |
Correct |
5 ms |
7424 KB |
Output is correct |
15 |
Correct |
5 ms |
7424 KB |
Output is correct |
16 |
Correct |
5 ms |
7424 KB |
Output is correct |
17 |
Correct |
5 ms |
7424 KB |
Output is correct |
18 |
Correct |
5 ms |
7424 KB |
Output is correct |
19 |
Correct |
5 ms |
7424 KB |
Output is correct |
20 |
Correct |
5 ms |
7424 KB |
Output is correct |
21 |
Correct |
5 ms |
7424 KB |
Output is correct |
22 |
Correct |
5 ms |
7424 KB |
Output is correct |
23 |
Correct |
5 ms |
7424 KB |
Output is correct |
24 |
Correct |
5 ms |
7424 KB |
Output is correct |
25 |
Correct |
5 ms |
7424 KB |
Output is correct |
26 |
Correct |
5 ms |
7424 KB |
Output is correct |
27 |
Correct |
5 ms |
7424 KB |
Output is correct |
28 |
Correct |
5 ms |
7424 KB |
Output is correct |
29 |
Correct |
5 ms |
7424 KB |
Output is correct |
30 |
Correct |
5 ms |
7456 KB |
Output is correct |
31 |
Correct |
5 ms |
7424 KB |
Output is correct |
32 |
Correct |
6 ms |
7552 KB |
Output is correct |
33 |
Correct |
6 ms |
7552 KB |
Output is correct |
34 |
Correct |
6 ms |
7552 KB |
Output is correct |
35 |
Correct |
7 ms |
7552 KB |
Output is correct |
36 |
Correct |
7 ms |
7680 KB |
Output is correct |
37 |
Correct |
7 ms |
7680 KB |
Output is correct |
38 |
Correct |
8 ms |
7680 KB |
Output is correct |
39 |
Correct |
7 ms |
7680 KB |
Output is correct |
40 |
Correct |
11 ms |
8576 KB |
Output is correct |
41 |
Correct |
8 ms |
8576 KB |
Output is correct |
42 |
Correct |
7 ms |
7680 KB |
Output is correct |
43 |
Correct |
9 ms |
8192 KB |
Output is correct |
44 |
Correct |
8 ms |
7936 KB |
Output is correct |
45 |
Correct |
9 ms |
7968 KB |
Output is correct |
46 |
Correct |
9 ms |
7680 KB |
Output is correct |
47 |
Correct |
9 ms |
7680 KB |
Output is correct |
48 |
Correct |
9 ms |
7680 KB |
Output is correct |
49 |
Correct |
9 ms |
7680 KB |
Output is correct |
50 |
Correct |
7 ms |
7680 KB |
Output is correct |
51 |
Correct |
7 ms |
7712 KB |
Output is correct |
52 |
Correct |
8 ms |
7680 KB |
Output is correct |
53 |
Correct |
8 ms |
7680 KB |
Output is correct |
54 |
Correct |
8 ms |
7680 KB |
Output is correct |
55 |
Correct |
12 ms |
8192 KB |
Output is correct |
56 |
Correct |
43 ms |
10480 KB |
Output is correct |
57 |
Correct |
61 ms |
12280 KB |
Output is correct |
58 |
Correct |
89 ms |
13860 KB |
Output is correct |
59 |
Correct |
115 ms |
15696 KB |
Output is correct |
60 |
Correct |
162 ms |
17440 KB |
Output is correct |
61 |
Correct |
182 ms |
19412 KB |
Output is correct |
62 |
Correct |
211 ms |
20456 KB |
Output is correct |
63 |
Correct |
277 ms |
22780 KB |
Output is correct |
64 |
Correct |
286 ms |
23232 KB |
Output is correct |
65 |
Correct |
134 ms |
82772 KB |
Output is correct |
66 |
Correct |
137 ms |
82680 KB |
Output is correct |
67 |
Correct |
140 ms |
26488 KB |
Output is correct |
68 |
Correct |
187 ms |
51948 KB |
Output is correct |
69 |
Correct |
215 ms |
47232 KB |
Output is correct |
70 |
Correct |
217 ms |
47088 KB |
Output is correct |
71 |
Correct |
237 ms |
22632 KB |
Output is correct |
72 |
Correct |
233 ms |
22620 KB |
Output is correct |
73 |
Correct |
218 ms |
21992 KB |
Output is correct |
74 |
Correct |
213 ms |
21992 KB |
Output is correct |
75 |
Correct |
218 ms |
21864 KB |
Output is correct |
76 |
Correct |
216 ms |
21932 KB |
Output is correct |
77 |
Correct |
235 ms |
22656 KB |
Output is correct |
78 |
Correct |
233 ms |
22964 KB |
Output is correct |
79 |
Correct |
215 ms |
24940 KB |
Output is correct |