#include <bits/stdc++.h>
using namespace std;
template<class F>
struct y_combinator_result{
F f;
template<class T> explicit y_combinator_result(T &&f): f(forward<T>(f)){ }
template<class ...Args> decltype(auto) operator()(Args &&...args){ return f(ref(*this), forward<Args>(args)...); }
};
template<class F>
decltype(auto) y_combinator(F &&f){
return y_combinator_result<decay_t<F>>(forward<F>(f));
}
int main(){
cin.tie(0)->sync_with_stdio(0);
cin.exceptions(ios::badbit | ios::failbit);
int n, m;
cin >> n >> m;
vector<vector<int>> adj(n + m);
vector<int> par_w(n + m);
for(auto u = 1; u < n + m; ++ u){
int p;
cin >> p >> par_w[u], -- p;
adj[p].push_back(u);
}
auto res = y_combinator([&](auto self, int u)->pair<pair<int, long long>, priority_queue<long long>>{
pair<int, long long> line{};
priority_queue<long long> pq;
if(u >= n){
line = {1, -par_w[u]};
pq = priority_queue<long long>{{}, vector<long long>(2, par_w[u])};
}
else{
for(auto v: adj[u]){
auto [line_v, pq_v] = self(v);
line.first += line_v.first, line.second += line_v.second;
if((int)pq.size() < (int)pq_v.size()){
swap(pq, pq_v);
}
while(!pq_v.empty()){
pq.push(pq_v.top());
pq_v.pop();
}
}
while(line.first > 1){
-- line.first;
line.second += pq.top();
pq.pop();
}
array<long long, 2> store{};
for(auto rep = 0; rep < 2; ++ rep){
store[rep] = pq.top() + par_w[u];
pq.pop();
}
for(auto x: store){
pq.push(x);
}
line.second -= par_w[u];
}
return {line, pq};
})(0);
cout << 1LL * res.first.first * res.second.top() + res.first.second << "\n";
return 0;
}
/*
*/
////////////////////////////////////////////////////////////////////////////////////////
// //
// Coded by Aeren //
// //
////////////////////////////////////////////////////////////////////////////////////////
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
204 KB |
Output is correct |
2 |
Correct |
0 ms |
204 KB |
Output is correct |
3 |
Correct |
0 ms |
204 KB |
Output is correct |
4 |
Correct |
0 ms |
204 KB |
Output is correct |
5 |
Correct |
1 ms |
204 KB |
Output is correct |
6 |
Correct |
0 ms |
204 KB |
Output is correct |
7 |
Correct |
0 ms |
204 KB |
Output is correct |
8 |
Correct |
1 ms |
204 KB |
Output is correct |
9 |
Correct |
1 ms |
204 KB |
Output is correct |
10 |
Correct |
0 ms |
216 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
204 KB |
Output is correct |
2 |
Correct |
1 ms |
204 KB |
Output is correct |
3 |
Correct |
0 ms |
204 KB |
Output is correct |
4 |
Correct |
0 ms |
332 KB |
Output is correct |
5 |
Correct |
1 ms |
204 KB |
Output is correct |
6 |
Correct |
1 ms |
204 KB |
Output is correct |
7 |
Correct |
1 ms |
204 KB |
Output is correct |
8 |
Correct |
0 ms |
204 KB |
Output is correct |
9 |
Correct |
1 ms |
204 KB |
Output is correct |
10 |
Correct |
1 ms |
204 KB |
Output is correct |
11 |
Correct |
0 ms |
204 KB |
Output is correct |
12 |
Correct |
1 ms |
204 KB |
Output is correct |
13 |
Correct |
1 ms |
204 KB |
Output is correct |
14 |
Correct |
0 ms |
332 KB |
Output is correct |
15 |
Correct |
1 ms |
332 KB |
Output is correct |
16 |
Correct |
1 ms |
204 KB |
Output is correct |
17 |
Correct |
1 ms |
332 KB |
Output is correct |
18 |
Correct |
1 ms |
204 KB |
Output is correct |
19 |
Correct |
1 ms |
332 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
204 KB |
Output is correct |
2 |
Correct |
0 ms |
204 KB |
Output is correct |
3 |
Correct |
0 ms |
204 KB |
Output is correct |
4 |
Correct |
0 ms |
204 KB |
Output is correct |
5 |
Correct |
1 ms |
204 KB |
Output is correct |
6 |
Correct |
0 ms |
204 KB |
Output is correct |
7 |
Correct |
0 ms |
204 KB |
Output is correct |
8 |
Correct |
1 ms |
204 KB |
Output is correct |
9 |
Correct |
1 ms |
204 KB |
Output is correct |
10 |
Correct |
0 ms |
216 KB |
Output is correct |
11 |
Correct |
1 ms |
204 KB |
Output is correct |
12 |
Correct |
1 ms |
204 KB |
Output is correct |
13 |
Correct |
0 ms |
204 KB |
Output is correct |
14 |
Correct |
0 ms |
332 KB |
Output is correct |
15 |
Correct |
1 ms |
204 KB |
Output is correct |
16 |
Correct |
1 ms |
204 KB |
Output is correct |
17 |
Correct |
1 ms |
204 KB |
Output is correct |
18 |
Correct |
0 ms |
204 KB |
Output is correct |
19 |
Correct |
1 ms |
204 KB |
Output is correct |
20 |
Correct |
1 ms |
204 KB |
Output is correct |
21 |
Correct |
0 ms |
204 KB |
Output is correct |
22 |
Correct |
1 ms |
204 KB |
Output is correct |
23 |
Correct |
1 ms |
204 KB |
Output is correct |
24 |
Correct |
0 ms |
332 KB |
Output is correct |
25 |
Correct |
1 ms |
332 KB |
Output is correct |
26 |
Correct |
1 ms |
204 KB |
Output is correct |
27 |
Correct |
1 ms |
332 KB |
Output is correct |
28 |
Correct |
1 ms |
204 KB |
Output is correct |
29 |
Correct |
1 ms |
332 KB |
Output is correct |
30 |
Correct |
1 ms |
332 KB |
Output is correct |
31 |
Correct |
1 ms |
332 KB |
Output is correct |
32 |
Correct |
2 ms |
460 KB |
Output is correct |
33 |
Correct |
2 ms |
452 KB |
Output is correct |
34 |
Correct |
2 ms |
588 KB |
Output is correct |
35 |
Correct |
3 ms |
588 KB |
Output is correct |
36 |
Correct |
3 ms |
588 KB |
Output is correct |
37 |
Correct |
3 ms |
716 KB |
Output is correct |
38 |
Correct |
3 ms |
716 KB |
Output is correct |
39 |
Correct |
3 ms |
716 KB |
Output is correct |
40 |
Correct |
3 ms |
1732 KB |
Output is correct |
41 |
Correct |
3 ms |
1740 KB |
Output is correct |
42 |
Correct |
3 ms |
588 KB |
Output is correct |
43 |
Correct |
7 ms |
1244 KB |
Output is correct |
44 |
Correct |
5 ms |
1100 KB |
Output is correct |
45 |
Correct |
5 ms |
1116 KB |
Output is correct |
46 |
Correct |
3 ms |
660 KB |
Output is correct |
47 |
Correct |
4 ms |
588 KB |
Output is correct |
48 |
Correct |
3 ms |
588 KB |
Output is correct |
49 |
Correct |
3 ms |
588 KB |
Output is correct |
50 |
Correct |
2 ms |
588 KB |
Output is correct |
51 |
Correct |
2 ms |
588 KB |
Output is correct |
52 |
Correct |
3 ms |
588 KB |
Output is correct |
53 |
Correct |
3 ms |
588 KB |
Output is correct |
54 |
Correct |
3 ms |
716 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
204 KB |
Output is correct |
2 |
Correct |
0 ms |
204 KB |
Output is correct |
3 |
Correct |
0 ms |
204 KB |
Output is correct |
4 |
Correct |
0 ms |
204 KB |
Output is correct |
5 |
Correct |
1 ms |
204 KB |
Output is correct |
6 |
Correct |
0 ms |
204 KB |
Output is correct |
7 |
Correct |
0 ms |
204 KB |
Output is correct |
8 |
Correct |
1 ms |
204 KB |
Output is correct |
9 |
Correct |
1 ms |
204 KB |
Output is correct |
10 |
Correct |
0 ms |
216 KB |
Output is correct |
11 |
Correct |
1 ms |
204 KB |
Output is correct |
12 |
Correct |
1 ms |
204 KB |
Output is correct |
13 |
Correct |
0 ms |
204 KB |
Output is correct |
14 |
Correct |
0 ms |
332 KB |
Output is correct |
15 |
Correct |
1 ms |
204 KB |
Output is correct |
16 |
Correct |
1 ms |
204 KB |
Output is correct |
17 |
Correct |
1 ms |
204 KB |
Output is correct |
18 |
Correct |
0 ms |
204 KB |
Output is correct |
19 |
Correct |
1 ms |
204 KB |
Output is correct |
20 |
Correct |
1 ms |
204 KB |
Output is correct |
21 |
Correct |
0 ms |
204 KB |
Output is correct |
22 |
Correct |
1 ms |
204 KB |
Output is correct |
23 |
Correct |
1 ms |
204 KB |
Output is correct |
24 |
Correct |
0 ms |
332 KB |
Output is correct |
25 |
Correct |
1 ms |
332 KB |
Output is correct |
26 |
Correct |
1 ms |
204 KB |
Output is correct |
27 |
Correct |
1 ms |
332 KB |
Output is correct |
28 |
Correct |
1 ms |
204 KB |
Output is correct |
29 |
Correct |
1 ms |
332 KB |
Output is correct |
30 |
Correct |
1 ms |
332 KB |
Output is correct |
31 |
Correct |
1 ms |
332 KB |
Output is correct |
32 |
Correct |
2 ms |
460 KB |
Output is correct |
33 |
Correct |
2 ms |
452 KB |
Output is correct |
34 |
Correct |
2 ms |
588 KB |
Output is correct |
35 |
Correct |
3 ms |
588 KB |
Output is correct |
36 |
Correct |
3 ms |
588 KB |
Output is correct |
37 |
Correct |
3 ms |
716 KB |
Output is correct |
38 |
Correct |
3 ms |
716 KB |
Output is correct |
39 |
Correct |
3 ms |
716 KB |
Output is correct |
40 |
Correct |
3 ms |
1732 KB |
Output is correct |
41 |
Correct |
3 ms |
1740 KB |
Output is correct |
42 |
Correct |
3 ms |
588 KB |
Output is correct |
43 |
Correct |
7 ms |
1244 KB |
Output is correct |
44 |
Correct |
5 ms |
1100 KB |
Output is correct |
45 |
Correct |
5 ms |
1116 KB |
Output is correct |
46 |
Correct |
3 ms |
660 KB |
Output is correct |
47 |
Correct |
4 ms |
588 KB |
Output is correct |
48 |
Correct |
3 ms |
588 KB |
Output is correct |
49 |
Correct |
3 ms |
588 KB |
Output is correct |
50 |
Correct |
2 ms |
588 KB |
Output is correct |
51 |
Correct |
2 ms |
588 KB |
Output is correct |
52 |
Correct |
3 ms |
588 KB |
Output is correct |
53 |
Correct |
3 ms |
588 KB |
Output is correct |
54 |
Correct |
3 ms |
716 KB |
Output is correct |
55 |
Correct |
10 ms |
1372 KB |
Output is correct |
56 |
Correct |
41 ms |
4220 KB |
Output is correct |
57 |
Correct |
73 ms |
7056 KB |
Output is correct |
58 |
Correct |
83 ms |
8796 KB |
Output is correct |
59 |
Correct |
132 ms |
11652 KB |
Output is correct |
60 |
Correct |
153 ms |
14260 KB |
Output is correct |
61 |
Correct |
207 ms |
16280 KB |
Output is correct |
62 |
Correct |
213 ms |
17868 KB |
Output is correct |
63 |
Correct |
238 ms |
20400 KB |
Output is correct |
64 |
Correct |
324 ms |
22544 KB |
Output is correct |
65 |
Correct |
149 ms |
88588 KB |
Output is correct |
66 |
Correct |
178 ms |
88516 KB |
Output is correct |
67 |
Correct |
162 ms |
22944 KB |
Output is correct |
68 |
Execution timed out |
2085 ms |
53148 KB |
Time limit exceeded |
69 |
Halted |
0 ms |
0 KB |
- |