#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define ld long double
#define pb push_back
#define all(x) begin(x), end(x)
#define SZ(x) (int)(x).size()
mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
const int n0 = 300003;
int n, m;
vector <array <int, 2>> g[n0];
struct Fn {
int k = 0;
ll b = 0;
priority_queue <ll> slp;
void merge(Fn & f) {
while (!f.slp.empty()) {
slp.push(f.slp.top());
f.slp.pop();
}
k += f.k;
b += f.b;
}
void minimize() {
while (k > 0) {
k--;
b += slp.top();
slp.pop();
}
}
void add(int w) {
while (k > 1) {
k--;
b += slp.top();
slp.pop();
}
if (k < 0) return;
ll x0 = slp.top() + w;
slp.pop();
if (k == 1) {
ll x1 = slp.top() + w;
slp.pop();
slp.push(x1);
}
slp.push(x0);
b -= w;
}
};
Fn f[n0];
void swap(Fn & x, Fn & y) {
swap(x.k, y.k), swap(x.b, y.b), x.slp.swap(y.slp);
}
void merge(int x, int y) {
if (SZ(f[x].slp) < SZ(f[y].slp)) swap(f[x], f[y]);
f[x].merge(f[y]);
}
Fn cur;
void dfs(int v, int p) {
for (auto i : g[v]) {
int to = i[0], w = i[1];
if (to == p) continue;
dfs(to, v);
if (SZ(g[to]) == 0) {
while (!cur.slp.empty()) cur.slp.pop();
cur.slp.push(w), cur.slp.push(w);
cur.k = 1, cur.b = -w;
f[v].merge(cur);
} else {
f[to].add(w);
merge(v, to);
}
}
}
int main() {
ios_base::sync_with_stdio(false), cin.tie(NULL);
cin >> n >> m;
for (int i = 2; i <= n + m; i++) {
int x, w;
cin >> x >> w;
g[x].pb({i, w});
}
dfs(1, 1);
f[1].minimize();
cout << f[1].b;
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
15 ms |
21504 KB |
Output is correct |
2 |
Correct |
16 ms |
21504 KB |
Output is correct |
3 |
Correct |
15 ms |
21504 KB |
Output is correct |
4 |
Correct |
17 ms |
21504 KB |
Output is correct |
5 |
Correct |
14 ms |
21504 KB |
Output is correct |
6 |
Correct |
15 ms |
21504 KB |
Output is correct |
7 |
Correct |
16 ms |
21632 KB |
Output is correct |
8 |
Correct |
16 ms |
21504 KB |
Output is correct |
9 |
Correct |
15 ms |
21504 KB |
Output is correct |
10 |
Correct |
15 ms |
21504 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
14 ms |
21504 KB |
Output is correct |
2 |
Correct |
20 ms |
21504 KB |
Output is correct |
3 |
Correct |
14 ms |
21504 KB |
Output is correct |
4 |
Correct |
15 ms |
21504 KB |
Output is correct |
5 |
Correct |
15 ms |
21504 KB |
Output is correct |
6 |
Correct |
14 ms |
21504 KB |
Output is correct |
7 |
Correct |
14 ms |
21504 KB |
Output is correct |
8 |
Correct |
14 ms |
21504 KB |
Output is correct |
9 |
Correct |
14 ms |
21504 KB |
Output is correct |
10 |
Correct |
14 ms |
21504 KB |
Output is correct |
11 |
Correct |
14 ms |
21504 KB |
Output is correct |
12 |
Correct |
15 ms |
21504 KB |
Output is correct |
13 |
Correct |
15 ms |
21504 KB |
Output is correct |
14 |
Correct |
14 ms |
21504 KB |
Output is correct |
15 |
Correct |
15 ms |
21504 KB |
Output is correct |
16 |
Correct |
15 ms |
21504 KB |
Output is correct |
17 |
Correct |
15 ms |
21504 KB |
Output is correct |
18 |
Correct |
15 ms |
21504 KB |
Output is correct |
19 |
Correct |
15 ms |
21504 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
15 ms |
21504 KB |
Output is correct |
2 |
Correct |
16 ms |
21504 KB |
Output is correct |
3 |
Correct |
15 ms |
21504 KB |
Output is correct |
4 |
Correct |
17 ms |
21504 KB |
Output is correct |
5 |
Correct |
14 ms |
21504 KB |
Output is correct |
6 |
Correct |
15 ms |
21504 KB |
Output is correct |
7 |
Correct |
16 ms |
21632 KB |
Output is correct |
8 |
Correct |
16 ms |
21504 KB |
Output is correct |
9 |
Correct |
15 ms |
21504 KB |
Output is correct |
10 |
Correct |
15 ms |
21504 KB |
Output is correct |
11 |
Correct |
14 ms |
21504 KB |
Output is correct |
12 |
Correct |
20 ms |
21504 KB |
Output is correct |
13 |
Correct |
14 ms |
21504 KB |
Output is correct |
14 |
Correct |
15 ms |
21504 KB |
Output is correct |
15 |
Correct |
15 ms |
21504 KB |
Output is correct |
16 |
Correct |
14 ms |
21504 KB |
Output is correct |
17 |
Correct |
14 ms |
21504 KB |
Output is correct |
18 |
Correct |
14 ms |
21504 KB |
Output is correct |
19 |
Correct |
14 ms |
21504 KB |
Output is correct |
20 |
Correct |
14 ms |
21504 KB |
Output is correct |
21 |
Correct |
14 ms |
21504 KB |
Output is correct |
22 |
Correct |
15 ms |
21504 KB |
Output is correct |
23 |
Correct |
15 ms |
21504 KB |
Output is correct |
24 |
Correct |
14 ms |
21504 KB |
Output is correct |
25 |
Correct |
15 ms |
21504 KB |
Output is correct |
26 |
Correct |
15 ms |
21504 KB |
Output is correct |
27 |
Correct |
15 ms |
21504 KB |
Output is correct |
28 |
Correct |
15 ms |
21504 KB |
Output is correct |
29 |
Correct |
15 ms |
21504 KB |
Output is correct |
30 |
Correct |
15 ms |
21504 KB |
Output is correct |
31 |
Correct |
16 ms |
21504 KB |
Output is correct |
32 |
Correct |
16 ms |
21632 KB |
Output is correct |
33 |
Correct |
16 ms |
21632 KB |
Output is correct |
34 |
Correct |
20 ms |
21632 KB |
Output is correct |
35 |
Correct |
18 ms |
21632 KB |
Output is correct |
36 |
Correct |
17 ms |
21760 KB |
Output is correct |
37 |
Correct |
21 ms |
21760 KB |
Output is correct |
38 |
Correct |
21 ms |
21760 KB |
Output is correct |
39 |
Correct |
22 ms |
21760 KB |
Output is correct |
40 |
Correct |
20 ms |
22400 KB |
Output is correct |
41 |
Correct |
18 ms |
22400 KB |
Output is correct |
42 |
Correct |
16 ms |
21760 KB |
Output is correct |
43 |
Correct |
17 ms |
22016 KB |
Output is correct |
44 |
Correct |
17 ms |
21888 KB |
Output is correct |
45 |
Correct |
17 ms |
21888 KB |
Output is correct |
46 |
Correct |
17 ms |
21760 KB |
Output is correct |
47 |
Correct |
17 ms |
21760 KB |
Output is correct |
48 |
Correct |
16 ms |
21760 KB |
Output is correct |
49 |
Correct |
17 ms |
21760 KB |
Output is correct |
50 |
Correct |
16 ms |
21632 KB |
Output is correct |
51 |
Correct |
16 ms |
21632 KB |
Output is correct |
52 |
Correct |
16 ms |
21760 KB |
Output is correct |
53 |
Correct |
16 ms |
21760 KB |
Output is correct |
54 |
Correct |
16 ms |
21760 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
15 ms |
21504 KB |
Output is correct |
2 |
Correct |
16 ms |
21504 KB |
Output is correct |
3 |
Correct |
15 ms |
21504 KB |
Output is correct |
4 |
Correct |
17 ms |
21504 KB |
Output is correct |
5 |
Correct |
14 ms |
21504 KB |
Output is correct |
6 |
Correct |
15 ms |
21504 KB |
Output is correct |
7 |
Correct |
16 ms |
21632 KB |
Output is correct |
8 |
Correct |
16 ms |
21504 KB |
Output is correct |
9 |
Correct |
15 ms |
21504 KB |
Output is correct |
10 |
Correct |
15 ms |
21504 KB |
Output is correct |
11 |
Correct |
14 ms |
21504 KB |
Output is correct |
12 |
Correct |
20 ms |
21504 KB |
Output is correct |
13 |
Correct |
14 ms |
21504 KB |
Output is correct |
14 |
Correct |
15 ms |
21504 KB |
Output is correct |
15 |
Correct |
15 ms |
21504 KB |
Output is correct |
16 |
Correct |
14 ms |
21504 KB |
Output is correct |
17 |
Correct |
14 ms |
21504 KB |
Output is correct |
18 |
Correct |
14 ms |
21504 KB |
Output is correct |
19 |
Correct |
14 ms |
21504 KB |
Output is correct |
20 |
Correct |
14 ms |
21504 KB |
Output is correct |
21 |
Correct |
14 ms |
21504 KB |
Output is correct |
22 |
Correct |
15 ms |
21504 KB |
Output is correct |
23 |
Correct |
15 ms |
21504 KB |
Output is correct |
24 |
Correct |
14 ms |
21504 KB |
Output is correct |
25 |
Correct |
15 ms |
21504 KB |
Output is correct |
26 |
Correct |
15 ms |
21504 KB |
Output is correct |
27 |
Correct |
15 ms |
21504 KB |
Output is correct |
28 |
Correct |
15 ms |
21504 KB |
Output is correct |
29 |
Correct |
15 ms |
21504 KB |
Output is correct |
30 |
Correct |
15 ms |
21504 KB |
Output is correct |
31 |
Correct |
16 ms |
21504 KB |
Output is correct |
32 |
Correct |
16 ms |
21632 KB |
Output is correct |
33 |
Correct |
16 ms |
21632 KB |
Output is correct |
34 |
Correct |
20 ms |
21632 KB |
Output is correct |
35 |
Correct |
18 ms |
21632 KB |
Output is correct |
36 |
Correct |
17 ms |
21760 KB |
Output is correct |
37 |
Correct |
21 ms |
21760 KB |
Output is correct |
38 |
Correct |
21 ms |
21760 KB |
Output is correct |
39 |
Correct |
22 ms |
21760 KB |
Output is correct |
40 |
Correct |
20 ms |
22400 KB |
Output is correct |
41 |
Correct |
18 ms |
22400 KB |
Output is correct |
42 |
Correct |
16 ms |
21760 KB |
Output is correct |
43 |
Correct |
17 ms |
22016 KB |
Output is correct |
44 |
Correct |
17 ms |
21888 KB |
Output is correct |
45 |
Correct |
17 ms |
21888 KB |
Output is correct |
46 |
Correct |
17 ms |
21760 KB |
Output is correct |
47 |
Correct |
17 ms |
21760 KB |
Output is correct |
48 |
Correct |
16 ms |
21760 KB |
Output is correct |
49 |
Correct |
17 ms |
21760 KB |
Output is correct |
50 |
Correct |
16 ms |
21632 KB |
Output is correct |
51 |
Correct |
16 ms |
21632 KB |
Output is correct |
52 |
Correct |
16 ms |
21760 KB |
Output is correct |
53 |
Correct |
16 ms |
21760 KB |
Output is correct |
54 |
Correct |
16 ms |
21760 KB |
Output is correct |
55 |
Correct |
22 ms |
22264 KB |
Output is correct |
56 |
Correct |
44 ms |
24320 KB |
Output is correct |
57 |
Correct |
74 ms |
26356 KB |
Output is correct |
58 |
Correct |
119 ms |
27704 KB |
Output is correct |
59 |
Correct |
127 ms |
29676 KB |
Output is correct |
60 |
Correct |
156 ms |
31728 KB |
Output is correct |
61 |
Correct |
233 ms |
33652 KB |
Output is correct |
62 |
Correct |
254 ms |
34928 KB |
Output is correct |
63 |
Correct |
314 ms |
37116 KB |
Output is correct |
64 |
Correct |
364 ms |
37740 KB |
Output is correct |
65 |
Correct |
162 ms |
78192 KB |
Output is correct |
66 |
Correct |
161 ms |
77944 KB |
Output is correct |
67 |
Correct |
147 ms |
35832 KB |
Output is correct |
68 |
Correct |
172 ms |
54248 KB |
Output is correct |
69 |
Correct |
205 ms |
51552 KB |
Output is correct |
70 |
Correct |
248 ms |
51560 KB |
Output is correct |
71 |
Correct |
226 ms |
41704 KB |
Output is correct |
72 |
Correct |
216 ms |
41928 KB |
Output is correct |
73 |
Correct |
201 ms |
38244 KB |
Output is correct |
74 |
Correct |
171 ms |
38300 KB |
Output is correct |
75 |
Correct |
155 ms |
37092 KB |
Output is correct |
76 |
Correct |
168 ms |
37092 KB |
Output is correct |
77 |
Correct |
174 ms |
37160 KB |
Output is correct |
78 |
Correct |
172 ms |
35140 KB |
Output is correct |
79 |
Correct |
145 ms |
35684 KB |
Output is correct |