#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[c[i][0]].size()){
swap(j, c[i][0]);
}
}
swap(dp[i], dp[c[i][0]]);
for (int j : c[i]){
if (j != c[i][0]){
dp[i] += dp[j];
}
}
}
cout << dp[0].get_min() << endl;
}
# |
결과 |
실행 시간 |
메모리 |
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 |
0 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 |
0 ms |
212 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 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 |
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 |
1 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 |
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 |
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 |
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 |
0 ms |
212 KB |
Output is correct |
11 |
Correct |
1 ms |
212 KB |
Output is correct |
12 |
Correct |
1 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 |
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 |
1 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 |
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 |
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 |
852 KB |
Output is correct |
36 |
Correct |
4 ms |
980 KB |
Output is correct |
37 |
Correct |
5 ms |
1108 KB |
Output is correct |
38 |
Correct |
5 ms |
1108 KB |
Output is correct |
39 |
Correct |
5 ms |
1104 KB |
Output is correct |
40 |
Correct |
4 ms |
852 KB |
Output is correct |
41 |
Correct |
4 ms |
980 KB |
Output is correct |
42 |
Correct |
4 ms |
980 KB |
Output is correct |
43 |
Correct |
5 ms |
1032 KB |
Output is correct |
44 |
Correct |
5 ms |
1108 KB |
Output is correct |
45 |
Correct |
6 ms |
1104 KB |
Output is correct |
46 |
Correct |
7 ms |
1364 KB |
Output is correct |
47 |
Correct |
7 ms |
1272 KB |
Output is correct |
48 |
Correct |
8 ms |
1232 KB |
Output is correct |
49 |
Correct |
8 ms |
1236 KB |
Output is correct |
50 |
Correct |
6 ms |
1108 KB |
Output is correct |
51 |
Correct |
5 ms |
1108 KB |
Output is correct |
52 |
Correct |
6 ms |
1108 KB |
Output is correct |
53 |
Correct |
6 ms |
1232 KB |
Output is correct |
54 |
Correct |
7 ms |
1236 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 |
0 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 |
0 ms |
212 KB |
Output is correct |
11 |
Correct |
1 ms |
212 KB |
Output is correct |
12 |
Correct |
1 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 |
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 |
1 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 |
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 |
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 |
852 KB |
Output is correct |
36 |
Correct |
4 ms |
980 KB |
Output is correct |
37 |
Correct |
5 ms |
1108 KB |
Output is correct |
38 |
Correct |
5 ms |
1108 KB |
Output is correct |
39 |
Correct |
5 ms |
1104 KB |
Output is correct |
40 |
Correct |
4 ms |
852 KB |
Output is correct |
41 |
Correct |
4 ms |
980 KB |
Output is correct |
42 |
Correct |
4 ms |
980 KB |
Output is correct |
43 |
Correct |
5 ms |
1032 KB |
Output is correct |
44 |
Correct |
5 ms |
1108 KB |
Output is correct |
45 |
Correct |
6 ms |
1104 KB |
Output is correct |
46 |
Correct |
7 ms |
1364 KB |
Output is correct |
47 |
Correct |
7 ms |
1272 KB |
Output is correct |
48 |
Correct |
8 ms |
1232 KB |
Output is correct |
49 |
Correct |
8 ms |
1236 KB |
Output is correct |
50 |
Correct |
6 ms |
1108 KB |
Output is correct |
51 |
Correct |
5 ms |
1108 KB |
Output is correct |
52 |
Correct |
6 ms |
1108 KB |
Output is correct |
53 |
Correct |
6 ms |
1232 KB |
Output is correct |
54 |
Correct |
7 ms |
1236 KB |
Output is correct |
55 |
Correct |
15 ms |
2488 KB |
Output is correct |
56 |
Correct |
59 ms |
8652 KB |
Output is correct |
57 |
Correct |
100 ms |
14412 KB |
Output is correct |
58 |
Correct |
128 ms |
18444 KB |
Output is correct |
59 |
Correct |
152 ms |
24444 KB |
Output is correct |
60 |
Correct |
199 ms |
30300 KB |
Output is correct |
61 |
Correct |
263 ms |
34636 KB |
Output is correct |
62 |
Correct |
254 ms |
38336 KB |
Output is correct |
63 |
Correct |
315 ms |
45484 KB |
Output is correct |
64 |
Correct |
353 ms |
47352 KB |
Output is correct |
65 |
Correct |
249 ms |
40140 KB |
Output is correct |
66 |
Correct |
237 ms |
40208 KB |
Output is correct |
67 |
Correct |
248 ms |
40264 KB |
Output is correct |
68 |
Correct |
298 ms |
47004 KB |
Output is correct |
69 |
Correct |
337 ms |
47212 KB |
Output is correct |
70 |
Correct |
322 ms |
47172 KB |
Output is correct |
71 |
Correct |
392 ms |
64708 KB |
Output is correct |
72 |
Correct |
391 ms |
64832 KB |
Output is correct |
73 |
Correct |
386 ms |
61652 KB |
Output is correct |
74 |
Correct |
382 ms |
61756 KB |
Output is correct |
75 |
Correct |
371 ms |
60728 KB |
Output is correct |
76 |
Correct |
377 ms |
61180 KB |
Output is correct |
77 |
Correct |
390 ms |
58856 KB |
Output is correct |
78 |
Correct |
410 ms |
58092 KB |
Output is correct |
79 |
Correct |
314 ms |
58860 KB |
Output is correct |