#include <iostream>
#include <cassert>
#include <queue>
#include <cmath>
#include <cstring>
#include <algorithm>
#include <bitset>
#include <ctime>
#include <map>
#include <set>
using namespace std;
#define int long long
#define pii pair<int, int>
#define eb emplace_back
#define F first
#define S second
#define test(x) cout << "Test: " << x << '\n'
#define lowbit(x) (x & -x)
#define debug puts("qwq");
#define open(x) freopen(#x".in", "r", stdin);freopen(#x".out", "w", stdout);
#define close fclose(stdin);fclose(stdout);
namespace FastIO {
template <typename T = int>
inline T read() {
T s = 0, w = 1;
char c = getchar();
while (!isdigit(c)) {
if (c == '-') w = -1;
c = getchar();
}
while (isdigit(c)) s = (s << 1) + (s << 3) + (c ^ 48), c = getchar();
return s * w;
}
template <typename T>
inline void read(T &s) {
s = 0;
int w = 1;
char c = getchar();
while (!isdigit(c)) {
if (c == '-') w = -1;
c = getchar();
}
while (isdigit(c)) s = (s << 1) + (s << 3) + (c ^ 48), c = getchar();
s = s * w;
}
template <typename T, typename... Arp> inline void read(T &x, Arp &...arp) {
read(x), read(arp...);
}
template <typename T>
inline void write(T x, char ch) {
if (x < 0) x = -x, putchar('-');
static char stk[25];
int top = 0;
do {
stk[top++] = x % 10 + '0', x /= 10;
} while (x);
while (top) putchar(stk[--top]);
putchar(ch);
return;
}
template <typename T>
inline void smax(T &x, T y) {
if (x < y) x = y;
}
template <typename T>
inline void smin(T &x, T y) {
if (x > y) x = y;
}
void quit() {
exit(0);
}
} using namespace FastIO;
const int N = 1e6 + 19;
int f[N], w[N], d[N], root[N];
struct LeftTree {
int val[N], t[N], ls[N], rs[N], cnt;
int build_new(int v) {
val[++cnt] = v;
return cnt;
}
int merge(int p, int q) {
if (!p || !q) return p + q;
if (val[p] < val[q]) swap(p, q);
rs[p] = merge(rs[p], q);
if (t[ls[p]] < t[rs[p]]) swap(ls[p], rs[p]);
t[p] = t[rs[p]] + 1;
return p;
}
void del(int &p) {
p = merge(ls[p], rs[p]);
}
} tr;
signed main() {
int n, m, ans = 0;
read(n, m);
for (int i = 2; i <= n + m; ++i) {
read(f[i], w[i]);
ans += w[i];
d[f[i]]++;
}
for (int u = n + m; u >= 2; --u) {
if (u <= n) while (--d[u]) tr.del(root[u]);
int R = tr.val[root[u]]; tr.del(root[u]);
int L = tr.val[root[u]]; tr.del(root[u]);
root[u] = tr.merge(root[u], tr.merge(tr.build_new(L + w[u]), tr.build_new(R + w[u])));
root[f[u]] = tr.merge(root[f[u]], root[u]);
}
while (d[1]--) tr.del(root[1]);
while (root[1]) ans -= tr.val[root[1]], tr.del(root[1]);
write(ans, '\n');
return 0;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
6492 KB |
Output is correct |
2 |
Correct |
1 ms |
2396 KB |
Output is correct |
3 |
Correct |
0 ms |
2396 KB |
Output is correct |
4 |
Correct |
1 ms |
2392 KB |
Output is correct |
5 |
Correct |
1 ms |
2396 KB |
Output is correct |
6 |
Correct |
1 ms |
2392 KB |
Output is correct |
7 |
Correct |
1 ms |
2396 KB |
Output is correct |
8 |
Correct |
1 ms |
2396 KB |
Output is correct |
9 |
Correct |
1 ms |
2396 KB |
Output is correct |
10 |
Correct |
1 ms |
2396 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
6492 KB |
Output is correct |
2 |
Correct |
1 ms |
6492 KB |
Output is correct |
3 |
Correct |
1 ms |
6492 KB |
Output is correct |
4 |
Correct |
1 ms |
6492 KB |
Output is correct |
5 |
Correct |
1 ms |
6604 KB |
Output is correct |
6 |
Correct |
1 ms |
6596 KB |
Output is correct |
7 |
Correct |
0 ms |
2396 KB |
Output is correct |
8 |
Correct |
1 ms |
6592 KB |
Output is correct |
9 |
Correct |
1 ms |
6492 KB |
Output is correct |
10 |
Correct |
1 ms |
6492 KB |
Output is correct |
11 |
Correct |
1 ms |
6588 KB |
Output is correct |
12 |
Correct |
1 ms |
2496 KB |
Output is correct |
13 |
Correct |
1 ms |
2492 KB |
Output is correct |
14 |
Correct |
0 ms |
2396 KB |
Output is correct |
15 |
Correct |
1 ms |
6492 KB |
Output is correct |
16 |
Correct |
1 ms |
6492 KB |
Output is correct |
17 |
Correct |
1 ms |
2396 KB |
Output is correct |
18 |
Correct |
1 ms |
2396 KB |
Output is correct |
19 |
Correct |
1 ms |
6492 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
6492 KB |
Output is correct |
2 |
Correct |
1 ms |
2396 KB |
Output is correct |
3 |
Correct |
0 ms |
2396 KB |
Output is correct |
4 |
Correct |
1 ms |
2392 KB |
Output is correct |
5 |
Correct |
1 ms |
2396 KB |
Output is correct |
6 |
Correct |
1 ms |
2392 KB |
Output is correct |
7 |
Correct |
1 ms |
2396 KB |
Output is correct |
8 |
Correct |
1 ms |
2396 KB |
Output is correct |
9 |
Correct |
1 ms |
2396 KB |
Output is correct |
10 |
Correct |
1 ms |
2396 KB |
Output is correct |
11 |
Correct |
1 ms |
6492 KB |
Output is correct |
12 |
Correct |
1 ms |
6492 KB |
Output is correct |
13 |
Correct |
1 ms |
6492 KB |
Output is correct |
14 |
Correct |
1 ms |
6492 KB |
Output is correct |
15 |
Correct |
1 ms |
6604 KB |
Output is correct |
16 |
Correct |
1 ms |
6596 KB |
Output is correct |
17 |
Correct |
0 ms |
2396 KB |
Output is correct |
18 |
Correct |
1 ms |
6592 KB |
Output is correct |
19 |
Correct |
1 ms |
6492 KB |
Output is correct |
20 |
Correct |
1 ms |
6492 KB |
Output is correct |
21 |
Correct |
1 ms |
6588 KB |
Output is correct |
22 |
Correct |
1 ms |
2496 KB |
Output is correct |
23 |
Correct |
1 ms |
2492 KB |
Output is correct |
24 |
Correct |
0 ms |
2396 KB |
Output is correct |
25 |
Correct |
1 ms |
6492 KB |
Output is correct |
26 |
Correct |
1 ms |
6492 KB |
Output is correct |
27 |
Correct |
1 ms |
2396 KB |
Output is correct |
28 |
Correct |
1 ms |
2396 KB |
Output is correct |
29 |
Correct |
1 ms |
6492 KB |
Output is correct |
30 |
Correct |
1 ms |
6488 KB |
Output is correct |
31 |
Correct |
1 ms |
6492 KB |
Output is correct |
32 |
Correct |
1 ms |
6748 KB |
Output is correct |
33 |
Correct |
1 ms |
6748 KB |
Output is correct |
34 |
Correct |
1 ms |
6748 KB |
Output is correct |
35 |
Correct |
1 ms |
2652 KB |
Output is correct |
36 |
Correct |
2 ms |
6748 KB |
Output is correct |
37 |
Correct |
2 ms |
7004 KB |
Output is correct |
38 |
Correct |
1 ms |
2908 KB |
Output is correct |
39 |
Correct |
2 ms |
6860 KB |
Output is correct |
40 |
Correct |
1 ms |
8796 KB |
Output is correct |
41 |
Correct |
1 ms |
8796 KB |
Output is correct |
42 |
Correct |
1 ms |
9052 KB |
Output is correct |
43 |
Correct |
2 ms |
7004 KB |
Output is correct |
44 |
Correct |
2 ms |
7056 KB |
Output is correct |
45 |
Correct |
2 ms |
7004 KB |
Output is correct |
46 |
Correct |
2 ms |
7004 KB |
Output is correct |
47 |
Correct |
2 ms |
7004 KB |
Output is correct |
48 |
Correct |
2 ms |
7004 KB |
Output is correct |
49 |
Correct |
2 ms |
7004 KB |
Output is correct |
50 |
Correct |
2 ms |
6748 KB |
Output is correct |
51 |
Correct |
3 ms |
6748 KB |
Output is correct |
52 |
Correct |
2 ms |
2908 KB |
Output is correct |
53 |
Correct |
2 ms |
2840 KB |
Output is correct |
54 |
Correct |
2 ms |
7004 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
6492 KB |
Output is correct |
2 |
Correct |
1 ms |
2396 KB |
Output is correct |
3 |
Correct |
0 ms |
2396 KB |
Output is correct |
4 |
Correct |
1 ms |
2392 KB |
Output is correct |
5 |
Correct |
1 ms |
2396 KB |
Output is correct |
6 |
Correct |
1 ms |
2392 KB |
Output is correct |
7 |
Correct |
1 ms |
2396 KB |
Output is correct |
8 |
Correct |
1 ms |
2396 KB |
Output is correct |
9 |
Correct |
1 ms |
2396 KB |
Output is correct |
10 |
Correct |
1 ms |
2396 KB |
Output is correct |
11 |
Correct |
1 ms |
6492 KB |
Output is correct |
12 |
Correct |
1 ms |
6492 KB |
Output is correct |
13 |
Correct |
1 ms |
6492 KB |
Output is correct |
14 |
Correct |
1 ms |
6492 KB |
Output is correct |
15 |
Correct |
1 ms |
6604 KB |
Output is correct |
16 |
Correct |
1 ms |
6596 KB |
Output is correct |
17 |
Correct |
0 ms |
2396 KB |
Output is correct |
18 |
Correct |
1 ms |
6592 KB |
Output is correct |
19 |
Correct |
1 ms |
6492 KB |
Output is correct |
20 |
Correct |
1 ms |
6492 KB |
Output is correct |
21 |
Correct |
1 ms |
6588 KB |
Output is correct |
22 |
Correct |
1 ms |
2496 KB |
Output is correct |
23 |
Correct |
1 ms |
2492 KB |
Output is correct |
24 |
Correct |
0 ms |
2396 KB |
Output is correct |
25 |
Correct |
1 ms |
6492 KB |
Output is correct |
26 |
Correct |
1 ms |
6492 KB |
Output is correct |
27 |
Correct |
1 ms |
2396 KB |
Output is correct |
28 |
Correct |
1 ms |
2396 KB |
Output is correct |
29 |
Correct |
1 ms |
6492 KB |
Output is correct |
30 |
Correct |
1 ms |
6488 KB |
Output is correct |
31 |
Correct |
1 ms |
6492 KB |
Output is correct |
32 |
Correct |
1 ms |
6748 KB |
Output is correct |
33 |
Correct |
1 ms |
6748 KB |
Output is correct |
34 |
Correct |
1 ms |
6748 KB |
Output is correct |
35 |
Correct |
1 ms |
2652 KB |
Output is correct |
36 |
Correct |
2 ms |
6748 KB |
Output is correct |
37 |
Correct |
2 ms |
7004 KB |
Output is correct |
38 |
Correct |
1 ms |
2908 KB |
Output is correct |
39 |
Correct |
2 ms |
6860 KB |
Output is correct |
40 |
Correct |
1 ms |
8796 KB |
Output is correct |
41 |
Correct |
1 ms |
8796 KB |
Output is correct |
42 |
Correct |
1 ms |
9052 KB |
Output is correct |
43 |
Correct |
2 ms |
7004 KB |
Output is correct |
44 |
Correct |
2 ms |
7056 KB |
Output is correct |
45 |
Correct |
2 ms |
7004 KB |
Output is correct |
46 |
Correct |
2 ms |
7004 KB |
Output is correct |
47 |
Correct |
2 ms |
7004 KB |
Output is correct |
48 |
Correct |
2 ms |
7004 KB |
Output is correct |
49 |
Correct |
2 ms |
7004 KB |
Output is correct |
50 |
Correct |
2 ms |
6748 KB |
Output is correct |
51 |
Correct |
3 ms |
6748 KB |
Output is correct |
52 |
Correct |
2 ms |
2908 KB |
Output is correct |
53 |
Correct |
2 ms |
2840 KB |
Output is correct |
54 |
Correct |
2 ms |
7004 KB |
Output is correct |
55 |
Correct |
3 ms |
9564 KB |
Output is correct |
56 |
Correct |
10 ms |
12860 KB |
Output is correct |
57 |
Correct |
20 ms |
16232 KB |
Output is correct |
58 |
Correct |
21 ms |
18232 KB |
Output is correct |
59 |
Correct |
29 ms |
21852 KB |
Output is correct |
60 |
Correct |
41 ms |
25416 KB |
Output is correct |
61 |
Correct |
44 ms |
27988 KB |
Output is correct |
62 |
Correct |
51 ms |
30032 KB |
Output is correct |
63 |
Correct |
61 ms |
34640 KB |
Output is correct |
64 |
Correct |
69 ms |
36176 KB |
Output is correct |
65 |
Correct |
28 ms |
33616 KB |
Output is correct |
66 |
Correct |
24 ms |
33620 KB |
Output is correct |
67 |
Correct |
25 ms |
36184 KB |
Output is correct |
68 |
Correct |
67 ms |
37968 KB |
Output is correct |
69 |
Correct |
74 ms |
37976 KB |
Output is correct |
70 |
Correct |
94 ms |
37972 KB |
Output is correct |
71 |
Correct |
131 ms |
35412 KB |
Output is correct |
72 |
Correct |
130 ms |
35408 KB |
Output is correct |
73 |
Correct |
139 ms |
35336 KB |
Output is correct |
74 |
Correct |
139 ms |
35348 KB |
Output is correct |
75 |
Correct |
152 ms |
34900 KB |
Output is correct |
76 |
Correct |
150 ms |
34896 KB |
Output is correct |
77 |
Correct |
160 ms |
34820 KB |
Output is correct |
78 |
Correct |
179 ms |
34644 KB |
Output is correct |
79 |
Correct |
234 ms |
34648 KB |
Output is correct |