#include <bits/stdc++.h>
using namespace std;
typedef long long LL;
constexpr int NMAX = 3e5 + 5;
struct SlopeTrick {
LL a, b;
priority_queue <LL> H;
SlopeTrick () {
a = 0;
b = 0;
}
SlopeTrick operator += (SlopeTrick r) {
a += r.a;
b += r.b;
while (!r.H.empty()) {
H.push(r.H.top());
r.H.pop();
}
return *this;
}
};
int N, M;
int Dad[NMAX];
LL Cost[NMAX];
vector <int> G[NMAX];
void Read () {
ios_base::sync_with_stdio(false);
cin.tie(nullptr);
cin >> N >> M;
for (int i = 2; i <= N + M; ++ i ) {
cin >> Dad[i] >> Cost[i];
G[Dad[i]].push_back(i);
}
}
void Solve (int Node, SlopeTrick &ans) {
while (ans.a > 1) {
LL x = ans.H.top();
ans.H.pop();
-- ans.a;
ans.b += x;
}
LL slope_1 = ans.H.top();
ans.H.pop();
LL slope_0 = ans.H.top();
ans.H.pop();
ans.H.push(slope_0 + Cost[Node]);
ans.H.push(slope_1 + Cost[Node]);
ans.b -= Cost[Node];
}
void Dfs (int Node, SlopeTrick &ans) {
bool leaf = true;
for (auto it : G[Node]) {
SlopeTrick aux;
leaf = false;
Dfs(it, aux);
if (ans.H.size() < aux.H.size())
swap(ans, aux);
ans += aux;
}
if (leaf) {
ans.a = 1;
ans.b = -Cost[Node];
ans.H.push(Cost[Node]);
ans.H.push(Cost[Node]);
return;
}
if (Node == 1) return;
Solve(Node, ans);
}
int main () {
Read();
SlopeTrick ans;
Dfs(1, ans);
while (ans.a > 0) {
LL x = ans.H.top();
ans.H.pop();
ans.b += x;
-- ans.a;
}
cout << ans.b << '\n';
return 0;
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
5 ms |
7360 KB |
Output is correct |
2 |
Correct |
4 ms |
7380 KB |
Output is correct |
3 |
Correct |
4 ms |
7380 KB |
Output is correct |
4 |
Correct |
4 ms |
7380 KB |
Output is correct |
5 |
Correct |
4 ms |
7380 KB |
Output is correct |
6 |
Correct |
5 ms |
7372 KB |
Output is correct |
7 |
Correct |
4 ms |
7376 KB |
Output is correct |
8 |
Correct |
4 ms |
7380 KB |
Output is correct |
9 |
Correct |
4 ms |
7380 KB |
Output is correct |
10 |
Correct |
4 ms |
7372 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
4 ms |
7380 KB |
Output is correct |
2 |
Correct |
4 ms |
7300 KB |
Output is correct |
3 |
Correct |
4 ms |
7380 KB |
Output is correct |
4 |
Correct |
4 ms |
7372 KB |
Output is correct |
5 |
Correct |
5 ms |
7376 KB |
Output is correct |
6 |
Correct |
5 ms |
7380 KB |
Output is correct |
7 |
Correct |
4 ms |
7376 KB |
Output is correct |
8 |
Correct |
5 ms |
7380 KB |
Output is correct |
9 |
Correct |
4 ms |
7380 KB |
Output is correct |
10 |
Correct |
4 ms |
7380 KB |
Output is correct |
11 |
Correct |
4 ms |
7380 KB |
Output is correct |
12 |
Correct |
4 ms |
7308 KB |
Output is correct |
13 |
Correct |
4 ms |
7376 KB |
Output is correct |
14 |
Correct |
4 ms |
7380 KB |
Output is correct |
15 |
Correct |
4 ms |
7376 KB |
Output is correct |
16 |
Correct |
4 ms |
7376 KB |
Output is correct |
17 |
Correct |
4 ms |
7380 KB |
Output is correct |
18 |
Correct |
4 ms |
7380 KB |
Output is correct |
19 |
Correct |
4 ms |
7380 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
5 ms |
7360 KB |
Output is correct |
2 |
Correct |
4 ms |
7380 KB |
Output is correct |
3 |
Correct |
4 ms |
7380 KB |
Output is correct |
4 |
Correct |
4 ms |
7380 KB |
Output is correct |
5 |
Correct |
4 ms |
7380 KB |
Output is correct |
6 |
Correct |
5 ms |
7372 KB |
Output is correct |
7 |
Correct |
4 ms |
7376 KB |
Output is correct |
8 |
Correct |
4 ms |
7380 KB |
Output is correct |
9 |
Correct |
4 ms |
7380 KB |
Output is correct |
10 |
Correct |
4 ms |
7372 KB |
Output is correct |
11 |
Correct |
4 ms |
7380 KB |
Output is correct |
12 |
Correct |
4 ms |
7300 KB |
Output is correct |
13 |
Correct |
4 ms |
7380 KB |
Output is correct |
14 |
Correct |
4 ms |
7372 KB |
Output is correct |
15 |
Correct |
5 ms |
7376 KB |
Output is correct |
16 |
Correct |
5 ms |
7380 KB |
Output is correct |
17 |
Correct |
4 ms |
7376 KB |
Output is correct |
18 |
Correct |
5 ms |
7380 KB |
Output is correct |
19 |
Correct |
4 ms |
7380 KB |
Output is correct |
20 |
Correct |
4 ms |
7380 KB |
Output is correct |
21 |
Correct |
4 ms |
7380 KB |
Output is correct |
22 |
Correct |
4 ms |
7308 KB |
Output is correct |
23 |
Correct |
4 ms |
7376 KB |
Output is correct |
24 |
Correct |
4 ms |
7380 KB |
Output is correct |
25 |
Correct |
4 ms |
7376 KB |
Output is correct |
26 |
Correct |
4 ms |
7376 KB |
Output is correct |
27 |
Correct |
4 ms |
7380 KB |
Output is correct |
28 |
Correct |
4 ms |
7380 KB |
Output is correct |
29 |
Correct |
4 ms |
7380 KB |
Output is correct |
30 |
Correct |
4 ms |
7380 KB |
Output is correct |
31 |
Correct |
4 ms |
7380 KB |
Output is correct |
32 |
Correct |
4 ms |
7508 KB |
Output is correct |
33 |
Correct |
5 ms |
7508 KB |
Output is correct |
34 |
Correct |
6 ms |
7508 KB |
Output is correct |
35 |
Correct |
5 ms |
7636 KB |
Output is correct |
36 |
Correct |
6 ms |
7636 KB |
Output is correct |
37 |
Correct |
6 ms |
7636 KB |
Output is correct |
38 |
Correct |
6 ms |
7636 KB |
Output is correct |
39 |
Correct |
6 ms |
7644 KB |
Output is correct |
40 |
Correct |
6 ms |
8788 KB |
Output is correct |
41 |
Correct |
6 ms |
8796 KB |
Output is correct |
42 |
Correct |
6 ms |
7640 KB |
Output is correct |
43 |
Correct |
8 ms |
8156 KB |
Output is correct |
44 |
Correct |
9 ms |
8056 KB |
Output is correct |
45 |
Correct |
8 ms |
8024 KB |
Output is correct |
46 |
Correct |
7 ms |
7636 KB |
Output is correct |
47 |
Correct |
7 ms |
7636 KB |
Output is correct |
48 |
Correct |
7 ms |
7644 KB |
Output is correct |
49 |
Correct |
8 ms |
7636 KB |
Output is correct |
50 |
Correct |
10 ms |
7648 KB |
Output is correct |
51 |
Correct |
11 ms |
7640 KB |
Output is correct |
52 |
Correct |
8 ms |
7636 KB |
Output is correct |
53 |
Correct |
8 ms |
7692 KB |
Output is correct |
54 |
Correct |
11 ms |
7764 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
5 ms |
7360 KB |
Output is correct |
2 |
Correct |
4 ms |
7380 KB |
Output is correct |
3 |
Correct |
4 ms |
7380 KB |
Output is correct |
4 |
Correct |
4 ms |
7380 KB |
Output is correct |
5 |
Correct |
4 ms |
7380 KB |
Output is correct |
6 |
Correct |
5 ms |
7372 KB |
Output is correct |
7 |
Correct |
4 ms |
7376 KB |
Output is correct |
8 |
Correct |
4 ms |
7380 KB |
Output is correct |
9 |
Correct |
4 ms |
7380 KB |
Output is correct |
10 |
Correct |
4 ms |
7372 KB |
Output is correct |
11 |
Correct |
4 ms |
7380 KB |
Output is correct |
12 |
Correct |
4 ms |
7300 KB |
Output is correct |
13 |
Correct |
4 ms |
7380 KB |
Output is correct |
14 |
Correct |
4 ms |
7372 KB |
Output is correct |
15 |
Correct |
5 ms |
7376 KB |
Output is correct |
16 |
Correct |
5 ms |
7380 KB |
Output is correct |
17 |
Correct |
4 ms |
7376 KB |
Output is correct |
18 |
Correct |
5 ms |
7380 KB |
Output is correct |
19 |
Correct |
4 ms |
7380 KB |
Output is correct |
20 |
Correct |
4 ms |
7380 KB |
Output is correct |
21 |
Correct |
4 ms |
7380 KB |
Output is correct |
22 |
Correct |
4 ms |
7308 KB |
Output is correct |
23 |
Correct |
4 ms |
7376 KB |
Output is correct |
24 |
Correct |
4 ms |
7380 KB |
Output is correct |
25 |
Correct |
4 ms |
7376 KB |
Output is correct |
26 |
Correct |
4 ms |
7376 KB |
Output is correct |
27 |
Correct |
4 ms |
7380 KB |
Output is correct |
28 |
Correct |
4 ms |
7380 KB |
Output is correct |
29 |
Correct |
4 ms |
7380 KB |
Output is correct |
30 |
Correct |
4 ms |
7380 KB |
Output is correct |
31 |
Correct |
4 ms |
7380 KB |
Output is correct |
32 |
Correct |
4 ms |
7508 KB |
Output is correct |
33 |
Correct |
5 ms |
7508 KB |
Output is correct |
34 |
Correct |
6 ms |
7508 KB |
Output is correct |
35 |
Correct |
5 ms |
7636 KB |
Output is correct |
36 |
Correct |
6 ms |
7636 KB |
Output is correct |
37 |
Correct |
6 ms |
7636 KB |
Output is correct |
38 |
Correct |
6 ms |
7636 KB |
Output is correct |
39 |
Correct |
6 ms |
7644 KB |
Output is correct |
40 |
Correct |
6 ms |
8788 KB |
Output is correct |
41 |
Correct |
6 ms |
8796 KB |
Output is correct |
42 |
Correct |
6 ms |
7640 KB |
Output is correct |
43 |
Correct |
8 ms |
8156 KB |
Output is correct |
44 |
Correct |
9 ms |
8056 KB |
Output is correct |
45 |
Correct |
8 ms |
8024 KB |
Output is correct |
46 |
Correct |
7 ms |
7636 KB |
Output is correct |
47 |
Correct |
7 ms |
7636 KB |
Output is correct |
48 |
Correct |
7 ms |
7644 KB |
Output is correct |
49 |
Correct |
8 ms |
7636 KB |
Output is correct |
50 |
Correct |
10 ms |
7648 KB |
Output is correct |
51 |
Correct |
11 ms |
7640 KB |
Output is correct |
52 |
Correct |
8 ms |
7636 KB |
Output is correct |
53 |
Correct |
8 ms |
7692 KB |
Output is correct |
54 |
Correct |
11 ms |
7764 KB |
Output is correct |
55 |
Correct |
10 ms |
8148 KB |
Output is correct |
56 |
Correct |
32 ms |
10172 KB |
Output is correct |
57 |
Correct |
52 ms |
12128 KB |
Output is correct |
58 |
Correct |
75 ms |
13360 KB |
Output is correct |
59 |
Correct |
103 ms |
15344 KB |
Output is correct |
60 |
Correct |
129 ms |
17376 KB |
Output is correct |
61 |
Correct |
160 ms |
18696 KB |
Output is correct |
62 |
Correct |
164 ms |
19888 KB |
Output is correct |
63 |
Correct |
199 ms |
22536 KB |
Output is correct |
64 |
Correct |
234 ms |
23308 KB |
Output is correct |
65 |
Correct |
126 ms |
95588 KB |
Output is correct |
66 |
Correct |
124 ms |
95560 KB |
Output is correct |
67 |
Correct |
106 ms |
25256 KB |
Output is correct |
68 |
Execution timed out |
2082 ms |
57444 KB |
Time limit exceeded |
69 |
Halted |
0 ms |
0 KB |
- |