#include <bits/stdc++.h>
using namespace std;
using lint = long long;
using pint = pair<int, int>;
#define x first
#define y second
const int maxn = 300004;
int N, M;
vector<pint> g[maxn];
struct func {
priority_queue<lint> neg;
priority_queue<lint, vector<lint>, greater<lint> > pos;
lint mn = 0;
void balance() {
while (neg.top() > pos.top()) {
lint x = neg.top(), y = pos.top();
neg.pop(); pos.pop();
pos.push(x); neg.push(y);
mn += x - y;
}
}
void add(lint a, lint b, lint c) {
neg.push(a);
pos.push(b);
mn += c;
balance();
}
void process(lint x) {
lint b = neg.top() + x, c = pos.top() + x;
while (!pos.empty()) pos.pop();
neg.pop();
neg.push(b);
pos.push(c);
}
} *b[maxn];
void dfs(int x) {
func *f = new func();
for (pint i : g[x]) if (!g[i.x].empty()) {
dfs(i.x);
b[i.x]->process(i.y);
if (f->neg.size() < b[i.x]->neg.size()) f = b[i.x];
}
b[x] = f;
for (pint i : g[x]) if (f != b[i.x]) {
if (!g[i.x].empty()) {
func *g = b[i.x];
while (!g->neg.empty()) f->neg.push(g->neg.top()), g->neg.pop();
while (!g->pos.empty()) f->pos.push(g->pos.top()), g->pos.pop();
f->mn += g->mn;
f->balance();
} else f->add(i.y, i.y, 0);
}
}
int main() {
ios::sync_with_stdio(0); cin.tie(0);
cin >> N >> M;
for (int i=2; i<=N+M; i++) {
int x, y;
cin >> x >> y;
g[x].emplace_back(i, y);
}
dfs(1);
cout << b[1]->mn << '\n';
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
9 ms |
7416 KB |
Output is correct |
2 |
Correct |
11 ms |
7416 KB |
Output is correct |
3 |
Correct |
9 ms |
7416 KB |
Output is correct |
4 |
Correct |
10 ms |
7416 KB |
Output is correct |
5 |
Correct |
9 ms |
7416 KB |
Output is correct |
6 |
Correct |
9 ms |
7288 KB |
Output is correct |
7 |
Correct |
9 ms |
7416 KB |
Output is correct |
8 |
Correct |
10 ms |
7420 KB |
Output is correct |
9 |
Correct |
9 ms |
7416 KB |
Output is correct |
10 |
Correct |
9 ms |
7416 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
9 ms |
7416 KB |
Output is correct |
2 |
Correct |
9 ms |
7416 KB |
Output is correct |
3 |
Correct |
9 ms |
7416 KB |
Output is correct |
4 |
Correct |
9 ms |
7416 KB |
Output is correct |
5 |
Correct |
9 ms |
7420 KB |
Output is correct |
6 |
Correct |
10 ms |
7416 KB |
Output is correct |
7 |
Correct |
10 ms |
7416 KB |
Output is correct |
8 |
Correct |
11 ms |
7416 KB |
Output is correct |
9 |
Correct |
9 ms |
7416 KB |
Output is correct |
10 |
Correct |
9 ms |
7416 KB |
Output is correct |
11 |
Correct |
11 ms |
7416 KB |
Output is correct |
12 |
Correct |
9 ms |
7416 KB |
Output is correct |
13 |
Correct |
9 ms |
7416 KB |
Output is correct |
14 |
Correct |
9 ms |
7416 KB |
Output is correct |
15 |
Correct |
9 ms |
7416 KB |
Output is correct |
16 |
Correct |
9 ms |
7416 KB |
Output is correct |
17 |
Correct |
10 ms |
7416 KB |
Output is correct |
18 |
Correct |
9 ms |
7416 KB |
Output is correct |
19 |
Correct |
10 ms |
7416 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
9 ms |
7416 KB |
Output is correct |
2 |
Correct |
11 ms |
7416 KB |
Output is correct |
3 |
Correct |
9 ms |
7416 KB |
Output is correct |
4 |
Correct |
10 ms |
7416 KB |
Output is correct |
5 |
Correct |
9 ms |
7416 KB |
Output is correct |
6 |
Correct |
9 ms |
7288 KB |
Output is correct |
7 |
Correct |
9 ms |
7416 KB |
Output is correct |
8 |
Correct |
10 ms |
7420 KB |
Output is correct |
9 |
Correct |
9 ms |
7416 KB |
Output is correct |
10 |
Correct |
9 ms |
7416 KB |
Output is correct |
11 |
Correct |
9 ms |
7416 KB |
Output is correct |
12 |
Correct |
9 ms |
7416 KB |
Output is correct |
13 |
Correct |
9 ms |
7416 KB |
Output is correct |
14 |
Correct |
9 ms |
7416 KB |
Output is correct |
15 |
Correct |
9 ms |
7420 KB |
Output is correct |
16 |
Correct |
10 ms |
7416 KB |
Output is correct |
17 |
Correct |
10 ms |
7416 KB |
Output is correct |
18 |
Correct |
11 ms |
7416 KB |
Output is correct |
19 |
Correct |
9 ms |
7416 KB |
Output is correct |
20 |
Correct |
9 ms |
7416 KB |
Output is correct |
21 |
Correct |
11 ms |
7416 KB |
Output is correct |
22 |
Correct |
9 ms |
7416 KB |
Output is correct |
23 |
Correct |
9 ms |
7416 KB |
Output is correct |
24 |
Correct |
9 ms |
7416 KB |
Output is correct |
25 |
Correct |
9 ms |
7416 KB |
Output is correct |
26 |
Correct |
9 ms |
7416 KB |
Output is correct |
27 |
Correct |
10 ms |
7416 KB |
Output is correct |
28 |
Correct |
9 ms |
7416 KB |
Output is correct |
29 |
Correct |
10 ms |
7416 KB |
Output is correct |
30 |
Correct |
10 ms |
7416 KB |
Output is correct |
31 |
Correct |
10 ms |
7544 KB |
Output is correct |
32 |
Correct |
10 ms |
7544 KB |
Output is correct |
33 |
Correct |
12 ms |
7720 KB |
Output is correct |
34 |
Correct |
12 ms |
7672 KB |
Output is correct |
35 |
Correct |
11 ms |
7800 KB |
Output is correct |
36 |
Correct |
12 ms |
7800 KB |
Output is correct |
37 |
Correct |
12 ms |
7928 KB |
Output is correct |
38 |
Correct |
12 ms |
7928 KB |
Output is correct |
39 |
Correct |
12 ms |
7928 KB |
Output is correct |
40 |
Correct |
12 ms |
8696 KB |
Output is correct |
41 |
Correct |
12 ms |
8696 KB |
Output is correct |
42 |
Correct |
12 ms |
8056 KB |
Output is correct |
43 |
Correct |
12 ms |
8184 KB |
Output is correct |
44 |
Correct |
12 ms |
8056 KB |
Output is correct |
45 |
Correct |
12 ms |
8056 KB |
Output is correct |
46 |
Correct |
13 ms |
7672 KB |
Output is correct |
47 |
Correct |
12 ms |
7672 KB |
Output is correct |
48 |
Correct |
12 ms |
7672 KB |
Output is correct |
49 |
Correct |
13 ms |
7672 KB |
Output is correct |
50 |
Correct |
12 ms |
7672 KB |
Output is correct |
51 |
Correct |
11 ms |
7672 KB |
Output is correct |
52 |
Correct |
12 ms |
7672 KB |
Output is correct |
53 |
Correct |
12 ms |
7672 KB |
Output is correct |
54 |
Correct |
12 ms |
7672 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
9 ms |
7416 KB |
Output is correct |
2 |
Correct |
11 ms |
7416 KB |
Output is correct |
3 |
Correct |
9 ms |
7416 KB |
Output is correct |
4 |
Correct |
10 ms |
7416 KB |
Output is correct |
5 |
Correct |
9 ms |
7416 KB |
Output is correct |
6 |
Correct |
9 ms |
7288 KB |
Output is correct |
7 |
Correct |
9 ms |
7416 KB |
Output is correct |
8 |
Correct |
10 ms |
7420 KB |
Output is correct |
9 |
Correct |
9 ms |
7416 KB |
Output is correct |
10 |
Correct |
9 ms |
7416 KB |
Output is correct |
11 |
Correct |
9 ms |
7416 KB |
Output is correct |
12 |
Correct |
9 ms |
7416 KB |
Output is correct |
13 |
Correct |
9 ms |
7416 KB |
Output is correct |
14 |
Correct |
9 ms |
7416 KB |
Output is correct |
15 |
Correct |
9 ms |
7420 KB |
Output is correct |
16 |
Correct |
10 ms |
7416 KB |
Output is correct |
17 |
Correct |
10 ms |
7416 KB |
Output is correct |
18 |
Correct |
11 ms |
7416 KB |
Output is correct |
19 |
Correct |
9 ms |
7416 KB |
Output is correct |
20 |
Correct |
9 ms |
7416 KB |
Output is correct |
21 |
Correct |
11 ms |
7416 KB |
Output is correct |
22 |
Correct |
9 ms |
7416 KB |
Output is correct |
23 |
Correct |
9 ms |
7416 KB |
Output is correct |
24 |
Correct |
9 ms |
7416 KB |
Output is correct |
25 |
Correct |
9 ms |
7416 KB |
Output is correct |
26 |
Correct |
9 ms |
7416 KB |
Output is correct |
27 |
Correct |
10 ms |
7416 KB |
Output is correct |
28 |
Correct |
9 ms |
7416 KB |
Output is correct |
29 |
Correct |
10 ms |
7416 KB |
Output is correct |
30 |
Correct |
10 ms |
7416 KB |
Output is correct |
31 |
Correct |
10 ms |
7544 KB |
Output is correct |
32 |
Correct |
10 ms |
7544 KB |
Output is correct |
33 |
Correct |
12 ms |
7720 KB |
Output is correct |
34 |
Correct |
12 ms |
7672 KB |
Output is correct |
35 |
Correct |
11 ms |
7800 KB |
Output is correct |
36 |
Correct |
12 ms |
7800 KB |
Output is correct |
37 |
Correct |
12 ms |
7928 KB |
Output is correct |
38 |
Correct |
12 ms |
7928 KB |
Output is correct |
39 |
Correct |
12 ms |
7928 KB |
Output is correct |
40 |
Correct |
12 ms |
8696 KB |
Output is correct |
41 |
Correct |
12 ms |
8696 KB |
Output is correct |
42 |
Correct |
12 ms |
8056 KB |
Output is correct |
43 |
Correct |
12 ms |
8184 KB |
Output is correct |
44 |
Correct |
12 ms |
8056 KB |
Output is correct |
45 |
Correct |
12 ms |
8056 KB |
Output is correct |
46 |
Correct |
13 ms |
7672 KB |
Output is correct |
47 |
Correct |
12 ms |
7672 KB |
Output is correct |
48 |
Correct |
12 ms |
7672 KB |
Output is correct |
49 |
Correct |
13 ms |
7672 KB |
Output is correct |
50 |
Correct |
12 ms |
7672 KB |
Output is correct |
51 |
Correct |
11 ms |
7672 KB |
Output is correct |
52 |
Correct |
12 ms |
7672 KB |
Output is correct |
53 |
Correct |
12 ms |
7672 KB |
Output is correct |
54 |
Correct |
12 ms |
7672 KB |
Output is correct |
55 |
Correct |
18 ms |
8824 KB |
Output is correct |
56 |
Correct |
41 ms |
13044 KB |
Output is correct |
57 |
Correct |
70 ms |
17268 KB |
Output is correct |
58 |
Correct |
88 ms |
19820 KB |
Output is correct |
59 |
Correct |
121 ms |
23920 KB |
Output is correct |
60 |
Correct |
152 ms |
27988 KB |
Output is correct |
61 |
Correct |
176 ms |
30956 KB |
Output is correct |
62 |
Correct |
204 ms |
33516 KB |
Output is correct |
63 |
Correct |
243 ms |
38636 KB |
Output is correct |
64 |
Correct |
253 ms |
40172 KB |
Output is correct |
65 |
Correct |
174 ms |
89720 KB |
Output is correct |
66 |
Correct |
160 ms |
89720 KB |
Output is correct |
67 |
Correct |
164 ms |
47632 KB |
Output is correct |
68 |
Correct |
168 ms |
52972 KB |
Output is correct |
69 |
Correct |
203 ms |
50476 KB |
Output is correct |
70 |
Correct |
207 ms |
50412 KB |
Output is correct |
71 |
Correct |
220 ms |
28736 KB |
Output is correct |
72 |
Correct |
220 ms |
28652 KB |
Output is correct |
73 |
Correct |
206 ms |
25164 KB |
Output is correct |
74 |
Correct |
200 ms |
25212 KB |
Output is correct |
75 |
Correct |
207 ms |
24036 KB |
Output is correct |
76 |
Correct |
211 ms |
24240 KB |
Output is correct |
77 |
Correct |
215 ms |
23460 KB |
Output is correct |
78 |
Correct |
221 ms |
22500 KB |
Output is correct |
79 |
Correct |
162 ms |
22752 KB |
Output is correct |