#include <bits/stdc++.h>
#define ll long long
#define ld long double
#define sp ' '
#define en '\n'
#define smin(a, b) a = min(a, b)
#define smax(a, b) a = max(a, b)
using namespace std;
const int N = 3e5 + 2;
const ll linf = 9e18;
priority_queue<ll> pq[N];
int deg[N], par[N];
ll len[N], dp[N];
int main() {
ios::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
int n, m;
cin >> n >> m;
for (int i = 2; i <= n + m; i++) {
cin >> par[i] >> len[i];
deg[par[i]]++;
dp[par[i]] -= len[i];
if (i > n) {
pq[par[i]].push(len[i]);
pq[par[i]].push(len[i]);
}
}
for (int i = n; i > 1; i--) {
for (int j = 0; j < deg[i] - 1; j++) {
dp[i] += pq[i].top();
pq[i].pop();
}
pq[par[i]].push(pq[i].top() + len[i]);
pq[i].pop();
pq[par[i]].push(pq[i].top() + len[i]);
pq[i].pop();
dp[par[i]] += dp[i];
if (pq[par[i]].size() < pq[i].size()) swap(pq[par[i]], pq[i]);
while (pq[i].size()) pq[par[i]].push(pq[i].top()), pq[i].pop();
}
ll ans = 0;
for (int j = 0; j < deg[1]; j++) {
ans += pq[1].top();
pq[1].pop();
}
cout << dp[1] + ans << en;
return 0;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
5 ms |
9684 KB |
Output is correct |
2 |
Correct |
5 ms |
9684 KB |
Output is correct |
3 |
Correct |
5 ms |
9684 KB |
Output is correct |
4 |
Correct |
4 ms |
9684 KB |
Output is correct |
5 |
Correct |
5 ms |
9732 KB |
Output is correct |
6 |
Correct |
5 ms |
9720 KB |
Output is correct |
7 |
Correct |
5 ms |
9684 KB |
Output is correct |
8 |
Correct |
5 ms |
9712 KB |
Output is correct |
9 |
Correct |
5 ms |
9676 KB |
Output is correct |
10 |
Correct |
5 ms |
9684 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
6 ms |
9684 KB |
Output is correct |
2 |
Correct |
5 ms |
9644 KB |
Output is correct |
3 |
Correct |
7 ms |
9724 KB |
Output is correct |
4 |
Correct |
5 ms |
9684 KB |
Output is correct |
5 |
Correct |
5 ms |
9684 KB |
Output is correct |
6 |
Correct |
5 ms |
9724 KB |
Output is correct |
7 |
Correct |
5 ms |
9724 KB |
Output is correct |
8 |
Correct |
5 ms |
9684 KB |
Output is correct |
9 |
Correct |
5 ms |
9684 KB |
Output is correct |
10 |
Correct |
5 ms |
9688 KB |
Output is correct |
11 |
Correct |
4 ms |
9684 KB |
Output is correct |
12 |
Correct |
5 ms |
9684 KB |
Output is correct |
13 |
Correct |
5 ms |
9684 KB |
Output is correct |
14 |
Correct |
5 ms |
9716 KB |
Output is correct |
15 |
Correct |
6 ms |
9684 KB |
Output is correct |
16 |
Correct |
5 ms |
9720 KB |
Output is correct |
17 |
Correct |
5 ms |
9684 KB |
Output is correct |
18 |
Correct |
6 ms |
9684 KB |
Output is correct |
19 |
Correct |
5 ms |
9744 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
5 ms |
9684 KB |
Output is correct |
2 |
Correct |
5 ms |
9684 KB |
Output is correct |
3 |
Correct |
5 ms |
9684 KB |
Output is correct |
4 |
Correct |
4 ms |
9684 KB |
Output is correct |
5 |
Correct |
5 ms |
9732 KB |
Output is correct |
6 |
Correct |
5 ms |
9720 KB |
Output is correct |
7 |
Correct |
5 ms |
9684 KB |
Output is correct |
8 |
Correct |
5 ms |
9712 KB |
Output is correct |
9 |
Correct |
5 ms |
9676 KB |
Output is correct |
10 |
Correct |
5 ms |
9684 KB |
Output is correct |
11 |
Correct |
6 ms |
9684 KB |
Output is correct |
12 |
Correct |
5 ms |
9644 KB |
Output is correct |
13 |
Correct |
7 ms |
9724 KB |
Output is correct |
14 |
Correct |
5 ms |
9684 KB |
Output is correct |
15 |
Correct |
5 ms |
9684 KB |
Output is correct |
16 |
Correct |
5 ms |
9724 KB |
Output is correct |
17 |
Correct |
5 ms |
9724 KB |
Output is correct |
18 |
Correct |
5 ms |
9684 KB |
Output is correct |
19 |
Correct |
5 ms |
9684 KB |
Output is correct |
20 |
Correct |
5 ms |
9688 KB |
Output is correct |
21 |
Correct |
4 ms |
9684 KB |
Output is correct |
22 |
Correct |
5 ms |
9684 KB |
Output is correct |
23 |
Correct |
5 ms |
9684 KB |
Output is correct |
24 |
Correct |
5 ms |
9716 KB |
Output is correct |
25 |
Correct |
6 ms |
9684 KB |
Output is correct |
26 |
Correct |
5 ms |
9720 KB |
Output is correct |
27 |
Correct |
5 ms |
9684 KB |
Output is correct |
28 |
Correct |
6 ms |
9684 KB |
Output is correct |
29 |
Correct |
5 ms |
9744 KB |
Output is correct |
30 |
Correct |
5 ms |
9684 KB |
Output is correct |
31 |
Correct |
6 ms |
9752 KB |
Output is correct |
32 |
Correct |
6 ms |
9812 KB |
Output is correct |
33 |
Correct |
6 ms |
9812 KB |
Output is correct |
34 |
Correct |
8 ms |
9856 KB |
Output is correct |
35 |
Correct |
8 ms |
9940 KB |
Output is correct |
36 |
Correct |
8 ms |
9940 KB |
Output is correct |
37 |
Correct |
9 ms |
10004 KB |
Output is correct |
38 |
Correct |
7 ms |
10068 KB |
Output is correct |
39 |
Correct |
7 ms |
10068 KB |
Output is correct |
40 |
Correct |
6 ms |
9984 KB |
Output is correct |
41 |
Correct |
6 ms |
10068 KB |
Output is correct |
42 |
Correct |
8 ms |
9988 KB |
Output is correct |
43 |
Correct |
8 ms |
10036 KB |
Output is correct |
44 |
Correct |
8 ms |
9980 KB |
Output is correct |
45 |
Correct |
7 ms |
10080 KB |
Output is correct |
46 |
Correct |
7 ms |
10084 KB |
Output is correct |
47 |
Correct |
7 ms |
9988 KB |
Output is correct |
48 |
Correct |
6 ms |
9940 KB |
Output is correct |
49 |
Correct |
6 ms |
9940 KB |
Output is correct |
50 |
Correct |
6 ms |
9952 KB |
Output is correct |
51 |
Correct |
6 ms |
9940 KB |
Output is correct |
52 |
Correct |
7 ms |
10068 KB |
Output is correct |
53 |
Correct |
9 ms |
9940 KB |
Output is correct |
54 |
Correct |
7 ms |
9992 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
5 ms |
9684 KB |
Output is correct |
2 |
Correct |
5 ms |
9684 KB |
Output is correct |
3 |
Correct |
5 ms |
9684 KB |
Output is correct |
4 |
Correct |
4 ms |
9684 KB |
Output is correct |
5 |
Correct |
5 ms |
9732 KB |
Output is correct |
6 |
Correct |
5 ms |
9720 KB |
Output is correct |
7 |
Correct |
5 ms |
9684 KB |
Output is correct |
8 |
Correct |
5 ms |
9712 KB |
Output is correct |
9 |
Correct |
5 ms |
9676 KB |
Output is correct |
10 |
Correct |
5 ms |
9684 KB |
Output is correct |
11 |
Correct |
6 ms |
9684 KB |
Output is correct |
12 |
Correct |
5 ms |
9644 KB |
Output is correct |
13 |
Correct |
7 ms |
9724 KB |
Output is correct |
14 |
Correct |
5 ms |
9684 KB |
Output is correct |
15 |
Correct |
5 ms |
9684 KB |
Output is correct |
16 |
Correct |
5 ms |
9724 KB |
Output is correct |
17 |
Correct |
5 ms |
9724 KB |
Output is correct |
18 |
Correct |
5 ms |
9684 KB |
Output is correct |
19 |
Correct |
5 ms |
9684 KB |
Output is correct |
20 |
Correct |
5 ms |
9688 KB |
Output is correct |
21 |
Correct |
4 ms |
9684 KB |
Output is correct |
22 |
Correct |
5 ms |
9684 KB |
Output is correct |
23 |
Correct |
5 ms |
9684 KB |
Output is correct |
24 |
Correct |
5 ms |
9716 KB |
Output is correct |
25 |
Correct |
6 ms |
9684 KB |
Output is correct |
26 |
Correct |
5 ms |
9720 KB |
Output is correct |
27 |
Correct |
5 ms |
9684 KB |
Output is correct |
28 |
Correct |
6 ms |
9684 KB |
Output is correct |
29 |
Correct |
5 ms |
9744 KB |
Output is correct |
30 |
Correct |
5 ms |
9684 KB |
Output is correct |
31 |
Correct |
6 ms |
9752 KB |
Output is correct |
32 |
Correct |
6 ms |
9812 KB |
Output is correct |
33 |
Correct |
6 ms |
9812 KB |
Output is correct |
34 |
Correct |
8 ms |
9856 KB |
Output is correct |
35 |
Correct |
8 ms |
9940 KB |
Output is correct |
36 |
Correct |
8 ms |
9940 KB |
Output is correct |
37 |
Correct |
9 ms |
10004 KB |
Output is correct |
38 |
Correct |
7 ms |
10068 KB |
Output is correct |
39 |
Correct |
7 ms |
10068 KB |
Output is correct |
40 |
Correct |
6 ms |
9984 KB |
Output is correct |
41 |
Correct |
6 ms |
10068 KB |
Output is correct |
42 |
Correct |
8 ms |
9988 KB |
Output is correct |
43 |
Correct |
8 ms |
10036 KB |
Output is correct |
44 |
Correct |
8 ms |
9980 KB |
Output is correct |
45 |
Correct |
7 ms |
10080 KB |
Output is correct |
46 |
Correct |
7 ms |
10084 KB |
Output is correct |
47 |
Correct |
7 ms |
9988 KB |
Output is correct |
48 |
Correct |
6 ms |
9940 KB |
Output is correct |
49 |
Correct |
6 ms |
9940 KB |
Output is correct |
50 |
Correct |
6 ms |
9952 KB |
Output is correct |
51 |
Correct |
6 ms |
9940 KB |
Output is correct |
52 |
Correct |
7 ms |
10068 KB |
Output is correct |
53 |
Correct |
9 ms |
9940 KB |
Output is correct |
54 |
Correct |
7 ms |
9992 KB |
Output is correct |
55 |
Correct |
12 ms |
10720 KB |
Output is correct |
56 |
Correct |
25 ms |
13740 KB |
Output is correct |
57 |
Correct |
42 ms |
16496 KB |
Output is correct |
58 |
Correct |
58 ms |
18428 KB |
Output is correct |
59 |
Correct |
79 ms |
21340 KB |
Output is correct |
60 |
Correct |
107 ms |
23904 KB |
Output is correct |
61 |
Correct |
103 ms |
26392 KB |
Output is correct |
62 |
Correct |
119 ms |
27856 KB |
Output is correct |
63 |
Correct |
135 ms |
31448 KB |
Output is correct |
64 |
Correct |
143 ms |
32388 KB |
Output is correct |
65 |
Correct |
88 ms |
30952 KB |
Output is correct |
66 |
Correct |
109 ms |
31012 KB |
Output is correct |
67 |
Correct |
86 ms |
30912 KB |
Output is correct |
68 |
Correct |
143 ms |
28508 KB |
Output is correct |
69 |
Correct |
140 ms |
29408 KB |
Output is correct |
70 |
Correct |
148 ms |
29336 KB |
Output is correct |
71 |
Correct |
125 ms |
31028 KB |
Output is correct |
72 |
Correct |
129 ms |
31176 KB |
Output is correct |
73 |
Correct |
107 ms |
27532 KB |
Output is correct |
74 |
Correct |
141 ms |
28068 KB |
Output is correct |
75 |
Correct |
109 ms |
26584 KB |
Output is correct |
76 |
Correct |
111 ms |
26580 KB |
Output is correct |
77 |
Correct |
146 ms |
26828 KB |
Output is correct |
78 |
Correct |
121 ms |
26476 KB |
Output is correct |
79 |
Correct |
104 ms |
24352 KB |
Output is correct |