#include <bits/stdc++.h>
using namespace std;
bool stB;
namespace SOL {
void debug(const char *msg, ...) {
#ifdef CLESIP
va_list arg;
static char pbString[512];
va_start(arg,msg);
vsprintf(pbString,msg,arg);
cerr << "[DEBUG] " << pbString << "\n";
va_end(arg);
#endif
}
#define PASSED cerr << "PASSED " << __FUNCTION__ << " #" << __LINE__ << "\n";
using i64 = long long;
using i128 = __int128_t;
template <typename T, typename L> void chkmax(T &x, L y) { if (x < y) x = y; }
template <typename T, typename L> void chkmin(T &x, L y) { if (y < x) x = y; }
const int N = 6e5 + 10;
int n, m, lc[N], rc[N], rt[N], dis[N], a[N], fa[N], cnt, deg[N];
i64 val[N];
int merge(int x, int y) {
if (!x || !y)
return x | y;
if (val[x] < val[y])
swap(x, y);
rc[x] = merge(rc[x], y);
if (dis[lc[x]] < dis[rc[x]])
swap(lc[x], rc[x]);
dis[x] = rc[x] ? dis[rc[x]] + 1 : 0;
return x;
}
void pop(int x) {
rt[x] = merge(lc[rt[x]], rc[rt[x]]);
}
void ___solve() {
cin >> n >> m;
for (int i = 2; i <= n + m; i ++)
cin >> fa[i] >> a[i], ++ deg[fa[i]];
for (int i = n + m; i > 1; i --) {
i64 l = 0, r = 0;
if (i <= n) {
for (int j = 1; j < deg[i]; j ++)
pop(i);
r = val[rt[i]];
pop(i);
l = val[rt[i]];
pop(i);
}
l += a[i], r += a[i];
val[++ cnt] = l, val[++ cnt] = r;
rt[fa[i]] = merge(rt[fa[i]], merge(rt[i], merge(cnt - 1, cnt)));
}
for (int j = 1; j <= deg[1]; j ++)
pop(1);
i64 ans = 0;
for (int i = 2; i <= n + m; i ++)
ans += a[i];
while (rt[1] > 0) {
ans -= val[rt[1]], pop(1);
}
cout << ans << "\n";
}
}
bool edB;
signed main() {
// cerr << "Memory: " << (&stB - &edB) / 1024.0 / 1024.0 << "MB\n";
ios::sync_with_stdio(false), cin.tie(0), cout.tie(0);
SOL::___solve();
return 0;
}
# |
결과 |
실행 시간 |
메모리 |
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 |
2 ms |
6492 KB |
Output is correct |
6 |
Correct |
1 ms |
6492 KB |
Output is correct |
7 |
Correct |
2 ms |
6488 KB |
Output is correct |
8 |
Correct |
1 ms |
6620 KB |
Output is correct |
9 |
Correct |
1 ms |
6488 KB |
Output is correct |
10 |
Correct |
1 ms |
6492 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
6488 KB |
Output is correct |
2 |
Correct |
2 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 |
6504 KB |
Output is correct |
6 |
Correct |
1 ms |
6504 KB |
Output is correct |
7 |
Correct |
1 ms |
6620 KB |
Output is correct |
8 |
Correct |
1 ms |
6504 KB |
Output is correct |
9 |
Correct |
1 ms |
6500 KB |
Output is correct |
10 |
Correct |
1 ms |
6500 KB |
Output is correct |
11 |
Correct |
1 ms |
6504 KB |
Output is correct |
12 |
Correct |
1 ms |
6504 KB |
Output is correct |
13 |
Correct |
1 ms |
6504 KB |
Output is correct |
14 |
Correct |
1 ms |
6504 KB |
Output is correct |
15 |
Correct |
1 ms |
6504 KB |
Output is correct |
16 |
Correct |
1 ms |
6504 KB |
Output is correct |
17 |
Correct |
1 ms |
6496 KB |
Output is correct |
18 |
Correct |
1 ms |
6492 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 |
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 |
2 ms |
6492 KB |
Output is correct |
6 |
Correct |
1 ms |
6492 KB |
Output is correct |
7 |
Correct |
2 ms |
6488 KB |
Output is correct |
8 |
Correct |
1 ms |
6620 KB |
Output is correct |
9 |
Correct |
1 ms |
6488 KB |
Output is correct |
10 |
Correct |
1 ms |
6492 KB |
Output is correct |
11 |
Correct |
1 ms |
6488 KB |
Output is correct |
12 |
Correct |
2 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 |
6504 KB |
Output is correct |
16 |
Correct |
1 ms |
6504 KB |
Output is correct |
17 |
Correct |
1 ms |
6620 KB |
Output is correct |
18 |
Correct |
1 ms |
6504 KB |
Output is correct |
19 |
Correct |
1 ms |
6500 KB |
Output is correct |
20 |
Correct |
1 ms |
6500 KB |
Output is correct |
21 |
Correct |
1 ms |
6504 KB |
Output is correct |
22 |
Correct |
1 ms |
6504 KB |
Output is correct |
23 |
Correct |
1 ms |
6504 KB |
Output is correct |
24 |
Correct |
1 ms |
6504 KB |
Output is correct |
25 |
Correct |
1 ms |
6504 KB |
Output is correct |
26 |
Correct |
1 ms |
6504 KB |
Output is correct |
27 |
Correct |
1 ms |
6496 KB |
Output is correct |
28 |
Correct |
1 ms |
6492 KB |
Output is correct |
29 |
Correct |
1 ms |
6492 KB |
Output is correct |
30 |
Correct |
1 ms |
6496 KB |
Output is correct |
31 |
Correct |
1 ms |
6632 KB |
Output is correct |
32 |
Correct |
1 ms |
6756 KB |
Output is correct |
33 |
Correct |
1 ms |
6756 KB |
Output is correct |
34 |
Correct |
2 ms |
6756 KB |
Output is correct |
35 |
Correct |
2 ms |
6748 KB |
Output is correct |
36 |
Correct |
2 ms |
6684 KB |
Output is correct |
37 |
Correct |
2 ms |
6756 KB |
Output is correct |
38 |
Correct |
3 ms |
6756 KB |
Output is correct |
39 |
Correct |
2 ms |
6756 KB |
Output is correct |
40 |
Correct |
2 ms |
6756 KB |
Output is correct |
41 |
Correct |
2 ms |
6744 KB |
Output is correct |
42 |
Correct |
2 ms |
6748 KB |
Output is correct |
43 |
Correct |
2 ms |
6748 KB |
Output is correct |
44 |
Correct |
2 ms |
6748 KB |
Output is correct |
45 |
Correct |
2 ms |
6748 KB |
Output is correct |
46 |
Correct |
3 ms |
6692 KB |
Output is correct |
47 |
Correct |
3 ms |
6748 KB |
Output is correct |
48 |
Correct |
3 ms |
6748 KB |
Output is correct |
49 |
Correct |
3 ms |
6748 KB |
Output is correct |
50 |
Correct |
3 ms |
6880 KB |
Output is correct |
51 |
Correct |
3 ms |
6748 KB |
Output is correct |
52 |
Correct |
3 ms |
6748 KB |
Output is correct |
53 |
Correct |
3 ms |
7004 KB |
Output is correct |
54 |
Correct |
3 ms |
6744 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 |
2 ms |
6492 KB |
Output is correct |
6 |
Correct |
1 ms |
6492 KB |
Output is correct |
7 |
Correct |
2 ms |
6488 KB |
Output is correct |
8 |
Correct |
1 ms |
6620 KB |
Output is correct |
9 |
Correct |
1 ms |
6488 KB |
Output is correct |
10 |
Correct |
1 ms |
6492 KB |
Output is correct |
11 |
Correct |
1 ms |
6488 KB |
Output is correct |
12 |
Correct |
2 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 |
6504 KB |
Output is correct |
16 |
Correct |
1 ms |
6504 KB |
Output is correct |
17 |
Correct |
1 ms |
6620 KB |
Output is correct |
18 |
Correct |
1 ms |
6504 KB |
Output is correct |
19 |
Correct |
1 ms |
6500 KB |
Output is correct |
20 |
Correct |
1 ms |
6500 KB |
Output is correct |
21 |
Correct |
1 ms |
6504 KB |
Output is correct |
22 |
Correct |
1 ms |
6504 KB |
Output is correct |
23 |
Correct |
1 ms |
6504 KB |
Output is correct |
24 |
Correct |
1 ms |
6504 KB |
Output is correct |
25 |
Correct |
1 ms |
6504 KB |
Output is correct |
26 |
Correct |
1 ms |
6504 KB |
Output is correct |
27 |
Correct |
1 ms |
6496 KB |
Output is correct |
28 |
Correct |
1 ms |
6492 KB |
Output is correct |
29 |
Correct |
1 ms |
6492 KB |
Output is correct |
30 |
Correct |
1 ms |
6496 KB |
Output is correct |
31 |
Correct |
1 ms |
6632 KB |
Output is correct |
32 |
Correct |
1 ms |
6756 KB |
Output is correct |
33 |
Correct |
1 ms |
6756 KB |
Output is correct |
34 |
Correct |
2 ms |
6756 KB |
Output is correct |
35 |
Correct |
2 ms |
6748 KB |
Output is correct |
36 |
Correct |
2 ms |
6684 KB |
Output is correct |
37 |
Correct |
2 ms |
6756 KB |
Output is correct |
38 |
Correct |
3 ms |
6756 KB |
Output is correct |
39 |
Correct |
2 ms |
6756 KB |
Output is correct |
40 |
Correct |
2 ms |
6756 KB |
Output is correct |
41 |
Correct |
2 ms |
6744 KB |
Output is correct |
42 |
Correct |
2 ms |
6748 KB |
Output is correct |
43 |
Correct |
2 ms |
6748 KB |
Output is correct |
44 |
Correct |
2 ms |
6748 KB |
Output is correct |
45 |
Correct |
2 ms |
6748 KB |
Output is correct |
46 |
Correct |
3 ms |
6692 KB |
Output is correct |
47 |
Correct |
3 ms |
6748 KB |
Output is correct |
48 |
Correct |
3 ms |
6748 KB |
Output is correct |
49 |
Correct |
3 ms |
6748 KB |
Output is correct |
50 |
Correct |
3 ms |
6880 KB |
Output is correct |
51 |
Correct |
3 ms |
6748 KB |
Output is correct |
52 |
Correct |
3 ms |
6748 KB |
Output is correct |
53 |
Correct |
3 ms |
7004 KB |
Output is correct |
54 |
Correct |
3 ms |
6744 KB |
Output is correct |
55 |
Correct |
4 ms |
9308 KB |
Output is correct |
56 |
Correct |
14 ms |
10736 KB |
Output is correct |
57 |
Correct |
24 ms |
12124 KB |
Output is correct |
58 |
Correct |
31 ms |
13264 KB |
Output is correct |
59 |
Correct |
41 ms |
16624 KB |
Output is correct |
60 |
Correct |
57 ms |
18260 KB |
Output is correct |
61 |
Correct |
60 ms |
19260 KB |
Output is correct |
62 |
Correct |
65 ms |
20204 KB |
Output is correct |
63 |
Correct |
77 ms |
21824 KB |
Output is correct |
64 |
Correct |
85 ms |
23892 KB |
Output is correct |
65 |
Correct |
47 ms |
22872 KB |
Output is correct |
66 |
Correct |
52 ms |
22868 KB |
Output is correct |
67 |
Correct |
48 ms |
24916 KB |
Output is correct |
68 |
Correct |
89 ms |
24400 KB |
Output is correct |
69 |
Correct |
104 ms |
24316 KB |
Output is correct |
70 |
Correct |
92 ms |
24524 KB |
Output is correct |
71 |
Correct |
157 ms |
23572 KB |
Output is correct |
72 |
Correct |
152 ms |
23492 KB |
Output is correct |
73 |
Correct |
161 ms |
23120 KB |
Output is correct |
74 |
Correct |
163 ms |
23120 KB |
Output is correct |
75 |
Correct |
181 ms |
22868 KB |
Output is correct |
76 |
Correct |
168 ms |
23072 KB |
Output is correct |
77 |
Correct |
196 ms |
23540 KB |
Output is correct |
78 |
Correct |
199 ms |
22864 KB |
Output is correct |
79 |
Correct |
236 ms |
22728 KB |
Output is correct |