#include <bits/stdc++.h>
using namespace std;
using ll = long long;
const int N=300010, M = 1e9 + 7;
ll n,m,a,b,plague[N],root[N],ans;
vector<int> edges[N];
priority_queue<ll> q[N];
void dfs(int v){
root[v]=v;
if(v>n){
q[v].push(plague[v]); q[v].push(plague[v]);
return;
}
for(auto i:edges[v]){
dfs(i);
if(q[root[i]].size() > q[root[v]].size()) root[v]=root[i];
}
for(auto i:edges[v]) if(root[i]!=root[v]){
while(q[root[i]].size()){
q[root[v]].push(q[root[i]].top());
q[root[i]].pop();
}
}
for (int i = 1; i < (int) edges[v].size(); i++) q[root[v]].pop();
a=q[root[v]].top(); q[root[v]].pop(); b=q[root[v]].top(); q[root[v]].pop();
a+=plague[v]; b+=plague[v]; q[root[v]].push(b); q[root[v]].push(a);
}
int main(){
ios_base::sync_with_stdio(0); cin.tie(0);
cin >> n >> m;
m += n;
for (int i = 2; i <= m; i++){
cin>>a>>b;
edges[a].push_back(i);
plague[i]=b; ans+=b;
}
dfs(1);
q[root[1]].pop();
a=q[root[1]].top();
q[root[1]].push(0);
b=0;
while(q[root[1]].size() > 0){
ans-=(a - q[root[1]].top()) * b;
b++;
a=q[root[1]].top();
q[root[1]].pop();
}
cout<<ans << "\n";
return 0;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
9 ms |
16724 KB |
Output is correct |
2 |
Correct |
11 ms |
16724 KB |
Output is correct |
3 |
Correct |
9 ms |
16724 KB |
Output is correct |
4 |
Correct |
9 ms |
16724 KB |
Output is correct |
5 |
Correct |
9 ms |
16724 KB |
Output is correct |
6 |
Correct |
10 ms |
16704 KB |
Output is correct |
7 |
Correct |
9 ms |
16724 KB |
Output is correct |
8 |
Correct |
9 ms |
16724 KB |
Output is correct |
9 |
Correct |
9 ms |
16872 KB |
Output is correct |
10 |
Correct |
9 ms |
16668 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
11 ms |
16772 KB |
Output is correct |
2 |
Correct |
10 ms |
16724 KB |
Output is correct |
3 |
Correct |
8 ms |
16724 KB |
Output is correct |
4 |
Correct |
11 ms |
16696 KB |
Output is correct |
5 |
Correct |
9 ms |
16724 KB |
Output is correct |
6 |
Correct |
10 ms |
16724 KB |
Output is correct |
7 |
Correct |
9 ms |
16724 KB |
Output is correct |
8 |
Correct |
9 ms |
16688 KB |
Output is correct |
9 |
Correct |
9 ms |
16784 KB |
Output is correct |
10 |
Correct |
11 ms |
16724 KB |
Output is correct |
11 |
Correct |
9 ms |
16724 KB |
Output is correct |
12 |
Correct |
9 ms |
16724 KB |
Output is correct |
13 |
Correct |
10 ms |
16724 KB |
Output is correct |
14 |
Correct |
10 ms |
16724 KB |
Output is correct |
15 |
Correct |
9 ms |
16724 KB |
Output is correct |
16 |
Correct |
11 ms |
16724 KB |
Output is correct |
17 |
Correct |
10 ms |
16676 KB |
Output is correct |
18 |
Correct |
9 ms |
16736 KB |
Output is correct |
19 |
Correct |
9 ms |
16756 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
9 ms |
16724 KB |
Output is correct |
2 |
Correct |
11 ms |
16724 KB |
Output is correct |
3 |
Correct |
9 ms |
16724 KB |
Output is correct |
4 |
Correct |
9 ms |
16724 KB |
Output is correct |
5 |
Correct |
9 ms |
16724 KB |
Output is correct |
6 |
Correct |
10 ms |
16704 KB |
Output is correct |
7 |
Correct |
9 ms |
16724 KB |
Output is correct |
8 |
Correct |
9 ms |
16724 KB |
Output is correct |
9 |
Correct |
9 ms |
16872 KB |
Output is correct |
10 |
Correct |
9 ms |
16668 KB |
Output is correct |
11 |
Correct |
11 ms |
16772 KB |
Output is correct |
12 |
Correct |
10 ms |
16724 KB |
Output is correct |
13 |
Correct |
8 ms |
16724 KB |
Output is correct |
14 |
Correct |
11 ms |
16696 KB |
Output is correct |
15 |
Correct |
9 ms |
16724 KB |
Output is correct |
16 |
Correct |
10 ms |
16724 KB |
Output is correct |
17 |
Correct |
9 ms |
16724 KB |
Output is correct |
18 |
Correct |
9 ms |
16688 KB |
Output is correct |
19 |
Correct |
9 ms |
16784 KB |
Output is correct |
20 |
Correct |
11 ms |
16724 KB |
Output is correct |
21 |
Correct |
9 ms |
16724 KB |
Output is correct |
22 |
Correct |
9 ms |
16724 KB |
Output is correct |
23 |
Correct |
10 ms |
16724 KB |
Output is correct |
24 |
Correct |
10 ms |
16724 KB |
Output is correct |
25 |
Correct |
9 ms |
16724 KB |
Output is correct |
26 |
Correct |
11 ms |
16724 KB |
Output is correct |
27 |
Correct |
10 ms |
16676 KB |
Output is correct |
28 |
Correct |
9 ms |
16736 KB |
Output is correct |
29 |
Correct |
9 ms |
16756 KB |
Output is correct |
30 |
Correct |
9 ms |
16804 KB |
Output is correct |
31 |
Correct |
9 ms |
16788 KB |
Output is correct |
32 |
Correct |
10 ms |
16852 KB |
Output is correct |
33 |
Correct |
11 ms |
16800 KB |
Output is correct |
34 |
Correct |
10 ms |
16852 KB |
Output is correct |
35 |
Correct |
11 ms |
16980 KB |
Output is correct |
36 |
Correct |
10 ms |
16980 KB |
Output is correct |
37 |
Correct |
11 ms |
16980 KB |
Output is correct |
38 |
Correct |
12 ms |
16980 KB |
Output is correct |
39 |
Correct |
12 ms |
16980 KB |
Output is correct |
40 |
Correct |
11 ms |
17492 KB |
Output is correct |
41 |
Correct |
10 ms |
17556 KB |
Output is correct |
42 |
Correct |
10 ms |
16980 KB |
Output is correct |
43 |
Correct |
11 ms |
17308 KB |
Output is correct |
44 |
Correct |
14 ms |
17148 KB |
Output is correct |
45 |
Correct |
12 ms |
17208 KB |
Output is correct |
46 |
Correct |
12 ms |
17232 KB |
Output is correct |
47 |
Correct |
14 ms |
17184 KB |
Output is correct |
48 |
Correct |
11 ms |
17108 KB |
Output is correct |
49 |
Correct |
12 ms |
17072 KB |
Output is correct |
50 |
Correct |
12 ms |
16980 KB |
Output is correct |
51 |
Correct |
10 ms |
16980 KB |
Output is correct |
52 |
Correct |
14 ms |
17108 KB |
Output is correct |
53 |
Correct |
11 ms |
17108 KB |
Output is correct |
54 |
Correct |
11 ms |
17172 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
9 ms |
16724 KB |
Output is correct |
2 |
Correct |
11 ms |
16724 KB |
Output is correct |
3 |
Correct |
9 ms |
16724 KB |
Output is correct |
4 |
Correct |
9 ms |
16724 KB |
Output is correct |
5 |
Correct |
9 ms |
16724 KB |
Output is correct |
6 |
Correct |
10 ms |
16704 KB |
Output is correct |
7 |
Correct |
9 ms |
16724 KB |
Output is correct |
8 |
Correct |
9 ms |
16724 KB |
Output is correct |
9 |
Correct |
9 ms |
16872 KB |
Output is correct |
10 |
Correct |
9 ms |
16668 KB |
Output is correct |
11 |
Correct |
11 ms |
16772 KB |
Output is correct |
12 |
Correct |
10 ms |
16724 KB |
Output is correct |
13 |
Correct |
8 ms |
16724 KB |
Output is correct |
14 |
Correct |
11 ms |
16696 KB |
Output is correct |
15 |
Correct |
9 ms |
16724 KB |
Output is correct |
16 |
Correct |
10 ms |
16724 KB |
Output is correct |
17 |
Correct |
9 ms |
16724 KB |
Output is correct |
18 |
Correct |
9 ms |
16688 KB |
Output is correct |
19 |
Correct |
9 ms |
16784 KB |
Output is correct |
20 |
Correct |
11 ms |
16724 KB |
Output is correct |
21 |
Correct |
9 ms |
16724 KB |
Output is correct |
22 |
Correct |
9 ms |
16724 KB |
Output is correct |
23 |
Correct |
10 ms |
16724 KB |
Output is correct |
24 |
Correct |
10 ms |
16724 KB |
Output is correct |
25 |
Correct |
9 ms |
16724 KB |
Output is correct |
26 |
Correct |
11 ms |
16724 KB |
Output is correct |
27 |
Correct |
10 ms |
16676 KB |
Output is correct |
28 |
Correct |
9 ms |
16736 KB |
Output is correct |
29 |
Correct |
9 ms |
16756 KB |
Output is correct |
30 |
Correct |
9 ms |
16804 KB |
Output is correct |
31 |
Correct |
9 ms |
16788 KB |
Output is correct |
32 |
Correct |
10 ms |
16852 KB |
Output is correct |
33 |
Correct |
11 ms |
16800 KB |
Output is correct |
34 |
Correct |
10 ms |
16852 KB |
Output is correct |
35 |
Correct |
11 ms |
16980 KB |
Output is correct |
36 |
Correct |
10 ms |
16980 KB |
Output is correct |
37 |
Correct |
11 ms |
16980 KB |
Output is correct |
38 |
Correct |
12 ms |
16980 KB |
Output is correct |
39 |
Correct |
12 ms |
16980 KB |
Output is correct |
40 |
Correct |
11 ms |
17492 KB |
Output is correct |
41 |
Correct |
10 ms |
17556 KB |
Output is correct |
42 |
Correct |
10 ms |
16980 KB |
Output is correct |
43 |
Correct |
11 ms |
17308 KB |
Output is correct |
44 |
Correct |
14 ms |
17148 KB |
Output is correct |
45 |
Correct |
12 ms |
17208 KB |
Output is correct |
46 |
Correct |
12 ms |
17232 KB |
Output is correct |
47 |
Correct |
14 ms |
17184 KB |
Output is correct |
48 |
Correct |
11 ms |
17108 KB |
Output is correct |
49 |
Correct |
12 ms |
17072 KB |
Output is correct |
50 |
Correct |
12 ms |
16980 KB |
Output is correct |
51 |
Correct |
10 ms |
16980 KB |
Output is correct |
52 |
Correct |
14 ms |
17108 KB |
Output is correct |
53 |
Correct |
11 ms |
17108 KB |
Output is correct |
54 |
Correct |
11 ms |
17172 KB |
Output is correct |
55 |
Correct |
16 ms |
17528 KB |
Output is correct |
56 |
Correct |
46 ms |
19864 KB |
Output is correct |
57 |
Correct |
65 ms |
22020 KB |
Output is correct |
58 |
Correct |
78 ms |
23488 KB |
Output is correct |
59 |
Correct |
106 ms |
25708 KB |
Output is correct |
60 |
Correct |
150 ms |
27828 KB |
Output is correct |
61 |
Correct |
156 ms |
29732 KB |
Output is correct |
62 |
Correct |
166 ms |
31024 KB |
Output is correct |
63 |
Correct |
202 ms |
33580 KB |
Output is correct |
64 |
Correct |
291 ms |
34316 KB |
Output is correct |
65 |
Correct |
114 ms |
63692 KB |
Output is correct |
66 |
Correct |
107 ms |
63684 KB |
Output is correct |
67 |
Correct |
111 ms |
30892 KB |
Output is correct |
68 |
Correct |
175 ms |
48504 KB |
Output is correct |
69 |
Correct |
188 ms |
45756 KB |
Output is correct |
70 |
Correct |
217 ms |
45964 KB |
Output is correct |
71 |
Correct |
208 ms |
45472 KB |
Output is correct |
72 |
Correct |
215 ms |
45452 KB |
Output is correct |
73 |
Correct |
191 ms |
42676 KB |
Output is correct |
74 |
Correct |
189 ms |
42592 KB |
Output is correct |
75 |
Correct |
207 ms |
41628 KB |
Output is correct |
76 |
Correct |
190 ms |
41620 KB |
Output is correct |
77 |
Correct |
197 ms |
40136 KB |
Output is correct |
78 |
Correct |
224 ms |
37940 KB |
Output is correct |
79 |
Correct |
179 ms |
40368 KB |
Output is correct |