#include<bits/stdc++.h>
using namespace std;
typedef long long LL;
const int MAXN = 3e5+7;
vector< int >children[MAXN];
int parent[MAXN];
int weight[MAXN];
struct SlopeTrickable {
priority_queue< LL >slopeChanges;
LL m, c; ///mx + c is the rightmost line
void throwSlopesGreaterThan(LL g) {
while (m > g) {
///mx + c = (m-1)x + (x+c)
m -= 1;
c += slopeChanges.top();
slopeChanges.pop();
}
}
};
void merge(SlopeTrickable &from, SlopeTrickable &to) {
if (from.slopeChanges.size() > to.slopeChanges.size())
swap(from, to);
while (!from.slopeChanges.empty()) {
to.slopeChanges.push(from.slopeChanges.top());
from.slopeChanges.pop();
}
to.m += from.m;
to.c += from.c;
}
SlopeTrickable subtree[MAXN];
int main() {
ios::sync_with_stdio(false);
cin.tie(0);
int n, m;
cin >> n >> m;
for (int i = 2; i <= n+m; i++) {
cin >> parent[i] >> weight[i];
children[parent[i]].push_back(i);
}
for (int i = n+1; i <= n+m; i++) {
subtree[i].slopeChanges.push(weight[i]);
subtree[i].slopeChanges.push(weight[i]);
subtree[i].m = 1;
subtree[i].c = -weight[i];
}
for (int i = n; i > 0; i--) {
for (int c : children[i]) {
merge(subtree[c], subtree[i]);
}
subtree[i].throwSlopesGreaterThan(1);
if (i > 1) {
LL x1 = subtree[i].slopeChanges.top();
subtree[i].slopeChanges.pop();
LL x0 = subtree[i].slopeChanges.top();
subtree[i].slopeChanges.pop();
subtree[i].slopeChanges.push(x0+weight[i]);
subtree[i].slopeChanges.push(x1+weight[i]);
subtree[i].c -= weight[i];
}
}
cout << subtree[1].slopeChanges.top() + subtree[1].c << "\n";
return 0;
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
15 ms |
21484 KB |
Output is correct |
2 |
Correct |
15 ms |
21484 KB |
Output is correct |
3 |
Correct |
14 ms |
21484 KB |
Output is correct |
4 |
Correct |
15 ms |
21484 KB |
Output is correct |
5 |
Correct |
14 ms |
21484 KB |
Output is correct |
6 |
Correct |
15 ms |
21484 KB |
Output is correct |
7 |
Correct |
14 ms |
21484 KB |
Output is correct |
8 |
Correct |
15 ms |
21484 KB |
Output is correct |
9 |
Correct |
15 ms |
21484 KB |
Output is correct |
10 |
Correct |
15 ms |
21612 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
15 ms |
21484 KB |
Output is correct |
2 |
Correct |
15 ms |
21484 KB |
Output is correct |
3 |
Correct |
15 ms |
21484 KB |
Output is correct |
4 |
Correct |
14 ms |
21484 KB |
Output is correct |
5 |
Correct |
16 ms |
21484 KB |
Output is correct |
6 |
Correct |
17 ms |
21484 KB |
Output is correct |
7 |
Correct |
15 ms |
21484 KB |
Output is correct |
8 |
Correct |
15 ms |
21484 KB |
Output is correct |
9 |
Correct |
15 ms |
21612 KB |
Output is correct |
10 |
Correct |
14 ms |
21484 KB |
Output is correct |
11 |
Correct |
14 ms |
21484 KB |
Output is correct |
12 |
Correct |
15 ms |
21484 KB |
Output is correct |
13 |
Correct |
15 ms |
21484 KB |
Output is correct |
14 |
Correct |
15 ms |
21612 KB |
Output is correct |
15 |
Correct |
15 ms |
21484 KB |
Output is correct |
16 |
Correct |
15 ms |
21484 KB |
Output is correct |
17 |
Correct |
14 ms |
21484 KB |
Output is correct |
18 |
Correct |
15 ms |
21484 KB |
Output is correct |
19 |
Correct |
15 ms |
21484 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
15 ms |
21484 KB |
Output is correct |
2 |
Correct |
15 ms |
21484 KB |
Output is correct |
3 |
Correct |
14 ms |
21484 KB |
Output is correct |
4 |
Correct |
15 ms |
21484 KB |
Output is correct |
5 |
Correct |
14 ms |
21484 KB |
Output is correct |
6 |
Correct |
15 ms |
21484 KB |
Output is correct |
7 |
Correct |
14 ms |
21484 KB |
Output is correct |
8 |
Correct |
15 ms |
21484 KB |
Output is correct |
9 |
Correct |
15 ms |
21484 KB |
Output is correct |
10 |
Correct |
15 ms |
21612 KB |
Output is correct |
11 |
Correct |
15 ms |
21484 KB |
Output is correct |
12 |
Correct |
15 ms |
21484 KB |
Output is correct |
13 |
Correct |
15 ms |
21484 KB |
Output is correct |
14 |
Correct |
14 ms |
21484 KB |
Output is correct |
15 |
Correct |
16 ms |
21484 KB |
Output is correct |
16 |
Correct |
17 ms |
21484 KB |
Output is correct |
17 |
Correct |
15 ms |
21484 KB |
Output is correct |
18 |
Correct |
15 ms |
21484 KB |
Output is correct |
19 |
Correct |
15 ms |
21612 KB |
Output is correct |
20 |
Correct |
14 ms |
21484 KB |
Output is correct |
21 |
Correct |
14 ms |
21484 KB |
Output is correct |
22 |
Correct |
15 ms |
21484 KB |
Output is correct |
23 |
Correct |
15 ms |
21484 KB |
Output is correct |
24 |
Correct |
15 ms |
21612 KB |
Output is correct |
25 |
Correct |
15 ms |
21484 KB |
Output is correct |
26 |
Correct |
15 ms |
21484 KB |
Output is correct |
27 |
Correct |
14 ms |
21484 KB |
Output is correct |
28 |
Correct |
15 ms |
21484 KB |
Output is correct |
29 |
Correct |
15 ms |
21484 KB |
Output is correct |
30 |
Correct |
15 ms |
21484 KB |
Output is correct |
31 |
Correct |
18 ms |
21612 KB |
Output is correct |
32 |
Correct |
17 ms |
21760 KB |
Output is correct |
33 |
Correct |
15 ms |
21612 KB |
Output is correct |
34 |
Correct |
16 ms |
21740 KB |
Output is correct |
35 |
Correct |
18 ms |
21868 KB |
Output is correct |
36 |
Correct |
17 ms |
21740 KB |
Output is correct |
37 |
Correct |
17 ms |
21868 KB |
Output is correct |
38 |
Correct |
18 ms |
21996 KB |
Output is correct |
39 |
Correct |
17 ms |
21996 KB |
Output is correct |
40 |
Correct |
16 ms |
21740 KB |
Output is correct |
41 |
Correct |
16 ms |
21740 KB |
Output is correct |
42 |
Correct |
16 ms |
21740 KB |
Output is correct |
43 |
Correct |
17 ms |
21740 KB |
Output is correct |
44 |
Correct |
17 ms |
21868 KB |
Output is correct |
45 |
Correct |
17 ms |
21868 KB |
Output is correct |
46 |
Correct |
20 ms |
21996 KB |
Output is correct |
47 |
Correct |
17 ms |
21996 KB |
Output is correct |
48 |
Correct |
18 ms |
21868 KB |
Output is correct |
49 |
Correct |
17 ms |
21948 KB |
Output is correct |
50 |
Correct |
16 ms |
21868 KB |
Output is correct |
51 |
Correct |
17 ms |
21868 KB |
Output is correct |
52 |
Correct |
19 ms |
21868 KB |
Output is correct |
53 |
Correct |
17 ms |
21868 KB |
Output is correct |
54 |
Correct |
19 ms |
22144 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
15 ms |
21484 KB |
Output is correct |
2 |
Correct |
15 ms |
21484 KB |
Output is correct |
3 |
Correct |
14 ms |
21484 KB |
Output is correct |
4 |
Correct |
15 ms |
21484 KB |
Output is correct |
5 |
Correct |
14 ms |
21484 KB |
Output is correct |
6 |
Correct |
15 ms |
21484 KB |
Output is correct |
7 |
Correct |
14 ms |
21484 KB |
Output is correct |
8 |
Correct |
15 ms |
21484 KB |
Output is correct |
9 |
Correct |
15 ms |
21484 KB |
Output is correct |
10 |
Correct |
15 ms |
21612 KB |
Output is correct |
11 |
Correct |
15 ms |
21484 KB |
Output is correct |
12 |
Correct |
15 ms |
21484 KB |
Output is correct |
13 |
Correct |
15 ms |
21484 KB |
Output is correct |
14 |
Correct |
14 ms |
21484 KB |
Output is correct |
15 |
Correct |
16 ms |
21484 KB |
Output is correct |
16 |
Correct |
17 ms |
21484 KB |
Output is correct |
17 |
Correct |
15 ms |
21484 KB |
Output is correct |
18 |
Correct |
15 ms |
21484 KB |
Output is correct |
19 |
Correct |
15 ms |
21612 KB |
Output is correct |
20 |
Correct |
14 ms |
21484 KB |
Output is correct |
21 |
Correct |
14 ms |
21484 KB |
Output is correct |
22 |
Correct |
15 ms |
21484 KB |
Output is correct |
23 |
Correct |
15 ms |
21484 KB |
Output is correct |
24 |
Correct |
15 ms |
21612 KB |
Output is correct |
25 |
Correct |
15 ms |
21484 KB |
Output is correct |
26 |
Correct |
15 ms |
21484 KB |
Output is correct |
27 |
Correct |
14 ms |
21484 KB |
Output is correct |
28 |
Correct |
15 ms |
21484 KB |
Output is correct |
29 |
Correct |
15 ms |
21484 KB |
Output is correct |
30 |
Correct |
15 ms |
21484 KB |
Output is correct |
31 |
Correct |
18 ms |
21612 KB |
Output is correct |
32 |
Correct |
17 ms |
21760 KB |
Output is correct |
33 |
Correct |
15 ms |
21612 KB |
Output is correct |
34 |
Correct |
16 ms |
21740 KB |
Output is correct |
35 |
Correct |
18 ms |
21868 KB |
Output is correct |
36 |
Correct |
17 ms |
21740 KB |
Output is correct |
37 |
Correct |
17 ms |
21868 KB |
Output is correct |
38 |
Correct |
18 ms |
21996 KB |
Output is correct |
39 |
Correct |
17 ms |
21996 KB |
Output is correct |
40 |
Correct |
16 ms |
21740 KB |
Output is correct |
41 |
Correct |
16 ms |
21740 KB |
Output is correct |
42 |
Correct |
16 ms |
21740 KB |
Output is correct |
43 |
Correct |
17 ms |
21740 KB |
Output is correct |
44 |
Correct |
17 ms |
21868 KB |
Output is correct |
45 |
Correct |
17 ms |
21868 KB |
Output is correct |
46 |
Correct |
20 ms |
21996 KB |
Output is correct |
47 |
Correct |
17 ms |
21996 KB |
Output is correct |
48 |
Correct |
18 ms |
21868 KB |
Output is correct |
49 |
Correct |
17 ms |
21948 KB |
Output is correct |
50 |
Correct |
16 ms |
21868 KB |
Output is correct |
51 |
Correct |
17 ms |
21868 KB |
Output is correct |
52 |
Correct |
19 ms |
21868 KB |
Output is correct |
53 |
Correct |
17 ms |
21868 KB |
Output is correct |
54 |
Correct |
19 ms |
22144 KB |
Output is correct |
55 |
Correct |
22 ms |
22508 KB |
Output is correct |
56 |
Correct |
43 ms |
25452 KB |
Output is correct |
57 |
Correct |
74 ms |
28140 KB |
Output is correct |
58 |
Correct |
89 ms |
29928 KB |
Output is correct |
59 |
Correct |
139 ms |
32612 KB |
Output is correct |
60 |
Correct |
156 ms |
35432 KB |
Output is correct |
61 |
Correct |
159 ms |
37732 KB |
Output is correct |
62 |
Correct |
188 ms |
39264 KB |
Output is correct |
63 |
Correct |
219 ms |
42584 KB |
Output is correct |
64 |
Correct |
231 ms |
43364 KB |
Output is correct |
65 |
Correct |
119 ms |
38228 KB |
Output is correct |
66 |
Correct |
115 ms |
38124 KB |
Output is correct |
67 |
Correct |
116 ms |
38124 KB |
Output is correct |
68 |
Correct |
183 ms |
40292 KB |
Output is correct |
69 |
Correct |
195 ms |
40208 KB |
Output is correct |
70 |
Correct |
206 ms |
40296 KB |
Output is correct |
71 |
Correct |
208 ms |
52860 KB |
Output is correct |
72 |
Correct |
212 ms |
53084 KB |
Output is correct |
73 |
Correct |
176 ms |
49116 KB |
Output is correct |
74 |
Correct |
182 ms |
49192 KB |
Output is correct |
75 |
Correct |
215 ms |
48224 KB |
Output is correct |
76 |
Correct |
176 ms |
48288 KB |
Output is correct |
77 |
Correct |
191 ms |
47616 KB |
Output is correct |
78 |
Correct |
202 ms |
46820 KB |
Output is correct |
79 |
Correct |
174 ms |
46304 KB |
Output is correct |