#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() + R[j].size() > L[i].size() + R[i].size()){
swap(mn[i], mn[j]);
swap(L[i], L[j]);
swap(R[i], R[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;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 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 |
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 |
0 ms |
212 KB |
Output is correct |
9 |
Correct |
1 ms |
212 KB |
Output is correct |
10 |
Correct |
0 ms |
212 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
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 |
0 ms |
212 KB |
Output is correct |
5 |
Correct |
0 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 |
1 ms |
212 KB |
Output is correct |
10 |
Correct |
1 ms |
212 KB |
Output is correct |
11 |
Correct |
1 ms |
340 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 |
340 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 |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 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 |
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 |
0 ms |
212 KB |
Output is correct |
9 |
Correct |
1 ms |
212 KB |
Output is correct |
10 |
Correct |
0 ms |
212 KB |
Output is correct |
11 |
Correct |
0 ms |
212 KB |
Output is correct |
12 |
Correct |
0 ms |
212 KB |
Output is correct |
13 |
Correct |
0 ms |
212 KB |
Output is correct |
14 |
Correct |
0 ms |
212 KB |
Output is correct |
15 |
Correct |
0 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 |
1 ms |
212 KB |
Output is correct |
20 |
Correct |
1 ms |
212 KB |
Output is correct |
21 |
Correct |
1 ms |
340 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 |
340 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 |
2 ms |
432 KB |
Output is correct |
32 |
Correct |
2 ms |
468 KB |
Output is correct |
33 |
Correct |
3 ms |
596 KB |
Output is correct |
34 |
Correct |
3 ms |
724 KB |
Output is correct |
35 |
Correct |
6 ms |
852 KB |
Output is correct |
36 |
Correct |
7 ms |
972 KB |
Output is correct |
37 |
Correct |
5 ms |
1092 KB |
Output is correct |
38 |
Correct |
6 ms |
1108 KB |
Output is correct |
39 |
Correct |
7 ms |
1108 KB |
Output is correct |
40 |
Correct |
7 ms |
980 KB |
Output is correct |
41 |
Correct |
4 ms |
980 KB |
Output is correct |
42 |
Correct |
7 ms |
980 KB |
Output is correct |
43 |
Correct |
7 ms |
1128 KB |
Output is correct |
44 |
Correct |
5 ms |
1108 KB |
Output is correct |
45 |
Correct |
5 ms |
1108 KB |
Output is correct |
46 |
Correct |
6 ms |
1364 KB |
Output is correct |
47 |
Correct |
7 ms |
1364 KB |
Output is correct |
48 |
Correct |
5 ms |
1236 KB |
Output is correct |
49 |
Correct |
8 ms |
1236 KB |
Output is correct |
50 |
Correct |
5 ms |
1108 KB |
Output is correct |
51 |
Correct |
8 ms |
1108 KB |
Output is correct |
52 |
Correct |
5 ms |
1236 KB |
Output is correct |
53 |
Correct |
6 ms |
1236 KB |
Output is correct |
54 |
Correct |
8 ms |
1236 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 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 |
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 |
0 ms |
212 KB |
Output is correct |
9 |
Correct |
1 ms |
212 KB |
Output is correct |
10 |
Correct |
0 ms |
212 KB |
Output is correct |
11 |
Correct |
0 ms |
212 KB |
Output is correct |
12 |
Correct |
0 ms |
212 KB |
Output is correct |
13 |
Correct |
0 ms |
212 KB |
Output is correct |
14 |
Correct |
0 ms |
212 KB |
Output is correct |
15 |
Correct |
0 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 |
1 ms |
212 KB |
Output is correct |
20 |
Correct |
1 ms |
212 KB |
Output is correct |
21 |
Correct |
1 ms |
340 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 |
340 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 |
2 ms |
432 KB |
Output is correct |
32 |
Correct |
2 ms |
468 KB |
Output is correct |
33 |
Correct |
3 ms |
596 KB |
Output is correct |
34 |
Correct |
3 ms |
724 KB |
Output is correct |
35 |
Correct |
6 ms |
852 KB |
Output is correct |
36 |
Correct |
7 ms |
972 KB |
Output is correct |
37 |
Correct |
5 ms |
1092 KB |
Output is correct |
38 |
Correct |
6 ms |
1108 KB |
Output is correct |
39 |
Correct |
7 ms |
1108 KB |
Output is correct |
40 |
Correct |
7 ms |
980 KB |
Output is correct |
41 |
Correct |
4 ms |
980 KB |
Output is correct |
42 |
Correct |
7 ms |
980 KB |
Output is correct |
43 |
Correct |
7 ms |
1128 KB |
Output is correct |
44 |
Correct |
5 ms |
1108 KB |
Output is correct |
45 |
Correct |
5 ms |
1108 KB |
Output is correct |
46 |
Correct |
6 ms |
1364 KB |
Output is correct |
47 |
Correct |
7 ms |
1364 KB |
Output is correct |
48 |
Correct |
5 ms |
1236 KB |
Output is correct |
49 |
Correct |
8 ms |
1236 KB |
Output is correct |
50 |
Correct |
5 ms |
1108 KB |
Output is correct |
51 |
Correct |
8 ms |
1108 KB |
Output is correct |
52 |
Correct |
5 ms |
1236 KB |
Output is correct |
53 |
Correct |
6 ms |
1236 KB |
Output is correct |
54 |
Correct |
8 ms |
1236 KB |
Output is correct |
55 |
Correct |
12 ms |
2388 KB |
Output is correct |
56 |
Correct |
59 ms |
8620 KB |
Output is correct |
57 |
Correct |
106 ms |
14332 KB |
Output is correct |
58 |
Correct |
133 ms |
18276 KB |
Output is correct |
59 |
Correct |
167 ms |
24264 KB |
Output is correct |
60 |
Correct |
256 ms |
29976 KB |
Output is correct |
61 |
Correct |
282 ms |
34388 KB |
Output is correct |
62 |
Correct |
302 ms |
37848 KB |
Output is correct |
63 |
Correct |
346 ms |
45152 KB |
Output is correct |
64 |
Correct |
392 ms |
47108 KB |
Output is correct |
65 |
Correct |
322 ms |
40208 KB |
Output is correct |
66 |
Correct |
275 ms |
40144 KB |
Output is correct |
67 |
Correct |
283 ms |
40268 KB |
Output is correct |
68 |
Correct |
333 ms |
47044 KB |
Output is correct |
69 |
Correct |
333 ms |
47176 KB |
Output is correct |
70 |
Correct |
403 ms |
47300 KB |
Output is correct |
71 |
Correct |
411 ms |
64876 KB |
Output is correct |
72 |
Correct |
401 ms |
64736 KB |
Output is correct |
73 |
Correct |
395 ms |
61536 KB |
Output is correct |
74 |
Correct |
360 ms |
61852 KB |
Output is correct |
75 |
Correct |
422 ms |
60752 KB |
Output is correct |
76 |
Correct |
380 ms |
61052 KB |
Output is correct |
77 |
Correct |
395 ms |
58892 KB |
Output is correct |
78 |
Correct |
352 ms |
57988 KB |
Output is correct |
79 |
Correct |
305 ms |
58808 KB |
Output is correct |