#include <iostream>
#include <vector>
#include <queue>
#define long long long
using namespace std;
const int N = 3e5 + 2;
int n, m;
vector<pair<int, int> > g[N];
priority_queue<long> q[N];
pair<int, long> line[N];
void Merge(priority_queue<long> &a, priority_queue<long> &b)
{
if (a.size() < b.size()) a.swap(b);
while (!b.empty()) a.push(b.top()), b.pop();
}
void Dfs(int u, int cost)
{
if (u > n)
{
q[u].push(cost);
q[u].push(cost);
line[u] = make_pair(1, -cost);
return;
}
for (auto e: g[u])
{
int v = e.first, c = e.second;
Dfs(v, c);
line[u].first += line[v].first;
line[u].second += line[v].second;
Merge(q[u], q[v]);
}
while (line[u].first > 1)
{
line[u].second += q[u].top();
q[u].pop();
--line[u].first;
}
long p1 = q[u].top(); q[u].pop();
long p0 = q[u].top(); q[u].pop();
q[u].push(p1 + cost);
q[u].push(p0 + cost);
line[u].second -= cost;
}
int main()
{
cin >> n >> m;
for (int i = 2; i <= n + m; ++i)
{
int p, c; cin >> p >> c;
g[p].push_back(make_pair(i, c));
}
Dfs(1, 0);
cout << line[1].second + q[1].top();
return 0;
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
15 ms |
16888 KB |
Output is correct |
2 |
Correct |
17 ms |
16888 KB |
Output is correct |
3 |
Correct |
17 ms |
16948 KB |
Output is correct |
4 |
Correct |
14 ms |
16948 KB |
Output is correct |
5 |
Correct |
16 ms |
16980 KB |
Output is correct |
6 |
Correct |
17 ms |
16980 KB |
Output is correct |
7 |
Correct |
16 ms |
16980 KB |
Output is correct |
8 |
Correct |
15 ms |
16980 KB |
Output is correct |
9 |
Correct |
15 ms |
16980 KB |
Output is correct |
10 |
Correct |
17 ms |
17040 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
17 ms |
17040 KB |
Output is correct |
2 |
Correct |
15 ms |
17040 KB |
Output is correct |
3 |
Correct |
15 ms |
17040 KB |
Output is correct |
4 |
Correct |
16 ms |
17040 KB |
Output is correct |
5 |
Correct |
18 ms |
17040 KB |
Output is correct |
6 |
Correct |
15 ms |
17040 KB |
Output is correct |
7 |
Correct |
16 ms |
17076 KB |
Output is correct |
8 |
Correct |
15 ms |
17076 KB |
Output is correct |
9 |
Correct |
16 ms |
17076 KB |
Output is correct |
10 |
Correct |
17 ms |
17076 KB |
Output is correct |
11 |
Correct |
16 ms |
17076 KB |
Output is correct |
12 |
Correct |
19 ms |
17076 KB |
Output is correct |
13 |
Correct |
18 ms |
17076 KB |
Output is correct |
14 |
Correct |
17 ms |
17076 KB |
Output is correct |
15 |
Correct |
16 ms |
17076 KB |
Output is correct |
16 |
Correct |
16 ms |
17076 KB |
Output is correct |
17 |
Correct |
15 ms |
17076 KB |
Output is correct |
18 |
Correct |
15 ms |
17076 KB |
Output is correct |
19 |
Correct |
17 ms |
17076 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
15 ms |
16888 KB |
Output is correct |
2 |
Correct |
17 ms |
16888 KB |
Output is correct |
3 |
Correct |
17 ms |
16948 KB |
Output is correct |
4 |
Correct |
14 ms |
16948 KB |
Output is correct |
5 |
Correct |
16 ms |
16980 KB |
Output is correct |
6 |
Correct |
17 ms |
16980 KB |
Output is correct |
7 |
Correct |
16 ms |
16980 KB |
Output is correct |
8 |
Correct |
15 ms |
16980 KB |
Output is correct |
9 |
Correct |
15 ms |
16980 KB |
Output is correct |
10 |
Correct |
17 ms |
17040 KB |
Output is correct |
11 |
Correct |
17 ms |
17040 KB |
Output is correct |
12 |
Correct |
15 ms |
17040 KB |
Output is correct |
13 |
Correct |
15 ms |
17040 KB |
Output is correct |
14 |
Correct |
16 ms |
17040 KB |
Output is correct |
15 |
Correct |
18 ms |
17040 KB |
Output is correct |
16 |
Correct |
15 ms |
17040 KB |
Output is correct |
17 |
Correct |
16 ms |
17076 KB |
Output is correct |
18 |
Correct |
15 ms |
17076 KB |
Output is correct |
19 |
Correct |
16 ms |
17076 KB |
Output is correct |
20 |
Correct |
17 ms |
17076 KB |
Output is correct |
21 |
Correct |
16 ms |
17076 KB |
Output is correct |
22 |
Correct |
19 ms |
17076 KB |
Output is correct |
23 |
Correct |
18 ms |
17076 KB |
Output is correct |
24 |
Correct |
17 ms |
17076 KB |
Output is correct |
25 |
Correct |
16 ms |
17076 KB |
Output is correct |
26 |
Correct |
16 ms |
17076 KB |
Output is correct |
27 |
Correct |
15 ms |
17076 KB |
Output is correct |
28 |
Correct |
15 ms |
17076 KB |
Output is correct |
29 |
Correct |
17 ms |
17076 KB |
Output is correct |
30 |
Correct |
17 ms |
17076 KB |
Output is correct |
31 |
Correct |
17 ms |
17148 KB |
Output is correct |
32 |
Correct |
22 ms |
17196 KB |
Output is correct |
33 |
Correct |
18 ms |
17236 KB |
Output is correct |
34 |
Correct |
25 ms |
17412 KB |
Output is correct |
35 |
Correct |
21 ms |
17456 KB |
Output is correct |
36 |
Correct |
22 ms |
17524 KB |
Output is correct |
37 |
Correct |
24 ms |
17724 KB |
Output is correct |
38 |
Correct |
23 ms |
17792 KB |
Output is correct |
39 |
Correct |
22 ms |
17852 KB |
Output is correct |
40 |
Correct |
22 ms |
18468 KB |
Output is correct |
41 |
Correct |
22 ms |
18536 KB |
Output is correct |
42 |
Correct |
20 ms |
18536 KB |
Output is correct |
43 |
Correct |
21 ms |
18536 KB |
Output is correct |
44 |
Correct |
24 ms |
18536 KB |
Output is correct |
45 |
Correct |
24 ms |
18536 KB |
Output is correct |
46 |
Correct |
21 ms |
18572 KB |
Output is correct |
47 |
Correct |
25 ms |
18764 KB |
Output is correct |
48 |
Correct |
21 ms |
18764 KB |
Output is correct |
49 |
Correct |
21 ms |
18764 KB |
Output is correct |
50 |
Correct |
19 ms |
18764 KB |
Output is correct |
51 |
Correct |
20 ms |
18848 KB |
Output is correct |
52 |
Correct |
21 ms |
18848 KB |
Output is correct |
53 |
Correct |
20 ms |
18856 KB |
Output is correct |
54 |
Correct |
20 ms |
18956 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
15 ms |
16888 KB |
Output is correct |
2 |
Correct |
17 ms |
16888 KB |
Output is correct |
3 |
Correct |
17 ms |
16948 KB |
Output is correct |
4 |
Correct |
14 ms |
16948 KB |
Output is correct |
5 |
Correct |
16 ms |
16980 KB |
Output is correct |
6 |
Correct |
17 ms |
16980 KB |
Output is correct |
7 |
Correct |
16 ms |
16980 KB |
Output is correct |
8 |
Correct |
15 ms |
16980 KB |
Output is correct |
9 |
Correct |
15 ms |
16980 KB |
Output is correct |
10 |
Correct |
17 ms |
17040 KB |
Output is correct |
11 |
Correct |
17 ms |
17040 KB |
Output is correct |
12 |
Correct |
15 ms |
17040 KB |
Output is correct |
13 |
Correct |
15 ms |
17040 KB |
Output is correct |
14 |
Correct |
16 ms |
17040 KB |
Output is correct |
15 |
Correct |
18 ms |
17040 KB |
Output is correct |
16 |
Correct |
15 ms |
17040 KB |
Output is correct |
17 |
Correct |
16 ms |
17076 KB |
Output is correct |
18 |
Correct |
15 ms |
17076 KB |
Output is correct |
19 |
Correct |
16 ms |
17076 KB |
Output is correct |
20 |
Correct |
17 ms |
17076 KB |
Output is correct |
21 |
Correct |
16 ms |
17076 KB |
Output is correct |
22 |
Correct |
19 ms |
17076 KB |
Output is correct |
23 |
Correct |
18 ms |
17076 KB |
Output is correct |
24 |
Correct |
17 ms |
17076 KB |
Output is correct |
25 |
Correct |
16 ms |
17076 KB |
Output is correct |
26 |
Correct |
16 ms |
17076 KB |
Output is correct |
27 |
Correct |
15 ms |
17076 KB |
Output is correct |
28 |
Correct |
15 ms |
17076 KB |
Output is correct |
29 |
Correct |
17 ms |
17076 KB |
Output is correct |
30 |
Correct |
17 ms |
17076 KB |
Output is correct |
31 |
Correct |
17 ms |
17148 KB |
Output is correct |
32 |
Correct |
22 ms |
17196 KB |
Output is correct |
33 |
Correct |
18 ms |
17236 KB |
Output is correct |
34 |
Correct |
25 ms |
17412 KB |
Output is correct |
35 |
Correct |
21 ms |
17456 KB |
Output is correct |
36 |
Correct |
22 ms |
17524 KB |
Output is correct |
37 |
Correct |
24 ms |
17724 KB |
Output is correct |
38 |
Correct |
23 ms |
17792 KB |
Output is correct |
39 |
Correct |
22 ms |
17852 KB |
Output is correct |
40 |
Correct |
22 ms |
18468 KB |
Output is correct |
41 |
Correct |
22 ms |
18536 KB |
Output is correct |
42 |
Correct |
20 ms |
18536 KB |
Output is correct |
43 |
Correct |
21 ms |
18536 KB |
Output is correct |
44 |
Correct |
24 ms |
18536 KB |
Output is correct |
45 |
Correct |
24 ms |
18536 KB |
Output is correct |
46 |
Correct |
21 ms |
18572 KB |
Output is correct |
47 |
Correct |
25 ms |
18764 KB |
Output is correct |
48 |
Correct |
21 ms |
18764 KB |
Output is correct |
49 |
Correct |
21 ms |
18764 KB |
Output is correct |
50 |
Correct |
19 ms |
18764 KB |
Output is correct |
51 |
Correct |
20 ms |
18848 KB |
Output is correct |
52 |
Correct |
21 ms |
18848 KB |
Output is correct |
53 |
Correct |
20 ms |
18856 KB |
Output is correct |
54 |
Correct |
20 ms |
18956 KB |
Output is correct |
55 |
Correct |
30 ms |
19620 KB |
Output is correct |
56 |
Correct |
94 ms |
22652 KB |
Output is correct |
57 |
Correct |
146 ms |
26156 KB |
Output is correct |
58 |
Correct |
204 ms |
29516 KB |
Output is correct |
59 |
Correct |
251 ms |
33848 KB |
Output is correct |
60 |
Correct |
393 ms |
38940 KB |
Output is correct |
61 |
Correct |
453 ms |
43976 KB |
Output is correct |
62 |
Correct |
430 ms |
48796 KB |
Output is correct |
63 |
Correct |
556 ms |
55584 KB |
Output is correct |
64 |
Correct |
469 ms |
60828 KB |
Output is correct |
65 |
Correct |
399 ms |
94744 KB |
Output is correct |
66 |
Correct |
326 ms |
99488 KB |
Output is correct |
67 |
Correct |
357 ms |
99488 KB |
Output is correct |
68 |
Correct |
446 ms |
99488 KB |
Output is correct |
69 |
Correct |
482 ms |
99488 KB |
Output is correct |
70 |
Correct |
515 ms |
101112 KB |
Output is correct |
71 |
Correct |
487 ms |
104852 KB |
Output is correct |
72 |
Correct |
476 ms |
109256 KB |
Output is correct |
73 |
Correct |
461 ms |
109680 KB |
Output is correct |
74 |
Correct |
386 ms |
113880 KB |
Output is correct |
75 |
Correct |
390 ms |
116472 KB |
Output is correct |
76 |
Correct |
346 ms |
120292 KB |
Output is correct |
77 |
Correct |
362 ms |
123400 KB |
Output is correct |
78 |
Correct |
358 ms |
125852 KB |
Output is correct |
79 |
Correct |
375 ms |
130320 KB |
Output is correct |