#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]);
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 |
1 ms |
212 KB |
Output is correct |
2 |
Correct |
1 ms |
212 KB |
Output is correct |
3 |
Correct |
1 ms |
212 KB |
Output is correct |
4 |
Correct |
1 ms |
212 KB |
Output is correct |
5 |
Correct |
1 ms |
296 KB |
Output is correct |
6 |
Correct |
1 ms |
212 KB |
Output is correct |
7 |
Correct |
1 ms |
300 KB |
Output is correct |
8 |
Correct |
1 ms |
300 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 |
0 ms |
212 KB |
Output is correct |
2 |
Correct |
1 ms |
300 KB |
Output is correct |
3 |
Correct |
1 ms |
212 KB |
Output is correct |
4 |
Correct |
1 ms |
212 KB |
Output is correct |
5 |
Correct |
1 ms |
300 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 |
1 ms |
340 KB |
Output is correct |
10 |
Correct |
1 ms |
340 KB |
Output is correct |
11 |
Correct |
1 ms |
340 KB |
Output is correct |
12 |
Correct |
1 ms |
300 KB |
Output is correct |
13 |
Correct |
1 ms |
340 KB |
Output is correct |
14 |
Correct |
1 ms |
340 KB |
Output is correct |
15 |
Correct |
1 ms |
352 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 |
300 KB |
Output is correct |
19 |
Correct |
1 ms |
340 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
212 KB |
Output is correct |
2 |
Correct |
1 ms |
212 KB |
Output is correct |
3 |
Correct |
1 ms |
212 KB |
Output is correct |
4 |
Correct |
1 ms |
212 KB |
Output is correct |
5 |
Correct |
1 ms |
296 KB |
Output is correct |
6 |
Correct |
1 ms |
212 KB |
Output is correct |
7 |
Correct |
1 ms |
300 KB |
Output is correct |
8 |
Correct |
1 ms |
300 KB |
Output is correct |
9 |
Correct |
1 ms |
212 KB |
Output is correct |
10 |
Correct |
1 ms |
212 KB |
Output is correct |
11 |
Correct |
0 ms |
212 KB |
Output is correct |
12 |
Correct |
1 ms |
300 KB |
Output is correct |
13 |
Correct |
1 ms |
212 KB |
Output is correct |
14 |
Correct |
1 ms |
212 KB |
Output is correct |
15 |
Correct |
1 ms |
300 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 |
1 ms |
340 KB |
Output is correct |
20 |
Correct |
1 ms |
340 KB |
Output is correct |
21 |
Correct |
1 ms |
340 KB |
Output is correct |
22 |
Correct |
1 ms |
300 KB |
Output is correct |
23 |
Correct |
1 ms |
340 KB |
Output is correct |
24 |
Correct |
1 ms |
340 KB |
Output is correct |
25 |
Correct |
1 ms |
352 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 |
300 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 |
692 KB |
Output is correct |
32 |
Correct |
4 ms |
820 KB |
Output is correct |
33 |
Correct |
5 ms |
980 KB |
Output is correct |
34 |
Correct |
7 ms |
1364 KB |
Output is correct |
35 |
Correct |
13 ms |
2164 KB |
Output is correct |
36 |
Correct |
11 ms |
2072 KB |
Output is correct |
37 |
Correct |
12 ms |
2132 KB |
Output is correct |
38 |
Correct |
25 ms |
3612 KB |
Output is correct |
39 |
Correct |
15 ms |
2388 KB |
Output is correct |
40 |
Correct |
5 ms |
1332 KB |
Output is correct |
41 |
Correct |
6 ms |
1364 KB |
Output is correct |
42 |
Correct |
5 ms |
1304 KB |
Output is correct |
43 |
Correct |
250 ms |
31988 KB |
Output is correct |
44 |
Correct |
188 ms |
23948 KB |
Output is correct |
45 |
Correct |
194 ms |
23548 KB |
Output is correct |
46 |
Correct |
8 ms |
1364 KB |
Output is correct |
47 |
Correct |
8 ms |
1376 KB |
Output is correct |
48 |
Correct |
5 ms |
1208 KB |
Output is correct |
49 |
Correct |
6 ms |
1364 KB |
Output is correct |
50 |
Correct |
5 ms |
1152 KB |
Output is correct |
51 |
Correct |
5 ms |
1108 KB |
Output is correct |
52 |
Correct |
11 ms |
1852 KB |
Output is correct |
53 |
Correct |
7 ms |
1492 KB |
Output is correct |
54 |
Correct |
6 ms |
1364 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
212 KB |
Output is correct |
2 |
Correct |
1 ms |
212 KB |
Output is correct |
3 |
Correct |
1 ms |
212 KB |
Output is correct |
4 |
Correct |
1 ms |
212 KB |
Output is correct |
5 |
Correct |
1 ms |
296 KB |
Output is correct |
6 |
Correct |
1 ms |
212 KB |
Output is correct |
7 |
Correct |
1 ms |
300 KB |
Output is correct |
8 |
Correct |
1 ms |
300 KB |
Output is correct |
9 |
Correct |
1 ms |
212 KB |
Output is correct |
10 |
Correct |
1 ms |
212 KB |
Output is correct |
11 |
Correct |
0 ms |
212 KB |
Output is correct |
12 |
Correct |
1 ms |
300 KB |
Output is correct |
13 |
Correct |
1 ms |
212 KB |
Output is correct |
14 |
Correct |
1 ms |
212 KB |
Output is correct |
15 |
Correct |
1 ms |
300 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 |
1 ms |
340 KB |
Output is correct |
20 |
Correct |
1 ms |
340 KB |
Output is correct |
21 |
Correct |
1 ms |
340 KB |
Output is correct |
22 |
Correct |
1 ms |
300 KB |
Output is correct |
23 |
Correct |
1 ms |
340 KB |
Output is correct |
24 |
Correct |
1 ms |
340 KB |
Output is correct |
25 |
Correct |
1 ms |
352 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 |
300 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 |
692 KB |
Output is correct |
32 |
Correct |
4 ms |
820 KB |
Output is correct |
33 |
Correct |
5 ms |
980 KB |
Output is correct |
34 |
Correct |
7 ms |
1364 KB |
Output is correct |
35 |
Correct |
13 ms |
2164 KB |
Output is correct |
36 |
Correct |
11 ms |
2072 KB |
Output is correct |
37 |
Correct |
12 ms |
2132 KB |
Output is correct |
38 |
Correct |
25 ms |
3612 KB |
Output is correct |
39 |
Correct |
15 ms |
2388 KB |
Output is correct |
40 |
Correct |
5 ms |
1332 KB |
Output is correct |
41 |
Correct |
6 ms |
1364 KB |
Output is correct |
42 |
Correct |
5 ms |
1304 KB |
Output is correct |
43 |
Correct |
250 ms |
31988 KB |
Output is correct |
44 |
Correct |
188 ms |
23948 KB |
Output is correct |
45 |
Correct |
194 ms |
23548 KB |
Output is correct |
46 |
Correct |
8 ms |
1364 KB |
Output is correct |
47 |
Correct |
8 ms |
1376 KB |
Output is correct |
48 |
Correct |
5 ms |
1208 KB |
Output is correct |
49 |
Correct |
6 ms |
1364 KB |
Output is correct |
50 |
Correct |
5 ms |
1152 KB |
Output is correct |
51 |
Correct |
5 ms |
1108 KB |
Output is correct |
52 |
Correct |
11 ms |
1852 KB |
Output is correct |
53 |
Correct |
7 ms |
1492 KB |
Output is correct |
54 |
Correct |
6 ms |
1364 KB |
Output is correct |
55 |
Correct |
59 ms |
8732 KB |
Output is correct |
56 |
Correct |
554 ms |
59840 KB |
Output is correct |
57 |
Correct |
1103 ms |
116316 KB |
Output is correct |
58 |
Correct |
1294 ms |
141876 KB |
Output is correct |
59 |
Execution timed out |
2081 ms |
220760 KB |
Time limit exceeded |
60 |
Halted |
0 ms |
0 KB |
- |