#include<bits/stdc++.h>
using namespace std;
#define int long long
const int N = 3e5 + 1;
struct Data {
int a, b;
priority_queue<int> pq;
Data operator + (Data &x) {
a += x.a;
b += x.b;
if (pq.size() < x.pq.size())
pq.swap(x.pq);
while (x.pq.size()) {
pq.push(x.pq.top());
x.pq.pop();
}
return *this;
}
} f[N];
int c[N];
vector<int> g[N];
void dfs(int u) {
if (g[u].empty()) {
f[u].a = 1;
f[u].b = -c[u];
f[u].pq.push(c[u]);
f[u].pq.push(c[u]);
return;
}
for(int v : g[u])
dfs(v), f[u] = f[u] + f[v];
for(; f[u].a > 1 ;)
f[u].a--,
f[u].b += f[u].pq.top(),
f[u].pq.pop();
int x = f[u].pq.top() + c[u]; f[u].pq.pop();
int y = f[u].pq.top() + c[u]; f[u].pq.pop();
f[u].b -= c[u];
f[u].pq.push(x);
f[u].pq.push(y);
}
signed main() {
ios_base::sync_with_stdio(0);
cin.tie(0); cout.tie(0);
int n; cin >> n;
int m; cin >> m;
for(int i = 2 ; i <= n + m ; ++i) {
int p;
cin >> p;
cin >> c[i];
g[p].push_back(i);
}
dfs(1); cout << f[1].b + f[1].pq.top() << endl;
}
/*
4 6
1 5
2 5
2 8
3 3
3 2
3 3
2 9
4 4
4 3
*/
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
24 ms |
21496 KB |
Output is correct |
2 |
Correct |
22 ms |
21496 KB |
Output is correct |
3 |
Correct |
24 ms |
21496 KB |
Output is correct |
4 |
Correct |
24 ms |
21512 KB |
Output is correct |
5 |
Correct |
24 ms |
21500 KB |
Output is correct |
6 |
Correct |
24 ms |
21496 KB |
Output is correct |
7 |
Correct |
24 ms |
21496 KB |
Output is correct |
8 |
Correct |
23 ms |
21496 KB |
Output is correct |
9 |
Correct |
24 ms |
21496 KB |
Output is correct |
10 |
Correct |
24 ms |
21496 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
23 ms |
21496 KB |
Output is correct |
2 |
Correct |
23 ms |
21496 KB |
Output is correct |
3 |
Correct |
22 ms |
21496 KB |
Output is correct |
4 |
Correct |
23 ms |
21496 KB |
Output is correct |
5 |
Correct |
24 ms |
21496 KB |
Output is correct |
6 |
Correct |
22 ms |
21496 KB |
Output is correct |
7 |
Correct |
21 ms |
21496 KB |
Output is correct |
8 |
Correct |
25 ms |
21496 KB |
Output is correct |
9 |
Correct |
23 ms |
21496 KB |
Output is correct |
10 |
Correct |
24 ms |
21496 KB |
Output is correct |
11 |
Correct |
24 ms |
21496 KB |
Output is correct |
12 |
Correct |
24 ms |
21496 KB |
Output is correct |
13 |
Correct |
24 ms |
21496 KB |
Output is correct |
14 |
Correct |
24 ms |
21448 KB |
Output is correct |
15 |
Correct |
24 ms |
21496 KB |
Output is correct |
16 |
Correct |
24 ms |
21496 KB |
Output is correct |
17 |
Correct |
24 ms |
21496 KB |
Output is correct |
18 |
Correct |
24 ms |
21496 KB |
Output is correct |
19 |
Correct |
23 ms |
21496 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
24 ms |
21496 KB |
Output is correct |
2 |
Correct |
22 ms |
21496 KB |
Output is correct |
3 |
Correct |
24 ms |
21496 KB |
Output is correct |
4 |
Correct |
24 ms |
21512 KB |
Output is correct |
5 |
Correct |
24 ms |
21500 KB |
Output is correct |
6 |
Correct |
24 ms |
21496 KB |
Output is correct |
7 |
Correct |
24 ms |
21496 KB |
Output is correct |
8 |
Correct |
23 ms |
21496 KB |
Output is correct |
9 |
Correct |
24 ms |
21496 KB |
Output is correct |
10 |
Correct |
24 ms |
21496 KB |
Output is correct |
11 |
Correct |
23 ms |
21496 KB |
Output is correct |
12 |
Correct |
23 ms |
21496 KB |
Output is correct |
13 |
Correct |
22 ms |
21496 KB |
Output is correct |
14 |
Correct |
23 ms |
21496 KB |
Output is correct |
15 |
Correct |
24 ms |
21496 KB |
Output is correct |
16 |
Correct |
22 ms |
21496 KB |
Output is correct |
17 |
Correct |
21 ms |
21496 KB |
Output is correct |
18 |
Correct |
25 ms |
21496 KB |
Output is correct |
19 |
Correct |
23 ms |
21496 KB |
Output is correct |
20 |
Correct |
24 ms |
21496 KB |
Output is correct |
21 |
Correct |
24 ms |
21496 KB |
Output is correct |
22 |
Correct |
24 ms |
21496 KB |
Output is correct |
23 |
Correct |
24 ms |
21496 KB |
Output is correct |
24 |
Correct |
24 ms |
21448 KB |
Output is correct |
25 |
Correct |
24 ms |
21496 KB |
Output is correct |
26 |
Correct |
24 ms |
21496 KB |
Output is correct |
27 |
Correct |
24 ms |
21496 KB |
Output is correct |
28 |
Correct |
24 ms |
21496 KB |
Output is correct |
29 |
Correct |
23 ms |
21496 KB |
Output is correct |
30 |
Correct |
22 ms |
21496 KB |
Output is correct |
31 |
Correct |
24 ms |
21596 KB |
Output is correct |
32 |
Correct |
24 ms |
21628 KB |
Output is correct |
33 |
Correct |
26 ms |
21624 KB |
Output is correct |
34 |
Correct |
24 ms |
21624 KB |
Output is correct |
35 |
Correct |
27 ms |
21752 KB |
Output is correct |
36 |
Correct |
27 ms |
21752 KB |
Output is correct |
37 |
Correct |
28 ms |
21808 KB |
Output is correct |
38 |
Correct |
26 ms |
21752 KB |
Output is correct |
39 |
Correct |
28 ms |
21856 KB |
Output is correct |
40 |
Correct |
24 ms |
22396 KB |
Output is correct |
41 |
Correct |
25 ms |
22520 KB |
Output is correct |
42 |
Correct |
59 ms |
21752 KB |
Output is correct |
43 |
Correct |
31 ms |
22136 KB |
Output is correct |
44 |
Correct |
29 ms |
22008 KB |
Output is correct |
45 |
Correct |
30 ms |
22136 KB |
Output is correct |
46 |
Correct |
28 ms |
22008 KB |
Output is correct |
47 |
Correct |
30 ms |
21996 KB |
Output is correct |
48 |
Correct |
33 ms |
21880 KB |
Output is correct |
49 |
Correct |
37 ms |
22044 KB |
Output is correct |
50 |
Correct |
45 ms |
22000 KB |
Output is correct |
51 |
Correct |
44 ms |
21996 KB |
Output is correct |
52 |
Correct |
43 ms |
21984 KB |
Output is correct |
53 |
Correct |
32 ms |
21880 KB |
Output is correct |
54 |
Correct |
97 ms |
22244 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
24 ms |
21496 KB |
Output is correct |
2 |
Correct |
22 ms |
21496 KB |
Output is correct |
3 |
Correct |
24 ms |
21496 KB |
Output is correct |
4 |
Correct |
24 ms |
21512 KB |
Output is correct |
5 |
Correct |
24 ms |
21500 KB |
Output is correct |
6 |
Correct |
24 ms |
21496 KB |
Output is correct |
7 |
Correct |
24 ms |
21496 KB |
Output is correct |
8 |
Correct |
23 ms |
21496 KB |
Output is correct |
9 |
Correct |
24 ms |
21496 KB |
Output is correct |
10 |
Correct |
24 ms |
21496 KB |
Output is correct |
11 |
Correct |
23 ms |
21496 KB |
Output is correct |
12 |
Correct |
23 ms |
21496 KB |
Output is correct |
13 |
Correct |
22 ms |
21496 KB |
Output is correct |
14 |
Correct |
23 ms |
21496 KB |
Output is correct |
15 |
Correct |
24 ms |
21496 KB |
Output is correct |
16 |
Correct |
22 ms |
21496 KB |
Output is correct |
17 |
Correct |
21 ms |
21496 KB |
Output is correct |
18 |
Correct |
25 ms |
21496 KB |
Output is correct |
19 |
Correct |
23 ms |
21496 KB |
Output is correct |
20 |
Correct |
24 ms |
21496 KB |
Output is correct |
21 |
Correct |
24 ms |
21496 KB |
Output is correct |
22 |
Correct |
24 ms |
21496 KB |
Output is correct |
23 |
Correct |
24 ms |
21496 KB |
Output is correct |
24 |
Correct |
24 ms |
21448 KB |
Output is correct |
25 |
Correct |
24 ms |
21496 KB |
Output is correct |
26 |
Correct |
24 ms |
21496 KB |
Output is correct |
27 |
Correct |
24 ms |
21496 KB |
Output is correct |
28 |
Correct |
24 ms |
21496 KB |
Output is correct |
29 |
Correct |
23 ms |
21496 KB |
Output is correct |
30 |
Correct |
22 ms |
21496 KB |
Output is correct |
31 |
Correct |
24 ms |
21596 KB |
Output is correct |
32 |
Correct |
24 ms |
21628 KB |
Output is correct |
33 |
Correct |
26 ms |
21624 KB |
Output is correct |
34 |
Correct |
24 ms |
21624 KB |
Output is correct |
35 |
Correct |
27 ms |
21752 KB |
Output is correct |
36 |
Correct |
27 ms |
21752 KB |
Output is correct |
37 |
Correct |
28 ms |
21808 KB |
Output is correct |
38 |
Correct |
26 ms |
21752 KB |
Output is correct |
39 |
Correct |
28 ms |
21856 KB |
Output is correct |
40 |
Correct |
24 ms |
22396 KB |
Output is correct |
41 |
Correct |
25 ms |
22520 KB |
Output is correct |
42 |
Correct |
59 ms |
21752 KB |
Output is correct |
43 |
Correct |
31 ms |
22136 KB |
Output is correct |
44 |
Correct |
29 ms |
22008 KB |
Output is correct |
45 |
Correct |
30 ms |
22136 KB |
Output is correct |
46 |
Correct |
28 ms |
22008 KB |
Output is correct |
47 |
Correct |
30 ms |
21996 KB |
Output is correct |
48 |
Correct |
33 ms |
21880 KB |
Output is correct |
49 |
Correct |
37 ms |
22044 KB |
Output is correct |
50 |
Correct |
45 ms |
22000 KB |
Output is correct |
51 |
Correct |
44 ms |
21996 KB |
Output is correct |
52 |
Correct |
43 ms |
21984 KB |
Output is correct |
53 |
Correct |
32 ms |
21880 KB |
Output is correct |
54 |
Correct |
97 ms |
22244 KB |
Output is correct |
55 |
Correct |
36 ms |
22364 KB |
Output is correct |
56 |
Correct |
88 ms |
24764 KB |
Output is correct |
57 |
Correct |
159 ms |
27216 KB |
Output is correct |
58 |
Correct |
328 ms |
28724 KB |
Output is correct |
59 |
Correct |
288 ms |
31072 KB |
Output is correct |
60 |
Correct |
345 ms |
33072 KB |
Output is correct |
61 |
Correct |
411 ms |
35392 KB |
Output is correct |
62 |
Correct |
452 ms |
37212 KB |
Output is correct |
63 |
Correct |
489 ms |
38836 KB |
Output is correct |
64 |
Correct |
584 ms |
43432 KB |
Output is correct |
65 |
Correct |
187 ms |
80376 KB |
Output is correct |
66 |
Correct |
314 ms |
80504 KB |
Output is correct |
67 |
Correct |
205 ms |
38264 KB |
Output is correct |
68 |
Execution timed out |
2041 ms |
56980 KB |
Time limit exceeded |
69 |
Halted |
0 ms |
0 KB |
- |