#include <bits/stdc++.h>
#include <bits/extc++.h>
#define int long long
#define fastio ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0);
using namespace std;
const int N = 6e5+5;
vector<pair<int,int>> adj[N];
int dp[N], to[N], deg[N], ans = 0;
__gnu_pbds::priority_queue<int> pq[N];
void dfs(int u, int p){
for(auto [v,w] : adj[u]){
if(v == p) continue;
dfs(v,u);
pq[u].join(pq[v]);
}
if(u==1) return;
int l = 0, r = 0;
if(deg[u]){
while(--deg[u]){
pq[u].pop();
}
r = pq[u].top();
pq[u].pop();
l = pq[u].top();
pq[u].pop();
}
pq[u].push(l+to[u]);
pq[u].push(r+to[u]);
}
signed main(){
fastio
int n,m;
cin >> n >> m;
for(int i = 2;i <= n;i++){
int p,x;
cin >> p >> x;
to[i] = x;
adj[p].push_back({i,x});
adj[i].push_back({p,x});
deg[p]++;
ans += x;
}
for(int i = 1;i <= m;i++){
int p,x;
cin >> p >> x;
to[n+i] = x;
adj[p].push_back({i+n,x});
adj[i+n].push_back({p,x});
deg[p]++;
ans += x;
}
dfs(1,-1);
while(deg[1]--){
pq[1].pop();
}
while(!pq[1].empty()){
ans -= pq[1].top();
pq[1].pop();
}
cout << ans << "\n";
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
17 ms |
28492 KB |
Output is correct |
2 |
Correct |
16 ms |
28408 KB |
Output is correct |
3 |
Correct |
16 ms |
28492 KB |
Output is correct |
4 |
Correct |
15 ms |
28432 KB |
Output is correct |
5 |
Correct |
18 ms |
28492 KB |
Output is correct |
6 |
Correct |
16 ms |
28492 KB |
Output is correct |
7 |
Correct |
16 ms |
28492 KB |
Output is correct |
8 |
Correct |
17 ms |
28528 KB |
Output is correct |
9 |
Correct |
16 ms |
28528 KB |
Output is correct |
10 |
Correct |
18 ms |
28468 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
17 ms |
28480 KB |
Output is correct |
2 |
Correct |
15 ms |
28492 KB |
Output is correct |
3 |
Correct |
16 ms |
28492 KB |
Output is correct |
4 |
Correct |
19 ms |
28408 KB |
Output is correct |
5 |
Correct |
20 ms |
28464 KB |
Output is correct |
6 |
Correct |
21 ms |
28428 KB |
Output is correct |
7 |
Correct |
17 ms |
28492 KB |
Output is correct |
8 |
Correct |
20 ms |
28536 KB |
Output is correct |
9 |
Correct |
16 ms |
28484 KB |
Output is correct |
10 |
Correct |
16 ms |
28472 KB |
Output is correct |
11 |
Correct |
16 ms |
28492 KB |
Output is correct |
12 |
Correct |
17 ms |
28452 KB |
Output is correct |
13 |
Correct |
16 ms |
28436 KB |
Output is correct |
14 |
Correct |
16 ms |
28540 KB |
Output is correct |
15 |
Correct |
17 ms |
28532 KB |
Output is correct |
16 |
Correct |
16 ms |
28440 KB |
Output is correct |
17 |
Correct |
16 ms |
28492 KB |
Output is correct |
18 |
Correct |
17 ms |
28532 KB |
Output is correct |
19 |
Correct |
17 ms |
28428 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
17 ms |
28492 KB |
Output is correct |
2 |
Correct |
16 ms |
28408 KB |
Output is correct |
3 |
Correct |
16 ms |
28492 KB |
Output is correct |
4 |
Correct |
15 ms |
28432 KB |
Output is correct |
5 |
Correct |
18 ms |
28492 KB |
Output is correct |
6 |
Correct |
16 ms |
28492 KB |
Output is correct |
7 |
Correct |
16 ms |
28492 KB |
Output is correct |
8 |
Correct |
17 ms |
28528 KB |
Output is correct |
9 |
Correct |
16 ms |
28528 KB |
Output is correct |
10 |
Correct |
18 ms |
28468 KB |
Output is correct |
11 |
Correct |
17 ms |
28480 KB |
Output is correct |
12 |
Correct |
15 ms |
28492 KB |
Output is correct |
13 |
Correct |
16 ms |
28492 KB |
Output is correct |
14 |
Correct |
19 ms |
28408 KB |
Output is correct |
15 |
Correct |
20 ms |
28464 KB |
Output is correct |
16 |
Correct |
21 ms |
28428 KB |
Output is correct |
17 |
Correct |
17 ms |
28492 KB |
Output is correct |
18 |
Correct |
20 ms |
28536 KB |
Output is correct |
19 |
Correct |
16 ms |
28484 KB |
Output is correct |
20 |
Correct |
16 ms |
28472 KB |
Output is correct |
21 |
Correct |
16 ms |
28492 KB |
Output is correct |
22 |
Correct |
17 ms |
28452 KB |
Output is correct |
23 |
Correct |
16 ms |
28436 KB |
Output is correct |
24 |
Correct |
16 ms |
28540 KB |
Output is correct |
25 |
Correct |
17 ms |
28532 KB |
Output is correct |
26 |
Correct |
16 ms |
28440 KB |
Output is correct |
27 |
Correct |
16 ms |
28492 KB |
Output is correct |
28 |
Correct |
17 ms |
28532 KB |
Output is correct |
29 |
Correct |
17 ms |
28428 KB |
Output is correct |
30 |
Correct |
16 ms |
28516 KB |
Output is correct |
31 |
Correct |
27 ms |
28484 KB |
Output is correct |
32 |
Correct |
19 ms |
28620 KB |
Output is correct |
33 |
Correct |
17 ms |
28632 KB |
Output is correct |
34 |
Correct |
18 ms |
28748 KB |
Output is correct |
35 |
Correct |
19 ms |
28864 KB |
Output is correct |
36 |
Correct |
20 ms |
28896 KB |
Output is correct |
37 |
Correct |
20 ms |
28900 KB |
Output is correct |
38 |
Correct |
21 ms |
28892 KB |
Output is correct |
39 |
Correct |
19 ms |
28876 KB |
Output is correct |
40 |
Correct |
19 ms |
29284 KB |
Output is correct |
41 |
Correct |
19 ms |
29276 KB |
Output is correct |
42 |
Correct |
18 ms |
28904 KB |
Output is correct |
43 |
Correct |
23 ms |
29116 KB |
Output is correct |
44 |
Correct |
19 ms |
29088 KB |
Output is correct |
45 |
Correct |
20 ms |
29016 KB |
Output is correct |
46 |
Correct |
21 ms |
29132 KB |
Output is correct |
47 |
Correct |
18 ms |
29040 KB |
Output is correct |
48 |
Correct |
20 ms |
29004 KB |
Output is correct |
49 |
Correct |
19 ms |
29076 KB |
Output is correct |
50 |
Correct |
19 ms |
29008 KB |
Output is correct |
51 |
Correct |
24 ms |
29024 KB |
Output is correct |
52 |
Correct |
19 ms |
29004 KB |
Output is correct |
53 |
Correct |
18 ms |
29004 KB |
Output is correct |
54 |
Correct |
20 ms |
29264 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
17 ms |
28492 KB |
Output is correct |
2 |
Correct |
16 ms |
28408 KB |
Output is correct |
3 |
Correct |
16 ms |
28492 KB |
Output is correct |
4 |
Correct |
15 ms |
28432 KB |
Output is correct |
5 |
Correct |
18 ms |
28492 KB |
Output is correct |
6 |
Correct |
16 ms |
28492 KB |
Output is correct |
7 |
Correct |
16 ms |
28492 KB |
Output is correct |
8 |
Correct |
17 ms |
28528 KB |
Output is correct |
9 |
Correct |
16 ms |
28528 KB |
Output is correct |
10 |
Correct |
18 ms |
28468 KB |
Output is correct |
11 |
Correct |
17 ms |
28480 KB |
Output is correct |
12 |
Correct |
15 ms |
28492 KB |
Output is correct |
13 |
Correct |
16 ms |
28492 KB |
Output is correct |
14 |
Correct |
19 ms |
28408 KB |
Output is correct |
15 |
Correct |
20 ms |
28464 KB |
Output is correct |
16 |
Correct |
21 ms |
28428 KB |
Output is correct |
17 |
Correct |
17 ms |
28492 KB |
Output is correct |
18 |
Correct |
20 ms |
28536 KB |
Output is correct |
19 |
Correct |
16 ms |
28484 KB |
Output is correct |
20 |
Correct |
16 ms |
28472 KB |
Output is correct |
21 |
Correct |
16 ms |
28492 KB |
Output is correct |
22 |
Correct |
17 ms |
28452 KB |
Output is correct |
23 |
Correct |
16 ms |
28436 KB |
Output is correct |
24 |
Correct |
16 ms |
28540 KB |
Output is correct |
25 |
Correct |
17 ms |
28532 KB |
Output is correct |
26 |
Correct |
16 ms |
28440 KB |
Output is correct |
27 |
Correct |
16 ms |
28492 KB |
Output is correct |
28 |
Correct |
17 ms |
28532 KB |
Output is correct |
29 |
Correct |
17 ms |
28428 KB |
Output is correct |
30 |
Correct |
16 ms |
28516 KB |
Output is correct |
31 |
Correct |
27 ms |
28484 KB |
Output is correct |
32 |
Correct |
19 ms |
28620 KB |
Output is correct |
33 |
Correct |
17 ms |
28632 KB |
Output is correct |
34 |
Correct |
18 ms |
28748 KB |
Output is correct |
35 |
Correct |
19 ms |
28864 KB |
Output is correct |
36 |
Correct |
20 ms |
28896 KB |
Output is correct |
37 |
Correct |
20 ms |
28900 KB |
Output is correct |
38 |
Correct |
21 ms |
28892 KB |
Output is correct |
39 |
Correct |
19 ms |
28876 KB |
Output is correct |
40 |
Correct |
19 ms |
29284 KB |
Output is correct |
41 |
Correct |
19 ms |
29276 KB |
Output is correct |
42 |
Correct |
18 ms |
28904 KB |
Output is correct |
43 |
Correct |
23 ms |
29116 KB |
Output is correct |
44 |
Correct |
19 ms |
29088 KB |
Output is correct |
45 |
Correct |
20 ms |
29016 KB |
Output is correct |
46 |
Correct |
21 ms |
29132 KB |
Output is correct |
47 |
Correct |
18 ms |
29040 KB |
Output is correct |
48 |
Correct |
20 ms |
29004 KB |
Output is correct |
49 |
Correct |
19 ms |
29076 KB |
Output is correct |
50 |
Correct |
19 ms |
29008 KB |
Output is correct |
51 |
Correct |
24 ms |
29024 KB |
Output is correct |
52 |
Correct |
19 ms |
29004 KB |
Output is correct |
53 |
Correct |
18 ms |
29004 KB |
Output is correct |
54 |
Correct |
20 ms |
29264 KB |
Output is correct |
55 |
Correct |
24 ms |
29772 KB |
Output is correct |
56 |
Correct |
46 ms |
33220 KB |
Output is correct |
57 |
Correct |
84 ms |
36640 KB |
Output is correct |
58 |
Correct |
96 ms |
38780 KB |
Output is correct |
59 |
Correct |
139 ms |
42036 KB |
Output is correct |
60 |
Correct |
178 ms |
44868 KB |
Output is correct |
61 |
Correct |
199 ms |
46840 KB |
Output is correct |
62 |
Correct |
211 ms |
48528 KB |
Output is correct |
63 |
Correct |
282 ms |
51908 KB |
Output is correct |
64 |
Correct |
285 ms |
53144 KB |
Output is correct |
65 |
Correct |
146 ms |
72616 KB |
Output is correct |
66 |
Correct |
141 ms |
72680 KB |
Output is correct |
67 |
Correct |
179 ms |
49276 KB |
Output is correct |
68 |
Correct |
197 ms |
69236 KB |
Output is correct |
69 |
Correct |
263 ms |
67176 KB |
Output is correct |
70 |
Correct |
263 ms |
67236 KB |
Output is correct |
71 |
Correct |
263 ms |
62948 KB |
Output is correct |
72 |
Correct |
260 ms |
62916 KB |
Output is correct |
73 |
Correct |
273 ms |
62292 KB |
Output is correct |
74 |
Correct |
288 ms |
62348 KB |
Output is correct |
75 |
Correct |
300 ms |
61420 KB |
Output is correct |
76 |
Correct |
301 ms |
61624 KB |
Output is correct |
77 |
Correct |
332 ms |
60540 KB |
Output is correct |
78 |
Correct |
345 ms |
61628 KB |
Output is correct |
79 |
Correct |
355 ms |
75000 KB |
Output is correct |