#include <bits/stdc++.h>
using namespace std;
const int Nmax = 3e5 + 5;
typedef long long ll;
ll A[Nmax], B[Nmax];
int up[Nmax], w[Nmax];
priority_queue<ll> heap[Nmax];
vector<int> edge[Nmax];
int n, m, N;
void combine(int node, int son)
{
A[node] += A[son]; B[node] += B[son];
while(heap[son].size())
{
heap[node].push(heap[son].top());
heap[son].pop();
}
}
void merge_sons(int node)
{
int xson = 0;
for(auto son : edge[node])
{
if(w[son] > w[xson]) xson = son;
w[node] += w[son];
}
swap(heap[node], heap[xson]);
A[node] = A[xson]; B[node] = B[xson];
for(auto son : edge[node])
if(son != xson)
combine(node, son);
}
int main()
{
// freopen("input", "r", stdin);
cin.sync_with_stdio(false); cin.tie(0);
cin >> n >> m;
N = n + m;
int i, node;
for(i=2; i<=N; ++i)
{
int parent;
cin >> parent >> up[i];
edge[parent].push_back(i);
}
for(node = N; node; --node)
{
w[node] = 1;
if(edge[node].empty())
{
heap[node].push(up[node]);
heap[node].push(up[node]);
A[node] = 1; B[node] = -up[node];
continue;
}
merge_sons(node);
while(A[node] > 1)
{
ll last_change = heap[node].top();
heap[node].pop();
A[node]--; B[node] += last_change;
}
ll change0, change1;
change1 = heap[node].top(); heap[node].pop();
change0 = heap[node].top(); heap[node].pop();
heap[node].push(change1 + up[node]);
heap[node].push(change0 + up[node]);
B[node] -= up[node];
}
cout << A[1] * heap[1].top() + B[1] << '\n';
return 0;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
18 ms |
16760 KB |
Output is correct |
2 |
Correct |
20 ms |
16760 KB |
Output is correct |
3 |
Correct |
17 ms |
16760 KB |
Output is correct |
4 |
Correct |
17 ms |
16888 KB |
Output is correct |
5 |
Correct |
17 ms |
16760 KB |
Output is correct |
6 |
Correct |
16 ms |
16760 KB |
Output is correct |
7 |
Correct |
17 ms |
16760 KB |
Output is correct |
8 |
Correct |
20 ms |
16760 KB |
Output is correct |
9 |
Correct |
21 ms |
16764 KB |
Output is correct |
10 |
Correct |
16 ms |
16760 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
20 ms |
16724 KB |
Output is correct |
2 |
Correct |
17 ms |
16732 KB |
Output is correct |
3 |
Correct |
17 ms |
16752 KB |
Output is correct |
4 |
Correct |
18 ms |
16760 KB |
Output is correct |
5 |
Correct |
17 ms |
16760 KB |
Output is correct |
6 |
Correct |
17 ms |
16888 KB |
Output is correct |
7 |
Correct |
17 ms |
16764 KB |
Output is correct |
8 |
Correct |
17 ms |
16760 KB |
Output is correct |
9 |
Correct |
19 ms |
16892 KB |
Output is correct |
10 |
Correct |
16 ms |
16760 KB |
Output is correct |
11 |
Correct |
17 ms |
16760 KB |
Output is correct |
12 |
Correct |
20 ms |
16760 KB |
Output is correct |
13 |
Correct |
21 ms |
16760 KB |
Output is correct |
14 |
Correct |
18 ms |
16760 KB |
Output is correct |
15 |
Correct |
17 ms |
16888 KB |
Output is correct |
16 |
Correct |
17 ms |
16888 KB |
Output is correct |
17 |
Correct |
17 ms |
16760 KB |
Output is correct |
18 |
Correct |
17 ms |
16888 KB |
Output is correct |
19 |
Correct |
17 ms |
16888 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
18 ms |
16760 KB |
Output is correct |
2 |
Correct |
20 ms |
16760 KB |
Output is correct |
3 |
Correct |
17 ms |
16760 KB |
Output is correct |
4 |
Correct |
17 ms |
16888 KB |
Output is correct |
5 |
Correct |
17 ms |
16760 KB |
Output is correct |
6 |
Correct |
16 ms |
16760 KB |
Output is correct |
7 |
Correct |
17 ms |
16760 KB |
Output is correct |
8 |
Correct |
20 ms |
16760 KB |
Output is correct |
9 |
Correct |
21 ms |
16764 KB |
Output is correct |
10 |
Correct |
16 ms |
16760 KB |
Output is correct |
11 |
Correct |
20 ms |
16724 KB |
Output is correct |
12 |
Correct |
17 ms |
16732 KB |
Output is correct |
13 |
Correct |
17 ms |
16752 KB |
Output is correct |
14 |
Correct |
18 ms |
16760 KB |
Output is correct |
15 |
Correct |
17 ms |
16760 KB |
Output is correct |
16 |
Correct |
17 ms |
16888 KB |
Output is correct |
17 |
Correct |
17 ms |
16764 KB |
Output is correct |
18 |
Correct |
17 ms |
16760 KB |
Output is correct |
19 |
Correct |
19 ms |
16892 KB |
Output is correct |
20 |
Correct |
16 ms |
16760 KB |
Output is correct |
21 |
Correct |
17 ms |
16760 KB |
Output is correct |
22 |
Correct |
20 ms |
16760 KB |
Output is correct |
23 |
Correct |
21 ms |
16760 KB |
Output is correct |
24 |
Correct |
18 ms |
16760 KB |
Output is correct |
25 |
Correct |
17 ms |
16888 KB |
Output is correct |
26 |
Correct |
17 ms |
16888 KB |
Output is correct |
27 |
Correct |
17 ms |
16760 KB |
Output is correct |
28 |
Correct |
17 ms |
16888 KB |
Output is correct |
29 |
Correct |
17 ms |
16888 KB |
Output is correct |
30 |
Correct |
17 ms |
16864 KB |
Output is correct |
31 |
Correct |
17 ms |
17016 KB |
Output is correct |
32 |
Correct |
18 ms |
17016 KB |
Output is correct |
33 |
Correct |
18 ms |
17016 KB |
Output is correct |
34 |
Correct |
18 ms |
17144 KB |
Output is correct |
35 |
Correct |
19 ms |
17144 KB |
Output is correct |
36 |
Correct |
20 ms |
17144 KB |
Output is correct |
37 |
Correct |
20 ms |
17276 KB |
Output is correct |
38 |
Correct |
20 ms |
17272 KB |
Output is correct |
39 |
Correct |
21 ms |
17272 KB |
Output is correct |
40 |
Correct |
19 ms |
17144 KB |
Output is correct |
41 |
Correct |
18 ms |
17144 KB |
Output is correct |
42 |
Correct |
19 ms |
17144 KB |
Output is correct |
43 |
Correct |
20 ms |
17144 KB |
Output is correct |
44 |
Correct |
23 ms |
17272 KB |
Output is correct |
45 |
Correct |
23 ms |
17144 KB |
Output is correct |
46 |
Correct |
20 ms |
17528 KB |
Output is correct |
47 |
Correct |
21 ms |
17400 KB |
Output is correct |
48 |
Correct |
19 ms |
17272 KB |
Output is correct |
49 |
Correct |
20 ms |
17400 KB |
Output is correct |
50 |
Correct |
19 ms |
17144 KB |
Output is correct |
51 |
Correct |
19 ms |
17272 KB |
Output is correct |
52 |
Correct |
20 ms |
17272 KB |
Output is correct |
53 |
Correct |
20 ms |
17272 KB |
Output is correct |
54 |
Correct |
20 ms |
17344 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
18 ms |
16760 KB |
Output is correct |
2 |
Correct |
20 ms |
16760 KB |
Output is correct |
3 |
Correct |
17 ms |
16760 KB |
Output is correct |
4 |
Correct |
17 ms |
16888 KB |
Output is correct |
5 |
Correct |
17 ms |
16760 KB |
Output is correct |
6 |
Correct |
16 ms |
16760 KB |
Output is correct |
7 |
Correct |
17 ms |
16760 KB |
Output is correct |
8 |
Correct |
20 ms |
16760 KB |
Output is correct |
9 |
Correct |
21 ms |
16764 KB |
Output is correct |
10 |
Correct |
16 ms |
16760 KB |
Output is correct |
11 |
Correct |
20 ms |
16724 KB |
Output is correct |
12 |
Correct |
17 ms |
16732 KB |
Output is correct |
13 |
Correct |
17 ms |
16752 KB |
Output is correct |
14 |
Correct |
18 ms |
16760 KB |
Output is correct |
15 |
Correct |
17 ms |
16760 KB |
Output is correct |
16 |
Correct |
17 ms |
16888 KB |
Output is correct |
17 |
Correct |
17 ms |
16764 KB |
Output is correct |
18 |
Correct |
17 ms |
16760 KB |
Output is correct |
19 |
Correct |
19 ms |
16892 KB |
Output is correct |
20 |
Correct |
16 ms |
16760 KB |
Output is correct |
21 |
Correct |
17 ms |
16760 KB |
Output is correct |
22 |
Correct |
20 ms |
16760 KB |
Output is correct |
23 |
Correct |
21 ms |
16760 KB |
Output is correct |
24 |
Correct |
18 ms |
16760 KB |
Output is correct |
25 |
Correct |
17 ms |
16888 KB |
Output is correct |
26 |
Correct |
17 ms |
16888 KB |
Output is correct |
27 |
Correct |
17 ms |
16760 KB |
Output is correct |
28 |
Correct |
17 ms |
16888 KB |
Output is correct |
29 |
Correct |
17 ms |
16888 KB |
Output is correct |
30 |
Correct |
17 ms |
16864 KB |
Output is correct |
31 |
Correct |
17 ms |
17016 KB |
Output is correct |
32 |
Correct |
18 ms |
17016 KB |
Output is correct |
33 |
Correct |
18 ms |
17016 KB |
Output is correct |
34 |
Correct |
18 ms |
17144 KB |
Output is correct |
35 |
Correct |
19 ms |
17144 KB |
Output is correct |
36 |
Correct |
20 ms |
17144 KB |
Output is correct |
37 |
Correct |
20 ms |
17276 KB |
Output is correct |
38 |
Correct |
20 ms |
17272 KB |
Output is correct |
39 |
Correct |
21 ms |
17272 KB |
Output is correct |
40 |
Correct |
19 ms |
17144 KB |
Output is correct |
41 |
Correct |
18 ms |
17144 KB |
Output is correct |
42 |
Correct |
19 ms |
17144 KB |
Output is correct |
43 |
Correct |
20 ms |
17144 KB |
Output is correct |
44 |
Correct |
23 ms |
17272 KB |
Output is correct |
45 |
Correct |
23 ms |
17144 KB |
Output is correct |
46 |
Correct |
20 ms |
17528 KB |
Output is correct |
47 |
Correct |
21 ms |
17400 KB |
Output is correct |
48 |
Correct |
19 ms |
17272 KB |
Output is correct |
49 |
Correct |
20 ms |
17400 KB |
Output is correct |
50 |
Correct |
19 ms |
17144 KB |
Output is correct |
51 |
Correct |
19 ms |
17272 KB |
Output is correct |
52 |
Correct |
20 ms |
17272 KB |
Output is correct |
53 |
Correct |
20 ms |
17272 KB |
Output is correct |
54 |
Correct |
20 ms |
17344 KB |
Output is correct |
55 |
Correct |
29 ms |
18040 KB |
Output is correct |
56 |
Correct |
50 ms |
21496 KB |
Output is correct |
57 |
Correct |
78 ms |
24632 KB |
Output is correct |
58 |
Correct |
101 ms |
26868 KB |
Output is correct |
59 |
Correct |
133 ms |
30196 KB |
Output is correct |
60 |
Correct |
167 ms |
33556 KB |
Output is correct |
61 |
Correct |
193 ms |
36284 KB |
Output is correct |
62 |
Correct |
251 ms |
37868 KB |
Output is correct |
63 |
Correct |
308 ms |
42084 KB |
Output is correct |
64 |
Correct |
279 ms |
43196 KB |
Output is correct |
65 |
Correct |
136 ms |
38012 KB |
Output is correct |
66 |
Correct |
129 ms |
38136 KB |
Output is correct |
67 |
Correct |
132 ms |
38048 KB |
Output is correct |
68 |
Correct |
191 ms |
39864 KB |
Output is correct |
69 |
Correct |
229 ms |
39920 KB |
Output is correct |
70 |
Correct |
228 ms |
39988 KB |
Output is correct |
71 |
Correct |
244 ms |
52664 KB |
Output is correct |
72 |
Correct |
237 ms |
52840 KB |
Output is correct |
73 |
Correct |
208 ms |
49124 KB |
Output is correct |
74 |
Correct |
213 ms |
49252 KB |
Output is correct |
75 |
Correct |
226 ms |
48396 KB |
Output is correct |
76 |
Correct |
210 ms |
48172 KB |
Output is correct |
77 |
Correct |
214 ms |
47356 KB |
Output is correct |
78 |
Correct |
218 ms |
46824 KB |
Output is correct |
79 |
Correct |
202 ms |
46176 KB |
Output is correct |