#include<bits/stdc++.h>
#define maxn 300005
using namespace std;
typedef long long ll;
int n, m;
vector < pair < ll, ll > > g[maxn];
struct Set{
priority_queue < ll > q;
ll sum, cnt;
Set(ll _sum = 0, ll _cnt = 0) : sum(_sum), cnt(_cnt) {};
void add(ll c) {
ll a = q.top(); q.pop();
ll b = q.top(); q.pop();
a += c;
b += c;
q.push(a);
q.push(b);
sum -= c;
}
void shift(){
while (cnt > 1) {
ll ret = q.top(); q.pop();
cnt --;
sum += ret;
}
}
} tem[maxn];
Set* a[maxn];
Set* mix(Set* a, Set* b) {
if ((a -> q).size() < (b -> q).size())
swap(a, b);
a -> cnt += b -> cnt;
a -> sum += b -> sum;
while (!(b -> q).empty()) {
ll ret = (b -> q).top(); (b -> q).pop();
(a -> q).push(ret);
}
return a;
}
void dfs(int u) {
if (g[u].size() == 0) {
(a[u] -> q).push(0);
(a[u] -> q).push(0);
a[u] -> cnt = 1;
}
for (auto i : g[u]) {
int v = i.first;
ll c = i.second;
dfs(v);
a[v] -> add(c);
a[u] = mix(a[u], a[v]);
}
a[u] -> shift();
}
int main(){
#define TASK "ABC"
// freopen(TASK".inp", "r", stdin); freopen(TASK".out", "w", stdout);
ios_base::sync_with_stdio(0);
cin >> n >> m;
for (int i = 2; i <= n + m; ++i) {
int p, c;
cin >> p >> c;
g[p].push_back({i, c});
}
for (int i = 1; i <= n + m; ++i)
a[i] = &tem[i];
dfs(1);
cout << (a[1] -> sum + (a[1] -> q).top());
return 0;
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
17 ms |
21504 KB |
Output is correct |
2 |
Correct |
16 ms |
21504 KB |
Output is correct |
3 |
Correct |
16 ms |
21504 KB |
Output is correct |
4 |
Correct |
17 ms |
21504 KB |
Output is correct |
5 |
Correct |
17 ms |
21504 KB |
Output is correct |
6 |
Correct |
16 ms |
21504 KB |
Output is correct |
7 |
Correct |
17 ms |
21504 KB |
Output is correct |
8 |
Correct |
17 ms |
21504 KB |
Output is correct |
9 |
Correct |
16 ms |
21504 KB |
Output is correct |
10 |
Correct |
17 ms |
21504 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
16 ms |
21504 KB |
Output is correct |
2 |
Correct |
16 ms |
21504 KB |
Output is correct |
3 |
Correct |
17 ms |
21504 KB |
Output is correct |
4 |
Correct |
17 ms |
21504 KB |
Output is correct |
5 |
Correct |
18 ms |
21504 KB |
Output is correct |
6 |
Correct |
17 ms |
21504 KB |
Output is correct |
7 |
Correct |
17 ms |
21504 KB |
Output is correct |
8 |
Correct |
17 ms |
21504 KB |
Output is correct |
9 |
Correct |
17 ms |
21504 KB |
Output is correct |
10 |
Correct |
16 ms |
21504 KB |
Output is correct |
11 |
Correct |
16 ms |
21504 KB |
Output is correct |
12 |
Correct |
17 ms |
21504 KB |
Output is correct |
13 |
Correct |
19 ms |
21504 KB |
Output is correct |
14 |
Correct |
17 ms |
21504 KB |
Output is correct |
15 |
Correct |
16 ms |
21504 KB |
Output is correct |
16 |
Correct |
18 ms |
21632 KB |
Output is correct |
17 |
Correct |
18 ms |
21504 KB |
Output is correct |
18 |
Correct |
17 ms |
21504 KB |
Output is correct |
19 |
Correct |
18 ms |
21504 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
17 ms |
21504 KB |
Output is correct |
2 |
Correct |
16 ms |
21504 KB |
Output is correct |
3 |
Correct |
16 ms |
21504 KB |
Output is correct |
4 |
Correct |
17 ms |
21504 KB |
Output is correct |
5 |
Correct |
17 ms |
21504 KB |
Output is correct |
6 |
Correct |
16 ms |
21504 KB |
Output is correct |
7 |
Correct |
17 ms |
21504 KB |
Output is correct |
8 |
Correct |
17 ms |
21504 KB |
Output is correct |
9 |
Correct |
16 ms |
21504 KB |
Output is correct |
10 |
Correct |
17 ms |
21504 KB |
Output is correct |
11 |
Correct |
16 ms |
21504 KB |
Output is correct |
12 |
Correct |
16 ms |
21504 KB |
Output is correct |
13 |
Correct |
17 ms |
21504 KB |
Output is correct |
14 |
Correct |
17 ms |
21504 KB |
Output is correct |
15 |
Correct |
18 ms |
21504 KB |
Output is correct |
16 |
Correct |
17 ms |
21504 KB |
Output is correct |
17 |
Correct |
17 ms |
21504 KB |
Output is correct |
18 |
Correct |
17 ms |
21504 KB |
Output is correct |
19 |
Correct |
17 ms |
21504 KB |
Output is correct |
20 |
Correct |
16 ms |
21504 KB |
Output is correct |
21 |
Correct |
16 ms |
21504 KB |
Output is correct |
22 |
Correct |
17 ms |
21504 KB |
Output is correct |
23 |
Correct |
19 ms |
21504 KB |
Output is correct |
24 |
Correct |
17 ms |
21504 KB |
Output is correct |
25 |
Correct |
16 ms |
21504 KB |
Output is correct |
26 |
Correct |
18 ms |
21632 KB |
Output is correct |
27 |
Correct |
18 ms |
21504 KB |
Output is correct |
28 |
Correct |
17 ms |
21504 KB |
Output is correct |
29 |
Correct |
18 ms |
21504 KB |
Output is correct |
30 |
Correct |
16 ms |
21504 KB |
Output is correct |
31 |
Correct |
17 ms |
21504 KB |
Output is correct |
32 |
Correct |
17 ms |
21632 KB |
Output is correct |
33 |
Correct |
18 ms |
21632 KB |
Output is correct |
34 |
Correct |
18 ms |
21632 KB |
Output is correct |
35 |
Correct |
18 ms |
21760 KB |
Output is correct |
36 |
Correct |
21 ms |
21760 KB |
Output is correct |
37 |
Correct |
19 ms |
21888 KB |
Output is correct |
38 |
Correct |
19 ms |
21888 KB |
Output is correct |
39 |
Correct |
19 ms |
21888 KB |
Output is correct |
40 |
Correct |
20 ms |
22272 KB |
Output is correct |
41 |
Correct |
19 ms |
22144 KB |
Output is correct |
42 |
Correct |
18 ms |
21760 KB |
Output is correct |
43 |
Correct |
19 ms |
22016 KB |
Output is correct |
44 |
Correct |
20 ms |
22016 KB |
Output is correct |
45 |
Correct |
19 ms |
22016 KB |
Output is correct |
46 |
Correct |
20 ms |
22016 KB |
Output is correct |
47 |
Correct |
24 ms |
22016 KB |
Output is correct |
48 |
Correct |
24 ms |
21888 KB |
Output is correct |
49 |
Correct |
22 ms |
21888 KB |
Output is correct |
50 |
Correct |
21 ms |
21888 KB |
Output is correct |
51 |
Correct |
19 ms |
21760 KB |
Output is correct |
52 |
Correct |
20 ms |
21888 KB |
Output is correct |
53 |
Correct |
20 ms |
22016 KB |
Output is correct |
54 |
Correct |
22 ms |
21888 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
17 ms |
21504 KB |
Output is correct |
2 |
Correct |
16 ms |
21504 KB |
Output is correct |
3 |
Correct |
16 ms |
21504 KB |
Output is correct |
4 |
Correct |
17 ms |
21504 KB |
Output is correct |
5 |
Correct |
17 ms |
21504 KB |
Output is correct |
6 |
Correct |
16 ms |
21504 KB |
Output is correct |
7 |
Correct |
17 ms |
21504 KB |
Output is correct |
8 |
Correct |
17 ms |
21504 KB |
Output is correct |
9 |
Correct |
16 ms |
21504 KB |
Output is correct |
10 |
Correct |
17 ms |
21504 KB |
Output is correct |
11 |
Correct |
16 ms |
21504 KB |
Output is correct |
12 |
Correct |
16 ms |
21504 KB |
Output is correct |
13 |
Correct |
17 ms |
21504 KB |
Output is correct |
14 |
Correct |
17 ms |
21504 KB |
Output is correct |
15 |
Correct |
18 ms |
21504 KB |
Output is correct |
16 |
Correct |
17 ms |
21504 KB |
Output is correct |
17 |
Correct |
17 ms |
21504 KB |
Output is correct |
18 |
Correct |
17 ms |
21504 KB |
Output is correct |
19 |
Correct |
17 ms |
21504 KB |
Output is correct |
20 |
Correct |
16 ms |
21504 KB |
Output is correct |
21 |
Correct |
16 ms |
21504 KB |
Output is correct |
22 |
Correct |
17 ms |
21504 KB |
Output is correct |
23 |
Correct |
19 ms |
21504 KB |
Output is correct |
24 |
Correct |
17 ms |
21504 KB |
Output is correct |
25 |
Correct |
16 ms |
21504 KB |
Output is correct |
26 |
Correct |
18 ms |
21632 KB |
Output is correct |
27 |
Correct |
18 ms |
21504 KB |
Output is correct |
28 |
Correct |
17 ms |
21504 KB |
Output is correct |
29 |
Correct |
18 ms |
21504 KB |
Output is correct |
30 |
Correct |
16 ms |
21504 KB |
Output is correct |
31 |
Correct |
17 ms |
21504 KB |
Output is correct |
32 |
Correct |
17 ms |
21632 KB |
Output is correct |
33 |
Correct |
18 ms |
21632 KB |
Output is correct |
34 |
Correct |
18 ms |
21632 KB |
Output is correct |
35 |
Correct |
18 ms |
21760 KB |
Output is correct |
36 |
Correct |
21 ms |
21760 KB |
Output is correct |
37 |
Correct |
19 ms |
21888 KB |
Output is correct |
38 |
Correct |
19 ms |
21888 KB |
Output is correct |
39 |
Correct |
19 ms |
21888 KB |
Output is correct |
40 |
Correct |
20 ms |
22272 KB |
Output is correct |
41 |
Correct |
19 ms |
22144 KB |
Output is correct |
42 |
Correct |
18 ms |
21760 KB |
Output is correct |
43 |
Correct |
19 ms |
22016 KB |
Output is correct |
44 |
Correct |
20 ms |
22016 KB |
Output is correct |
45 |
Correct |
19 ms |
22016 KB |
Output is correct |
46 |
Correct |
20 ms |
22016 KB |
Output is correct |
47 |
Correct |
24 ms |
22016 KB |
Output is correct |
48 |
Correct |
24 ms |
21888 KB |
Output is correct |
49 |
Correct |
22 ms |
21888 KB |
Output is correct |
50 |
Correct |
21 ms |
21888 KB |
Output is correct |
51 |
Correct |
19 ms |
21760 KB |
Output is correct |
52 |
Correct |
20 ms |
21888 KB |
Output is correct |
53 |
Correct |
20 ms |
22016 KB |
Output is correct |
54 |
Correct |
22 ms |
21888 KB |
Output is correct |
55 |
Correct |
27 ms |
22528 KB |
Output is correct |
56 |
Correct |
51 ms |
25336 KB |
Output is correct |
57 |
Correct |
83 ms |
27636 KB |
Output is correct |
58 |
Correct |
107 ms |
29168 KB |
Output is correct |
59 |
Correct |
141 ms |
31344 KB |
Output is correct |
60 |
Correct |
192 ms |
33520 KB |
Output is correct |
61 |
Correct |
202 ms |
35316 KB |
Output is correct |
62 |
Correct |
228 ms |
36592 KB |
Output is correct |
63 |
Correct |
282 ms |
39148 KB |
Output is correct |
64 |
Correct |
294 ms |
39788 KB |
Output is correct |
65 |
Correct |
141 ms |
62456 KB |
Output is correct |
66 |
Correct |
147 ms |
62584 KB |
Output is correct |
67 |
Correct |
160 ms |
34296 KB |
Output is correct |
68 |
Correct |
193 ms |
52204 KB |
Output is correct |
69 |
Correct |
236 ms |
50284 KB |
Output is correct |
70 |
Correct |
237 ms |
50284 KB |
Output is correct |
71 |
Correct |
241 ms |
52580 KB |
Output is correct |
72 |
Correct |
239 ms |
52964 KB |
Output is correct |
73 |
Correct |
200 ms |
48612 KB |
Output is correct |
74 |
Correct |
219 ms |
48736 KB |
Output is correct |
75 |
Correct |
203 ms |
47204 KB |
Output is correct |
76 |
Correct |
203 ms |
47204 KB |
Output is correct |
77 |
Correct |
216 ms |
46372 KB |
Output is correct |
78 |
Correct |
208 ms |
44384 KB |
Output is correct |
79 |
Correct |
198 ms |
46600 KB |
Output is correct |