#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 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 |
14 ms |
21504 KB |
Output is correct |
3 |
Correct |
14 ms |
21504 KB |
Output is correct |
4 |
Correct |
14 ms |
21504 KB |
Output is correct |
5 |
Correct |
14 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 |
15 ms |
21632 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
18 ms |
21504 KB |
Output is correct |
2 |
Correct |
18 ms |
21504 KB |
Output is correct |
3 |
Correct |
18 ms |
21504 KB |
Output is correct |
4 |
Correct |
14 ms |
21436 KB |
Output is correct |
5 |
Correct |
17 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 |
17 ms |
21504 KB |
Output is correct |
9 |
Correct |
18 ms |
21504 KB |
Output is correct |
10 |
Correct |
17 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 |
19 ms |
21504 KB |
Output is correct |
14 |
Correct |
18 ms |
21504 KB |
Output is correct |
15 |
Correct |
14 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 |
15 ms |
21504 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
15 ms |
21504 KB |
Output is correct |
2 |
Correct |
14 ms |
21504 KB |
Output is correct |
3 |
Correct |
14 ms |
21504 KB |
Output is correct |
4 |
Correct |
14 ms |
21504 KB |
Output is correct |
5 |
Correct |
14 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 |
15 ms |
21632 KB |
Output is correct |
11 |
Correct |
18 ms |
21504 KB |
Output is correct |
12 |
Correct |
18 ms |
21504 KB |
Output is correct |
13 |
Correct |
18 ms |
21504 KB |
Output is correct |
14 |
Correct |
14 ms |
21436 KB |
Output is correct |
15 |
Correct |
17 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 |
17 ms |
21504 KB |
Output is correct |
19 |
Correct |
18 ms |
21504 KB |
Output is correct |
20 |
Correct |
17 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 |
19 ms |
21504 KB |
Output is correct |
24 |
Correct |
18 ms |
21504 KB |
Output is correct |
25 |
Correct |
14 ms |
21504 KB |
Output is correct |
26 |
Correct |
14 ms |
21504 KB |
Output is correct |
27 |
Correct |
14 ms |
21504 KB |
Output is correct |
28 |
Correct |
14 ms |
21504 KB |
Output is correct |
29 |
Correct |
15 ms |
21504 KB |
Output is correct |
30 |
Correct |
14 ms |
21504 KB |
Output is correct |
31 |
Correct |
20 ms |
21504 KB |
Output is correct |
32 |
Correct |
15 ms |
21504 KB |
Output is correct |
33 |
Correct |
16 ms |
21632 KB |
Output is correct |
34 |
Correct |
16 ms |
21632 KB |
Output is correct |
35 |
Correct |
16 ms |
21632 KB |
Output is correct |
36 |
Correct |
17 ms |
21632 KB |
Output is correct |
37 |
Correct |
17 ms |
21632 KB |
Output is correct |
38 |
Correct |
17 ms |
21632 KB |
Output is correct |
39 |
Correct |
21 ms |
21632 KB |
Output is correct |
40 |
Correct |
16 ms |
22304 KB |
Output is correct |
41 |
Correct |
16 ms |
22272 KB |
Output is correct |
42 |
Correct |
16 ms |
21632 KB |
Output is correct |
43 |
Correct |
17 ms |
22016 KB |
Output is correct |
44 |
Correct |
17 ms |
21888 KB |
Output is correct |
45 |
Correct |
20 ms |
21888 KB |
Output is correct |
46 |
Correct |
16 ms |
21760 KB |
Output is correct |
47 |
Correct |
17 ms |
21760 KB |
Output is correct |
48 |
Correct |
19 ms |
21632 KB |
Output is correct |
49 |
Correct |
16 ms |
21632 KB |
Output is correct |
50 |
Correct |
16 ms |
21664 KB |
Output is correct |
51 |
Correct |
16 ms |
21632 KB |
Output is correct |
52 |
Correct |
17 ms |
21632 KB |
Output is correct |
53 |
Correct |
16 ms |
21632 KB |
Output is correct |
54 |
Correct |
16 ms |
21664 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
15 ms |
21504 KB |
Output is correct |
2 |
Correct |
14 ms |
21504 KB |
Output is correct |
3 |
Correct |
14 ms |
21504 KB |
Output is correct |
4 |
Correct |
14 ms |
21504 KB |
Output is correct |
5 |
Correct |
14 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 |
15 ms |
21632 KB |
Output is correct |
11 |
Correct |
18 ms |
21504 KB |
Output is correct |
12 |
Correct |
18 ms |
21504 KB |
Output is correct |
13 |
Correct |
18 ms |
21504 KB |
Output is correct |
14 |
Correct |
14 ms |
21436 KB |
Output is correct |
15 |
Correct |
17 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 |
17 ms |
21504 KB |
Output is correct |
19 |
Correct |
18 ms |
21504 KB |
Output is correct |
20 |
Correct |
17 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 |
19 ms |
21504 KB |
Output is correct |
24 |
Correct |
18 ms |
21504 KB |
Output is correct |
25 |
Correct |
14 ms |
21504 KB |
Output is correct |
26 |
Correct |
14 ms |
21504 KB |
Output is correct |
27 |
Correct |
14 ms |
21504 KB |
Output is correct |
28 |
Correct |
14 ms |
21504 KB |
Output is correct |
29 |
Correct |
15 ms |
21504 KB |
Output is correct |
30 |
Correct |
14 ms |
21504 KB |
Output is correct |
31 |
Correct |
20 ms |
21504 KB |
Output is correct |
32 |
Correct |
15 ms |
21504 KB |
Output is correct |
33 |
Correct |
16 ms |
21632 KB |
Output is correct |
34 |
Correct |
16 ms |
21632 KB |
Output is correct |
35 |
Correct |
16 ms |
21632 KB |
Output is correct |
36 |
Correct |
17 ms |
21632 KB |
Output is correct |
37 |
Correct |
17 ms |
21632 KB |
Output is correct |
38 |
Correct |
17 ms |
21632 KB |
Output is correct |
39 |
Correct |
21 ms |
21632 KB |
Output is correct |
40 |
Correct |
16 ms |
22304 KB |
Output is correct |
41 |
Correct |
16 ms |
22272 KB |
Output is correct |
42 |
Correct |
16 ms |
21632 KB |
Output is correct |
43 |
Correct |
17 ms |
22016 KB |
Output is correct |
44 |
Correct |
17 ms |
21888 KB |
Output is correct |
45 |
Correct |
20 ms |
21888 KB |
Output is correct |
46 |
Correct |
16 ms |
21760 KB |
Output is correct |
47 |
Correct |
17 ms |
21760 KB |
Output is correct |
48 |
Correct |
19 ms |
21632 KB |
Output is correct |
49 |
Correct |
16 ms |
21632 KB |
Output is correct |
50 |
Correct |
16 ms |
21664 KB |
Output is correct |
51 |
Correct |
16 ms |
21632 KB |
Output is correct |
52 |
Correct |
17 ms |
21632 KB |
Output is correct |
53 |
Correct |
16 ms |
21632 KB |
Output is correct |
54 |
Correct |
16 ms |
21664 KB |
Output is correct |
55 |
Correct |
21 ms |
22016 KB |
Output is correct |
56 |
Correct |
43 ms |
23548 KB |
Output is correct |
57 |
Correct |
69 ms |
24948 KB |
Output is correct |
58 |
Correct |
93 ms |
26096 KB |
Output is correct |
59 |
Correct |
122 ms |
27504 KB |
Output is correct |
60 |
Correct |
154 ms |
28912 KB |
Output is correct |
61 |
Correct |
182 ms |
30452 KB |
Output is correct |
62 |
Correct |
191 ms |
31344 KB |
Output is correct |
63 |
Correct |
242 ms |
32876 KB |
Output is correct |
64 |
Correct |
260 ms |
33420 KB |
Output is correct |
65 |
Correct |
147 ms |
73080 KB |
Output is correct |
66 |
Correct |
158 ms |
73084 KB |
Output is correct |
67 |
Correct |
142 ms |
31096 KB |
Output is correct |
68 |
Correct |
172 ms |
49512 KB |
Output is correct |
69 |
Correct |
204 ms |
46824 KB |
Output is correct |
70 |
Correct |
209 ms |
46960 KB |
Output is correct |
71 |
Correct |
203 ms |
37352 KB |
Output is correct |
72 |
Correct |
178 ms |
37608 KB |
Output is correct |
73 |
Correct |
146 ms |
34276 KB |
Output is correct |
74 |
Correct |
143 ms |
34276 KB |
Output is correct |
75 |
Correct |
150 ms |
33236 KB |
Output is correct |
76 |
Correct |
149 ms |
33256 KB |
Output is correct |
77 |
Correct |
165 ms |
33064 KB |
Output is correct |
78 |
Correct |
172 ms |
31072 KB |
Output is correct |
79 |
Correct |
146 ms |
32224 KB |
Output is correct |