#include <bits/stdc++.h>
#define DEBUG false
using namespace std;
const int MAX_N = 3e5 + 10;
int P[MAX_N], C[MAX_N];
class SlopeTrick {
public :
long long m, c;
priority_queue <long long> *pq;
SlopeTrick() : m(0), c(0), pq(new priority_queue <long long>) {}
SlopeTrick operator+(const SlopeTrick &o) const {
SlopeTrick result;
result.m = m + o.m;
result.c = c + o.c;
if(pq->size() > o.pq->size()) {
result.pq = pq;
while(!o.pq->empty()) {
result.pq->push(o.pq->top());
o.pq->pop();
}
}
else {
result.pq = o.pq;
while(!pq->empty()) {
result.pq->push(pq->top());
pq->pop();
}
}
return result;
}
}D[MAX_N];
int main() {
cin.tie(nullptr)->sync_with_stdio(false);
int N, M;
cin >> N >> M;
for(int i = 2; i <= N + M; i++) {
cin >> P[i] >> C[i];
}
for(int i = N + 1; i <= N + M; i++) {
D[i].m = 1;
D[i].c = -C[i];
D[i].pq->push(C[i]);
D[i].pq->push(C[i]);
D[P[i]] = D[P[i]] + D[i];
}
for(int i = N; i >= 2; i--) {
while(D[i].m > 1) {
D[i].m--;
D[i].c += D[i].pq->top();
D[i].pq->pop();
}
long long slope1 = D[i].pq->top();
D[i].pq->pop();
long long slope0 = D[i].pq->top();
D[i].pq->pop();
D[i].pq->push(slope1 + C[i]);
D[i].pq->push(slope0 + C[i]);
D[i].c -= C[i];
D[P[i]] = D[P[i]] + D[i];
}
while(D[1].m > 0) {
D[1].m--;
D[1].c += D[1].pq->top();
D[1].pq->pop();
}
cout << D[1].c;
return 0;
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
15 ms |
21460 KB |
Output is correct |
2 |
Correct |
15 ms |
21372 KB |
Output is correct |
3 |
Correct |
15 ms |
21420 KB |
Output is correct |
4 |
Correct |
15 ms |
21460 KB |
Output is correct |
5 |
Correct |
19 ms |
21444 KB |
Output is correct |
6 |
Correct |
16 ms |
21460 KB |
Output is correct |
7 |
Correct |
16 ms |
21500 KB |
Output is correct |
8 |
Correct |
16 ms |
21408 KB |
Output is correct |
9 |
Correct |
17 ms |
21464 KB |
Output is correct |
10 |
Correct |
14 ms |
21416 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
15 ms |
21464 KB |
Output is correct |
2 |
Correct |
16 ms |
21440 KB |
Output is correct |
3 |
Correct |
15 ms |
21468 KB |
Output is correct |
4 |
Correct |
15 ms |
21460 KB |
Output is correct |
5 |
Correct |
16 ms |
21460 KB |
Output is correct |
6 |
Correct |
15 ms |
21408 KB |
Output is correct |
7 |
Correct |
16 ms |
21460 KB |
Output is correct |
8 |
Correct |
15 ms |
21460 KB |
Output is correct |
9 |
Correct |
14 ms |
21452 KB |
Output is correct |
10 |
Correct |
16 ms |
21456 KB |
Output is correct |
11 |
Correct |
16 ms |
21420 KB |
Output is correct |
12 |
Correct |
14 ms |
21464 KB |
Output is correct |
13 |
Correct |
16 ms |
21464 KB |
Output is correct |
14 |
Correct |
22 ms |
21460 KB |
Output is correct |
15 |
Correct |
18 ms |
21460 KB |
Output is correct |
16 |
Correct |
16 ms |
21468 KB |
Output is correct |
17 |
Correct |
23 ms |
21432 KB |
Output is correct |
18 |
Correct |
23 ms |
21468 KB |
Output is correct |
19 |
Correct |
22 ms |
21408 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
15 ms |
21460 KB |
Output is correct |
2 |
Correct |
15 ms |
21372 KB |
Output is correct |
3 |
Correct |
15 ms |
21420 KB |
Output is correct |
4 |
Correct |
15 ms |
21460 KB |
Output is correct |
5 |
Correct |
19 ms |
21444 KB |
Output is correct |
6 |
Correct |
16 ms |
21460 KB |
Output is correct |
7 |
Correct |
16 ms |
21500 KB |
Output is correct |
8 |
Correct |
16 ms |
21408 KB |
Output is correct |
9 |
Correct |
17 ms |
21464 KB |
Output is correct |
10 |
Correct |
14 ms |
21416 KB |
Output is correct |
11 |
Correct |
15 ms |
21464 KB |
Output is correct |
12 |
Correct |
16 ms |
21440 KB |
Output is correct |
13 |
Correct |
15 ms |
21468 KB |
Output is correct |
14 |
Correct |
15 ms |
21460 KB |
Output is correct |
15 |
Correct |
16 ms |
21460 KB |
Output is correct |
16 |
Correct |
15 ms |
21408 KB |
Output is correct |
17 |
Correct |
16 ms |
21460 KB |
Output is correct |
18 |
Correct |
15 ms |
21460 KB |
Output is correct |
19 |
Correct |
14 ms |
21452 KB |
Output is correct |
20 |
Correct |
16 ms |
21456 KB |
Output is correct |
21 |
Correct |
16 ms |
21420 KB |
Output is correct |
22 |
Correct |
14 ms |
21464 KB |
Output is correct |
23 |
Correct |
16 ms |
21464 KB |
Output is correct |
24 |
Correct |
22 ms |
21460 KB |
Output is correct |
25 |
Correct |
18 ms |
21460 KB |
Output is correct |
26 |
Correct |
16 ms |
21468 KB |
Output is correct |
27 |
Correct |
23 ms |
21432 KB |
Output is correct |
28 |
Correct |
23 ms |
21468 KB |
Output is correct |
29 |
Correct |
22 ms |
21408 KB |
Output is correct |
30 |
Correct |
16 ms |
21460 KB |
Output is correct |
31 |
Correct |
16 ms |
21488 KB |
Output is correct |
32 |
Correct |
18 ms |
21732 KB |
Output is correct |
33 |
Correct |
21 ms |
21676 KB |
Output is correct |
34 |
Correct |
17 ms |
21736 KB |
Output is correct |
35 |
Correct |
22 ms |
21716 KB |
Output is correct |
36 |
Correct |
17 ms |
21844 KB |
Output is correct |
37 |
Correct |
19 ms |
21920 KB |
Output is correct |
38 |
Correct |
18 ms |
21868 KB |
Output is correct |
39 |
Correct |
17 ms |
21860 KB |
Output is correct |
40 |
Correct |
17 ms |
21740 KB |
Output is correct |
41 |
Correct |
17 ms |
21716 KB |
Output is correct |
42 |
Correct |
17 ms |
21820 KB |
Output is correct |
43 |
Correct |
17 ms |
21856 KB |
Output is correct |
44 |
Correct |
18 ms |
21900 KB |
Output is correct |
45 |
Correct |
17 ms |
21916 KB |
Output is correct |
46 |
Correct |
19 ms |
22100 KB |
Output is correct |
47 |
Correct |
18 ms |
22104 KB |
Output is correct |
48 |
Correct |
18 ms |
21988 KB |
Output is correct |
49 |
Correct |
19 ms |
22092 KB |
Output is correct |
50 |
Correct |
18 ms |
21968 KB |
Output is correct |
51 |
Correct |
16 ms |
21956 KB |
Output is correct |
52 |
Correct |
17 ms |
22092 KB |
Output is correct |
53 |
Correct |
16 ms |
21964 KB |
Output is correct |
54 |
Correct |
20 ms |
21964 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
15 ms |
21460 KB |
Output is correct |
2 |
Correct |
15 ms |
21372 KB |
Output is correct |
3 |
Correct |
15 ms |
21420 KB |
Output is correct |
4 |
Correct |
15 ms |
21460 KB |
Output is correct |
5 |
Correct |
19 ms |
21444 KB |
Output is correct |
6 |
Correct |
16 ms |
21460 KB |
Output is correct |
7 |
Correct |
16 ms |
21500 KB |
Output is correct |
8 |
Correct |
16 ms |
21408 KB |
Output is correct |
9 |
Correct |
17 ms |
21464 KB |
Output is correct |
10 |
Correct |
14 ms |
21416 KB |
Output is correct |
11 |
Correct |
15 ms |
21464 KB |
Output is correct |
12 |
Correct |
16 ms |
21440 KB |
Output is correct |
13 |
Correct |
15 ms |
21468 KB |
Output is correct |
14 |
Correct |
15 ms |
21460 KB |
Output is correct |
15 |
Correct |
16 ms |
21460 KB |
Output is correct |
16 |
Correct |
15 ms |
21408 KB |
Output is correct |
17 |
Correct |
16 ms |
21460 KB |
Output is correct |
18 |
Correct |
15 ms |
21460 KB |
Output is correct |
19 |
Correct |
14 ms |
21452 KB |
Output is correct |
20 |
Correct |
16 ms |
21456 KB |
Output is correct |
21 |
Correct |
16 ms |
21420 KB |
Output is correct |
22 |
Correct |
14 ms |
21464 KB |
Output is correct |
23 |
Correct |
16 ms |
21464 KB |
Output is correct |
24 |
Correct |
22 ms |
21460 KB |
Output is correct |
25 |
Correct |
18 ms |
21460 KB |
Output is correct |
26 |
Correct |
16 ms |
21468 KB |
Output is correct |
27 |
Correct |
23 ms |
21432 KB |
Output is correct |
28 |
Correct |
23 ms |
21468 KB |
Output is correct |
29 |
Correct |
22 ms |
21408 KB |
Output is correct |
30 |
Correct |
16 ms |
21460 KB |
Output is correct |
31 |
Correct |
16 ms |
21488 KB |
Output is correct |
32 |
Correct |
18 ms |
21732 KB |
Output is correct |
33 |
Correct |
21 ms |
21676 KB |
Output is correct |
34 |
Correct |
17 ms |
21736 KB |
Output is correct |
35 |
Correct |
22 ms |
21716 KB |
Output is correct |
36 |
Correct |
17 ms |
21844 KB |
Output is correct |
37 |
Correct |
19 ms |
21920 KB |
Output is correct |
38 |
Correct |
18 ms |
21868 KB |
Output is correct |
39 |
Correct |
17 ms |
21860 KB |
Output is correct |
40 |
Correct |
17 ms |
21740 KB |
Output is correct |
41 |
Correct |
17 ms |
21716 KB |
Output is correct |
42 |
Correct |
17 ms |
21820 KB |
Output is correct |
43 |
Correct |
17 ms |
21856 KB |
Output is correct |
44 |
Correct |
18 ms |
21900 KB |
Output is correct |
45 |
Correct |
17 ms |
21916 KB |
Output is correct |
46 |
Correct |
19 ms |
22100 KB |
Output is correct |
47 |
Correct |
18 ms |
22104 KB |
Output is correct |
48 |
Correct |
18 ms |
21988 KB |
Output is correct |
49 |
Correct |
19 ms |
22092 KB |
Output is correct |
50 |
Correct |
18 ms |
21968 KB |
Output is correct |
51 |
Correct |
16 ms |
21956 KB |
Output is correct |
52 |
Correct |
17 ms |
22092 KB |
Output is correct |
53 |
Correct |
16 ms |
21964 KB |
Output is correct |
54 |
Correct |
20 ms |
21964 KB |
Output is correct |
55 |
Correct |
21 ms |
22732 KB |
Output is correct |
56 |
Correct |
36 ms |
26572 KB |
Output is correct |
57 |
Correct |
57 ms |
30172 KB |
Output is correct |
58 |
Correct |
72 ms |
32588 KB |
Output is correct |
59 |
Correct |
88 ms |
36188 KB |
Output is correct |
60 |
Correct |
116 ms |
39748 KB |
Output is correct |
61 |
Correct |
125 ms |
42552 KB |
Output is correct |
62 |
Correct |
138 ms |
44724 KB |
Output is correct |
63 |
Correct |
165 ms |
49208 KB |
Output is correct |
64 |
Correct |
176 ms |
50436 KB |
Output is correct |
65 |
Correct |
87 ms |
42756 KB |
Output is correct |
66 |
Correct |
87 ms |
42740 KB |
Output is correct |
67 |
Correct |
86 ms |
42692 KB |
Output is correct |
68 |
Correct |
141 ms |
48492 KB |
Output is correct |
69 |
Correct |
158 ms |
49192 KB |
Output is correct |
70 |
Correct |
156 ms |
49260 KB |
Output is correct |
71 |
Correct |
175 ms |
64612 KB |
Output is correct |
72 |
Correct |
176 ms |
64696 KB |
Output is correct |
73 |
Correct |
155 ms |
61408 KB |
Output is correct |
74 |
Correct |
159 ms |
61924 KB |
Output is correct |
75 |
Correct |
153 ms |
60180 KB |
Output is correct |
76 |
Correct |
156 ms |
60304 KB |
Output is correct |
77 |
Correct |
169 ms |
59568 KB |
Output is correct |
78 |
Correct |
165 ms |
58648 KB |
Output is correct |
79 |
Correct |
172 ms |
55936 KB |
Output is correct |