#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 u){
root[u]=u;
if(u>n){
q[u].push(plague[u]);
q[u].push(plague[u]);
return;
}
for(auto v:edges[u]){
dfs(v);
if(q[root[v]].size() > q[root[u]].size()) root[u]=root[v];
}
for(auto v : edges[u])
if(root[v]!=root[u]){
while(q[root[v]].size()){
q[root[u]].push(q[root[v]].top());
q[root[v]].pop();
}
}
for (int i = 1; i < (int) edges[u].size(); i++) q[root[u]].pop();
a=q[root[u]].top(); q[root[u]].pop(); b=q[root[u]].top(); q[root[u]].pop();
a+=plague[u]; b+=plague[u]; q[root[u]].push(b); q[root[u]].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 |
10 ms |
16724 KB |
Output is correct |
3 |
Correct |
9 ms |
16724 KB |
Output is correct |
4 |
Correct |
10 ms |
16708 KB |
Output is correct |
5 |
Correct |
11 ms |
16684 KB |
Output is correct |
6 |
Correct |
9 ms |
16724 KB |
Output is correct |
7 |
Correct |
9 ms |
16724 KB |
Output is correct |
8 |
Correct |
9 ms |
16672 KB |
Output is correct |
9 |
Correct |
9 ms |
16724 KB |
Output is correct |
10 |
Correct |
10 ms |
16724 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
9 ms |
16712 KB |
Output is correct |
2 |
Correct |
9 ms |
16660 KB |
Output is correct |
3 |
Correct |
10 ms |
16724 KB |
Output is correct |
4 |
Correct |
9 ms |
16752 KB |
Output is correct |
5 |
Correct |
9 ms |
16724 KB |
Output is correct |
6 |
Correct |
10 ms |
16768 KB |
Output is correct |
7 |
Correct |
10 ms |
16724 KB |
Output is correct |
8 |
Correct |
9 ms |
16724 KB |
Output is correct |
9 |
Correct |
9 ms |
16724 KB |
Output is correct |
10 |
Correct |
10 ms |
16772 KB |
Output is correct |
11 |
Correct |
11 ms |
16724 KB |
Output is correct |
12 |
Correct |
10 ms |
16724 KB |
Output is correct |
13 |
Correct |
9 ms |
16716 KB |
Output is correct |
14 |
Correct |
9 ms |
16756 KB |
Output is correct |
15 |
Correct |
10 ms |
16724 KB |
Output is correct |
16 |
Correct |
10 ms |
16724 KB |
Output is correct |
17 |
Correct |
11 ms |
16776 KB |
Output is correct |
18 |
Correct |
9 ms |
16724 KB |
Output is correct |
19 |
Correct |
11 ms |
16768 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
9 ms |
16724 KB |
Output is correct |
2 |
Correct |
10 ms |
16724 KB |
Output is correct |
3 |
Correct |
9 ms |
16724 KB |
Output is correct |
4 |
Correct |
10 ms |
16708 KB |
Output is correct |
5 |
Correct |
11 ms |
16684 KB |
Output is correct |
6 |
Correct |
9 ms |
16724 KB |
Output is correct |
7 |
Correct |
9 ms |
16724 KB |
Output is correct |
8 |
Correct |
9 ms |
16672 KB |
Output is correct |
9 |
Correct |
9 ms |
16724 KB |
Output is correct |
10 |
Correct |
10 ms |
16724 KB |
Output is correct |
11 |
Correct |
9 ms |
16712 KB |
Output is correct |
12 |
Correct |
9 ms |
16660 KB |
Output is correct |
13 |
Correct |
10 ms |
16724 KB |
Output is correct |
14 |
Correct |
9 ms |
16752 KB |
Output is correct |
15 |
Correct |
9 ms |
16724 KB |
Output is correct |
16 |
Correct |
10 ms |
16768 KB |
Output is correct |
17 |
Correct |
10 ms |
16724 KB |
Output is correct |
18 |
Correct |
9 ms |
16724 KB |
Output is correct |
19 |
Correct |
9 ms |
16724 KB |
Output is correct |
20 |
Correct |
10 ms |
16772 KB |
Output is correct |
21 |
Correct |
11 ms |
16724 KB |
Output is correct |
22 |
Correct |
10 ms |
16724 KB |
Output is correct |
23 |
Correct |
9 ms |
16716 KB |
Output is correct |
24 |
Correct |
9 ms |
16756 KB |
Output is correct |
25 |
Correct |
10 ms |
16724 KB |
Output is correct |
26 |
Correct |
10 ms |
16724 KB |
Output is correct |
27 |
Correct |
11 ms |
16776 KB |
Output is correct |
28 |
Correct |
9 ms |
16724 KB |
Output is correct |
29 |
Correct |
11 ms |
16768 KB |
Output is correct |
30 |
Correct |
10 ms |
16724 KB |
Output is correct |
31 |
Correct |
10 ms |
16792 KB |
Output is correct |
32 |
Correct |
10 ms |
16852 KB |
Output is correct |
33 |
Correct |
10 ms |
16852 KB |
Output is correct |
34 |
Correct |
11 ms |
16980 KB |
Output is correct |
35 |
Correct |
12 ms |
16976 KB |
Output is correct |
36 |
Correct |
12 ms |
17088 KB |
Output is correct |
37 |
Correct |
12 ms |
17124 KB |
Output is correct |
38 |
Correct |
12 ms |
17044 KB |
Output is correct |
39 |
Correct |
13 ms |
17036 KB |
Output is correct |
40 |
Correct |
11 ms |
17520 KB |
Output is correct |
41 |
Correct |
12 ms |
17564 KB |
Output is correct |
42 |
Correct |
12 ms |
16980 KB |
Output is correct |
43 |
Correct |
14 ms |
17296 KB |
Output is correct |
44 |
Correct |
13 ms |
17168 KB |
Output is correct |
45 |
Correct |
14 ms |
17292 KB |
Output is correct |
46 |
Correct |
13 ms |
17296 KB |
Output is correct |
47 |
Correct |
12 ms |
17236 KB |
Output is correct |
48 |
Correct |
12 ms |
17160 KB |
Output is correct |
49 |
Correct |
12 ms |
17172 KB |
Output is correct |
50 |
Correct |
12 ms |
17108 KB |
Output is correct |
51 |
Correct |
12 ms |
17108 KB |
Output is correct |
52 |
Correct |
12 ms |
17108 KB |
Output is correct |
53 |
Correct |
12 ms |
17224 KB |
Output is correct |
54 |
Correct |
11 ms |
17236 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
9 ms |
16724 KB |
Output is correct |
2 |
Correct |
10 ms |
16724 KB |
Output is correct |
3 |
Correct |
9 ms |
16724 KB |
Output is correct |
4 |
Correct |
10 ms |
16708 KB |
Output is correct |
5 |
Correct |
11 ms |
16684 KB |
Output is correct |
6 |
Correct |
9 ms |
16724 KB |
Output is correct |
7 |
Correct |
9 ms |
16724 KB |
Output is correct |
8 |
Correct |
9 ms |
16672 KB |
Output is correct |
9 |
Correct |
9 ms |
16724 KB |
Output is correct |
10 |
Correct |
10 ms |
16724 KB |
Output is correct |
11 |
Correct |
9 ms |
16712 KB |
Output is correct |
12 |
Correct |
9 ms |
16660 KB |
Output is correct |
13 |
Correct |
10 ms |
16724 KB |
Output is correct |
14 |
Correct |
9 ms |
16752 KB |
Output is correct |
15 |
Correct |
9 ms |
16724 KB |
Output is correct |
16 |
Correct |
10 ms |
16768 KB |
Output is correct |
17 |
Correct |
10 ms |
16724 KB |
Output is correct |
18 |
Correct |
9 ms |
16724 KB |
Output is correct |
19 |
Correct |
9 ms |
16724 KB |
Output is correct |
20 |
Correct |
10 ms |
16772 KB |
Output is correct |
21 |
Correct |
11 ms |
16724 KB |
Output is correct |
22 |
Correct |
10 ms |
16724 KB |
Output is correct |
23 |
Correct |
9 ms |
16716 KB |
Output is correct |
24 |
Correct |
9 ms |
16756 KB |
Output is correct |
25 |
Correct |
10 ms |
16724 KB |
Output is correct |
26 |
Correct |
10 ms |
16724 KB |
Output is correct |
27 |
Correct |
11 ms |
16776 KB |
Output is correct |
28 |
Correct |
9 ms |
16724 KB |
Output is correct |
29 |
Correct |
11 ms |
16768 KB |
Output is correct |
30 |
Correct |
10 ms |
16724 KB |
Output is correct |
31 |
Correct |
10 ms |
16792 KB |
Output is correct |
32 |
Correct |
10 ms |
16852 KB |
Output is correct |
33 |
Correct |
10 ms |
16852 KB |
Output is correct |
34 |
Correct |
11 ms |
16980 KB |
Output is correct |
35 |
Correct |
12 ms |
16976 KB |
Output is correct |
36 |
Correct |
12 ms |
17088 KB |
Output is correct |
37 |
Correct |
12 ms |
17124 KB |
Output is correct |
38 |
Correct |
12 ms |
17044 KB |
Output is correct |
39 |
Correct |
13 ms |
17036 KB |
Output is correct |
40 |
Correct |
11 ms |
17520 KB |
Output is correct |
41 |
Correct |
12 ms |
17564 KB |
Output is correct |
42 |
Correct |
12 ms |
16980 KB |
Output is correct |
43 |
Correct |
14 ms |
17296 KB |
Output is correct |
44 |
Correct |
13 ms |
17168 KB |
Output is correct |
45 |
Correct |
14 ms |
17292 KB |
Output is correct |
46 |
Correct |
13 ms |
17296 KB |
Output is correct |
47 |
Correct |
12 ms |
17236 KB |
Output is correct |
48 |
Correct |
12 ms |
17160 KB |
Output is correct |
49 |
Correct |
12 ms |
17172 KB |
Output is correct |
50 |
Correct |
12 ms |
17108 KB |
Output is correct |
51 |
Correct |
12 ms |
17108 KB |
Output is correct |
52 |
Correct |
12 ms |
17108 KB |
Output is correct |
53 |
Correct |
12 ms |
17224 KB |
Output is correct |
54 |
Correct |
11 ms |
17236 KB |
Output is correct |
55 |
Correct |
17 ms |
17620 KB |
Output is correct |
56 |
Correct |
38 ms |
20040 KB |
Output is correct |
57 |
Correct |
65 ms |
22052 KB |
Output is correct |
58 |
Correct |
83 ms |
23492 KB |
Output is correct |
59 |
Correct |
111 ms |
25844 KB |
Output is correct |
60 |
Correct |
139 ms |
27992 KB |
Output is correct |
61 |
Correct |
163 ms |
29928 KB |
Output is correct |
62 |
Correct |
180 ms |
31172 KB |
Output is correct |
63 |
Correct |
216 ms |
33672 KB |
Output is correct |
64 |
Correct |
222 ms |
34408 KB |
Output is correct |
65 |
Correct |
113 ms |
63796 KB |
Output is correct |
66 |
Correct |
110 ms |
63772 KB |
Output is correct |
67 |
Correct |
116 ms |
31000 KB |
Output is correct |
68 |
Correct |
166 ms |
48640 KB |
Output is correct |
69 |
Correct |
196 ms |
45892 KB |
Output is correct |
70 |
Correct |
187 ms |
45964 KB |
Output is correct |
71 |
Correct |
213 ms |
45544 KB |
Output is correct |
72 |
Correct |
223 ms |
45616 KB |
Output is correct |
73 |
Correct |
192 ms |
42624 KB |
Output is correct |
74 |
Correct |
190 ms |
42724 KB |
Output is correct |
75 |
Correct |
192 ms |
41812 KB |
Output is correct |
76 |
Correct |
189 ms |
41724 KB |
Output is correct |
77 |
Correct |
193 ms |
40268 KB |
Output is correct |
78 |
Correct |
204 ms |
38000 KB |
Output is correct |
79 |
Correct |
174 ms |
40440 KB |
Output is correct |