#include <bits/stdc++.h>
using namespace std;
int main(){
int N, M;
cin >> N >> M;
vector<int> P(N + M), C(N + M);
for (int i = 1; i < N + M; i++){
cin >> P[i] >> C[i];
P[i]--;
}
vector<vector<int>> c(N + M);
for (int i = 1; i < N + M; i++){
c[P[i]].push_back(i);
}
vector<long long> mn(N + M, 0);
vector<priority_queue<long long>> L(N + M);
vector<priority_queue<long long, vector<long long>, greater<long long>>> R(N + M);
for (int i = N; i < N + M; i++){
L[i].push(0);
R[i].push(0);
}
for (int i = N - 1; i >= 0; i--){
for (int j : c[i]){
long long x = L[j].top();
L[j].pop();
L[j].push(x + C[j]);
long long y = R[j].top();
while (!R[j].empty()){
R[j].pop();
}
R[j].push(y + C[j]);
}
for (int &j : c[i]){
if (L[j].size() > L[c[i][0]].size()){
swap(j, c[i][0]);
}
}
swap(mn[i], mn[c[i][0]]);
swap(L[i], L[c[i][0]]);
swap(R[i], R[c[i][0]]);
for (int j : c[i]){
if (j != c[i][0]){
mn[i] += mn[j];
while (!R[j].empty()){
long long a = R[j].top();
R[j].pop();
if (!L[i].empty()){
mn[i] += max(L[i].top() - a, (long long) 0);
}
L[i].push(a);
R[i].push(L[i].top());
L[i].pop();
}
while (!L[j].empty()){
long long a = L[j].top();
L[j].pop();
if (!R[i].empty()){
mn[i] += max(a - R[i].top(), (long long) 0);
}
R[i].push(a);
L[i].push(R[i].top());
R[i].pop();
}
}
}
}
cout << mn[0] << endl;
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Correct |
0 ms |
212 KB |
Output is correct |
3 |
Correct |
0 ms |
212 KB |
Output is correct |
4 |
Correct |
1 ms |
212 KB |
Output is correct |
5 |
Correct |
1 ms |
212 KB |
Output is correct |
6 |
Correct |
1 ms |
212 KB |
Output is correct |
7 |
Correct |
0 ms |
212 KB |
Output is correct |
8 |
Correct |
1 ms |
212 KB |
Output is correct |
9 |
Correct |
1 ms |
212 KB |
Output is correct |
10 |
Correct |
1 ms |
212 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
212 KB |
Output is correct |
2 |
Correct |
0 ms |
212 KB |
Output is correct |
3 |
Correct |
1 ms |
212 KB |
Output is correct |
4 |
Correct |
0 ms |
212 KB |
Output is correct |
5 |
Correct |
1 ms |
212 KB |
Output is correct |
6 |
Correct |
1 ms |
212 KB |
Output is correct |
7 |
Correct |
1 ms |
212 KB |
Output is correct |
8 |
Correct |
1 ms |
212 KB |
Output is correct |
9 |
Correct |
0 ms |
212 KB |
Output is correct |
10 |
Correct |
1 ms |
212 KB |
Output is correct |
11 |
Correct |
1 ms |
212 KB |
Output is correct |
12 |
Correct |
1 ms |
340 KB |
Output is correct |
13 |
Correct |
1 ms |
340 KB |
Output is correct |
14 |
Correct |
1 ms |
212 KB |
Output is correct |
15 |
Correct |
1 ms |
340 KB |
Output is correct |
16 |
Correct |
1 ms |
340 KB |
Output is correct |
17 |
Correct |
1 ms |
340 KB |
Output is correct |
18 |
Correct |
1 ms |
340 KB |
Output is correct |
19 |
Correct |
1 ms |
340 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Correct |
0 ms |
212 KB |
Output is correct |
3 |
Correct |
0 ms |
212 KB |
Output is correct |
4 |
Correct |
1 ms |
212 KB |
Output is correct |
5 |
Correct |
1 ms |
212 KB |
Output is correct |
6 |
Correct |
1 ms |
212 KB |
Output is correct |
7 |
Correct |
0 ms |
212 KB |
Output is correct |
8 |
Correct |
1 ms |
212 KB |
Output is correct |
9 |
Correct |
1 ms |
212 KB |
Output is correct |
10 |
Correct |
1 ms |
212 KB |
Output is correct |
11 |
Correct |
1 ms |
212 KB |
Output is correct |
12 |
Correct |
0 ms |
212 KB |
Output is correct |
13 |
Correct |
1 ms |
212 KB |
Output is correct |
14 |
Correct |
0 ms |
212 KB |
Output is correct |
15 |
Correct |
1 ms |
212 KB |
Output is correct |
16 |
Correct |
1 ms |
212 KB |
Output is correct |
17 |
Correct |
1 ms |
212 KB |
Output is correct |
18 |
Correct |
1 ms |
212 KB |
Output is correct |
19 |
Correct |
0 ms |
212 KB |
Output is correct |
20 |
Correct |
1 ms |
212 KB |
Output is correct |
21 |
Correct |
1 ms |
212 KB |
Output is correct |
22 |
Correct |
1 ms |
340 KB |
Output is correct |
23 |
Correct |
1 ms |
340 KB |
Output is correct |
24 |
Correct |
1 ms |
212 KB |
Output is correct |
25 |
Correct |
1 ms |
340 KB |
Output is correct |
26 |
Correct |
1 ms |
340 KB |
Output is correct |
27 |
Correct |
1 ms |
340 KB |
Output is correct |
28 |
Correct |
1 ms |
340 KB |
Output is correct |
29 |
Correct |
1 ms |
340 KB |
Output is correct |
30 |
Correct |
1 ms |
340 KB |
Output is correct |
31 |
Correct |
3 ms |
468 KB |
Output is correct |
32 |
Correct |
2 ms |
468 KB |
Output is correct |
33 |
Correct |
2 ms |
596 KB |
Output is correct |
34 |
Correct |
3 ms |
724 KB |
Output is correct |
35 |
Correct |
4 ms |
852 KB |
Output is correct |
36 |
Correct |
4 ms |
980 KB |
Output is correct |
37 |
Correct |
4 ms |
1108 KB |
Output is correct |
38 |
Correct |
6 ms |
1104 KB |
Output is correct |
39 |
Correct |
5 ms |
1108 KB |
Output is correct |
40 |
Correct |
4 ms |
980 KB |
Output is correct |
41 |
Correct |
4 ms |
928 KB |
Output is correct |
42 |
Correct |
4 ms |
980 KB |
Output is correct |
43 |
Correct |
5 ms |
1108 KB |
Output is correct |
44 |
Correct |
5 ms |
1108 KB |
Output is correct |
45 |
Correct |
5 ms |
1108 KB |
Output is correct |
46 |
Correct |
5 ms |
1364 KB |
Output is correct |
47 |
Correct |
6 ms |
1364 KB |
Output is correct |
48 |
Correct |
5 ms |
1236 KB |
Output is correct |
49 |
Correct |
5 ms |
1236 KB |
Output is correct |
50 |
Correct |
4 ms |
1108 KB |
Output is correct |
51 |
Correct |
5 ms |
1108 KB |
Output is correct |
52 |
Correct |
5 ms |
1236 KB |
Output is correct |
53 |
Correct |
7 ms |
1236 KB |
Output is correct |
54 |
Correct |
5 ms |
1236 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Correct |
0 ms |
212 KB |
Output is correct |
3 |
Correct |
0 ms |
212 KB |
Output is correct |
4 |
Correct |
1 ms |
212 KB |
Output is correct |
5 |
Correct |
1 ms |
212 KB |
Output is correct |
6 |
Correct |
1 ms |
212 KB |
Output is correct |
7 |
Correct |
0 ms |
212 KB |
Output is correct |
8 |
Correct |
1 ms |
212 KB |
Output is correct |
9 |
Correct |
1 ms |
212 KB |
Output is correct |
10 |
Correct |
1 ms |
212 KB |
Output is correct |
11 |
Correct |
1 ms |
212 KB |
Output is correct |
12 |
Correct |
0 ms |
212 KB |
Output is correct |
13 |
Correct |
1 ms |
212 KB |
Output is correct |
14 |
Correct |
0 ms |
212 KB |
Output is correct |
15 |
Correct |
1 ms |
212 KB |
Output is correct |
16 |
Correct |
1 ms |
212 KB |
Output is correct |
17 |
Correct |
1 ms |
212 KB |
Output is correct |
18 |
Correct |
1 ms |
212 KB |
Output is correct |
19 |
Correct |
0 ms |
212 KB |
Output is correct |
20 |
Correct |
1 ms |
212 KB |
Output is correct |
21 |
Correct |
1 ms |
212 KB |
Output is correct |
22 |
Correct |
1 ms |
340 KB |
Output is correct |
23 |
Correct |
1 ms |
340 KB |
Output is correct |
24 |
Correct |
1 ms |
212 KB |
Output is correct |
25 |
Correct |
1 ms |
340 KB |
Output is correct |
26 |
Correct |
1 ms |
340 KB |
Output is correct |
27 |
Correct |
1 ms |
340 KB |
Output is correct |
28 |
Correct |
1 ms |
340 KB |
Output is correct |
29 |
Correct |
1 ms |
340 KB |
Output is correct |
30 |
Correct |
1 ms |
340 KB |
Output is correct |
31 |
Correct |
3 ms |
468 KB |
Output is correct |
32 |
Correct |
2 ms |
468 KB |
Output is correct |
33 |
Correct |
2 ms |
596 KB |
Output is correct |
34 |
Correct |
3 ms |
724 KB |
Output is correct |
35 |
Correct |
4 ms |
852 KB |
Output is correct |
36 |
Correct |
4 ms |
980 KB |
Output is correct |
37 |
Correct |
4 ms |
1108 KB |
Output is correct |
38 |
Correct |
6 ms |
1104 KB |
Output is correct |
39 |
Correct |
5 ms |
1108 KB |
Output is correct |
40 |
Correct |
4 ms |
980 KB |
Output is correct |
41 |
Correct |
4 ms |
928 KB |
Output is correct |
42 |
Correct |
4 ms |
980 KB |
Output is correct |
43 |
Correct |
5 ms |
1108 KB |
Output is correct |
44 |
Correct |
5 ms |
1108 KB |
Output is correct |
45 |
Correct |
5 ms |
1108 KB |
Output is correct |
46 |
Correct |
5 ms |
1364 KB |
Output is correct |
47 |
Correct |
6 ms |
1364 KB |
Output is correct |
48 |
Correct |
5 ms |
1236 KB |
Output is correct |
49 |
Correct |
5 ms |
1236 KB |
Output is correct |
50 |
Correct |
4 ms |
1108 KB |
Output is correct |
51 |
Correct |
5 ms |
1108 KB |
Output is correct |
52 |
Correct |
5 ms |
1236 KB |
Output is correct |
53 |
Correct |
7 ms |
1236 KB |
Output is correct |
54 |
Correct |
5 ms |
1236 KB |
Output is correct |
55 |
Correct |
13 ms |
2516 KB |
Output is correct |
56 |
Correct |
52 ms |
8628 KB |
Output is correct |
57 |
Correct |
97 ms |
14328 KB |
Output is correct |
58 |
Correct |
116 ms |
18284 KB |
Output is correct |
59 |
Correct |
174 ms |
24332 KB |
Output is correct |
60 |
Correct |
222 ms |
32732 KB |
Output is correct |
61 |
Correct |
235 ms |
37572 KB |
Output is correct |
62 |
Correct |
289 ms |
41332 KB |
Output is correct |
63 |
Correct |
352 ms |
49328 KB |
Output is correct |
64 |
Correct |
336 ms |
51644 KB |
Output is correct |
65 |
Correct |
229 ms |
44988 KB |
Output is correct |
66 |
Correct |
247 ms |
44996 KB |
Output is correct |
67 |
Correct |
238 ms |
45248 KB |
Output is correct |
68 |
Correct |
270 ms |
51768 KB |
Output is correct |
69 |
Correct |
350 ms |
51928 KB |
Output is correct |
70 |
Correct |
321 ms |
51908 KB |
Output is correct |
71 |
Correct |
375 ms |
68756 KB |
Output is correct |
72 |
Correct |
356 ms |
68920 KB |
Output is correct |
73 |
Correct |
359 ms |
65612 KB |
Output is correct |
74 |
Correct |
338 ms |
65864 KB |
Output is correct |
75 |
Correct |
343 ms |
64544 KB |
Output is correct |
76 |
Correct |
345 ms |
64936 KB |
Output is correct |
77 |
Correct |
390 ms |
62848 KB |
Output is correct |
78 |
Correct |
344 ms |
62128 KB |
Output is correct |
79 |
Correct |
294 ms |
62392 KB |
Output is correct |