#include <bits/stdc++.h>
using namespace std;
struct slope_trick{
long long mn;
priority_queue<long long> L;
priority_queue<long long, vector<long long>, greater<long long>> R;
slope_trick(){
mn = 0;
}
long long get_min(){
return mn;
}
void add_right(long long a){
if (!L.empty()){
mn += max(L.top() - a, (long long) 0);
}
L.push(a);
R.push(L.top());
L.pop();
}
void add_left(long long a){
if (!R.empty()){
mn += max(a - R.top(), (long long) 0);
}
R.push(a);
L.push(R.top());
R.pop();
}
void conv(long long a){
long long x = L.top();
L.pop();
L.push(x + a);
long long y = R.top();
while (!R.empty()){
R.pop();
}
R.push(y + a);
}
slope_trick& operator +=(slope_trick f){
mn += f.mn;
while (!f.R.empty()){
add_right(f.R.top());
f.R.pop();
}
while (!f.L.empty()){
add_left(f.L.top());
f.L.pop();
}
return *this;
}
int size(){
return L.size() + R.size();
}
};
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<slope_trick> dp(N + M);
for (int i = N; i < N + M; i++){
dp[i].add_left(0);
dp[i].add_right(0);
}
for (int i = N - 1; i >= 0; i--){
for (int j : c[i]){
dp[j].conv(C[j]);
}
for (int j : c[i]){
if (dp[j].size() > dp[i].size()){
swap(dp[i], dp[j]);
}
dp[i] += dp[j];
}
}
cout << dp[0].get_min() << endl;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Correct |
1 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 |
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 |
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 |
1 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 |
0 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 |
0 ms |
212 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 |
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 |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Correct |
1 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 |
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 |
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 |
1 ms |
212 KB |
Output is correct |
14 |
Correct |
1 ms |
212 KB |
Output is correct |
15 |
Correct |
1 ms |
212 KB |
Output is correct |
16 |
Correct |
0 ms |
212 KB |
Output is correct |
17 |
Correct |
0 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 |
340 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 |
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 |
2 ms |
468 KB |
Output is correct |
32 |
Correct |
2 ms |
468 KB |
Output is correct |
33 |
Correct |
3 ms |
596 KB |
Output is correct |
34 |
Correct |
4 ms |
724 KB |
Output is correct |
35 |
Correct |
4 ms |
844 KB |
Output is correct |
36 |
Correct |
5 ms |
980 KB |
Output is correct |
37 |
Correct |
6 ms |
1108 KB |
Output is correct |
38 |
Correct |
5 ms |
1108 KB |
Output is correct |
39 |
Correct |
7 ms |
1108 KB |
Output is correct |
40 |
Correct |
7 ms |
852 KB |
Output is correct |
41 |
Correct |
5 ms |
852 KB |
Output is correct |
42 |
Correct |
5 ms |
980 KB |
Output is correct |
43 |
Correct |
5 ms |
1108 KB |
Output is correct |
44 |
Correct |
6 ms |
1108 KB |
Output is correct |
45 |
Correct |
6 ms |
1108 KB |
Output is correct |
46 |
Correct |
7 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 |
6 ms |
1236 KB |
Output is correct |
50 |
Correct |
5 ms |
1108 KB |
Output is correct |
51 |
Correct |
5 ms |
1108 KB |
Output is correct |
52 |
Correct |
7 ms |
1236 KB |
Output is correct |
53 |
Correct |
6 ms |
1236 KB |
Output is correct |
54 |
Correct |
7 ms |
1316 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 |
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 |
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 |
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 |
1 ms |
212 KB |
Output is correct |
14 |
Correct |
1 ms |
212 KB |
Output is correct |
15 |
Correct |
1 ms |
212 KB |
Output is correct |
16 |
Correct |
0 ms |
212 KB |
Output is correct |
17 |
Correct |
0 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 |
340 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 |
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 |
2 ms |
468 KB |
Output is correct |
32 |
Correct |
2 ms |
468 KB |
Output is correct |
33 |
Correct |
3 ms |
596 KB |
Output is correct |
34 |
Correct |
4 ms |
724 KB |
Output is correct |
35 |
Correct |
4 ms |
844 KB |
Output is correct |
36 |
Correct |
5 ms |
980 KB |
Output is correct |
37 |
Correct |
6 ms |
1108 KB |
Output is correct |
38 |
Correct |
5 ms |
1108 KB |
Output is correct |
39 |
Correct |
7 ms |
1108 KB |
Output is correct |
40 |
Correct |
7 ms |
852 KB |
Output is correct |
41 |
Correct |
5 ms |
852 KB |
Output is correct |
42 |
Correct |
5 ms |
980 KB |
Output is correct |
43 |
Correct |
5 ms |
1108 KB |
Output is correct |
44 |
Correct |
6 ms |
1108 KB |
Output is correct |
45 |
Correct |
6 ms |
1108 KB |
Output is correct |
46 |
Correct |
7 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 |
6 ms |
1236 KB |
Output is correct |
50 |
Correct |
5 ms |
1108 KB |
Output is correct |
51 |
Correct |
5 ms |
1108 KB |
Output is correct |
52 |
Correct |
7 ms |
1236 KB |
Output is correct |
53 |
Correct |
6 ms |
1236 KB |
Output is correct |
54 |
Correct |
7 ms |
1316 KB |
Output is correct |
55 |
Correct |
15 ms |
2424 KB |
Output is correct |
56 |
Correct |
54 ms |
8600 KB |
Output is correct |
57 |
Correct |
90 ms |
14484 KB |
Output is correct |
58 |
Correct |
121 ms |
18476 KB |
Output is correct |
59 |
Correct |
183 ms |
24440 KB |
Output is correct |
60 |
Correct |
230 ms |
30280 KB |
Output is correct |
61 |
Correct |
243 ms |
34632 KB |
Output is correct |
62 |
Correct |
305 ms |
38260 KB |
Output is correct |
63 |
Correct |
333 ms |
45488 KB |
Output is correct |
64 |
Correct |
383 ms |
47356 KB |
Output is correct |
65 |
Correct |
253 ms |
40152 KB |
Output is correct |
66 |
Correct |
302 ms |
40140 KB |
Output is correct |
67 |
Correct |
237 ms |
40268 KB |
Output is correct |
68 |
Correct |
291 ms |
47092 KB |
Output is correct |
69 |
Correct |
352 ms |
47168 KB |
Output is correct |
70 |
Correct |
324 ms |
47172 KB |
Output is correct |
71 |
Correct |
412 ms |
65088 KB |
Output is correct |
72 |
Correct |
388 ms |
64964 KB |
Output is correct |
73 |
Correct |
392 ms |
61652 KB |
Output is correct |
74 |
Correct |
370 ms |
61756 KB |
Output is correct |
75 |
Correct |
383 ms |
60752 KB |
Output is correct |
76 |
Correct |
407 ms |
61156 KB |
Output is correct |
77 |
Correct |
413 ms |
59092 KB |
Output is correct |
78 |
Correct |
411 ms |
58088 KB |
Output is correct |
79 |
Correct |
319 ms |
58804 KB |
Output is correct |