#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
#define pb push_back
#define sz(x) (int)x.size()
#define all(x) begin(x),end(x)
#define lb(x,y) lower_bound(all(x),y)-begin(x)
mt19937 rng;
void solve() {
int N, M; cin >> N >> M;
vector<vector<int>> adj(N);
vector<int> len(N + M, 0);
for (int i = 1; i < N + M; i++) {
int u; cin >> u; u--;
adj[u].pb(i);
cin >> len[i];
}
if (M == 0) {
cout << 0 << "\n";
return;
}
vector<priority_queue<ll>> x(N + M);
vector<ll> a(N + M, 0), b(N + M, 0);
for (int u = N + M - 1; u >= 0; u--) {
if (u >= N) {
x[u].push(len[u]);
x[u].push(len[u]);
a[u] = 1;
b[u] = -len[u];
} else {
for (int v : adj[u]) {
if (sz(x[v]) > sz(x[u])) {
swap(x[u], x[v]);
swap(a[u], a[v]);
swap(b[u], b[v]);
}
}
for (int v : adj[u]) {
if (x[v].empty()) continue;
while (!x[v].empty()) {
x[u].push(x[v].top());
x[v].pop();
}
a[u] += a[v];
b[u] += b[v];
}
while (a[u] > 1) {
a[u]--;
b[u] += x[u].top(); x[u].pop();
}
ll n2 = x[u].top(); x[u].pop();
ll n1 = x[u].top(); x[u].pop();
n1 += len[u];
n2 += len[u];
x[u].push(n1);
x[u].push(n2);
b[u] -= len[u];
}
}
cout << (x[0].top() + b[0]) << "\n";
}
int main() {
ios::sync_with_stdio(false);
cin.tie(nullptr);
rng = mt19937(chrono::steady_clock::now().time_since_epoch().count());
solve();
return 0;
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
344 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
0 ms |
348 KB |
Output is correct |
4 |
Correct |
0 ms |
348 KB |
Output is correct |
5 |
Correct |
0 ms |
348 KB |
Output is correct |
6 |
Correct |
0 ms |
348 KB |
Output is correct |
7 |
Correct |
1 ms |
348 KB |
Output is correct |
8 |
Correct |
0 ms |
348 KB |
Output is correct |
9 |
Correct |
1 ms |
348 KB |
Output is correct |
10 |
Correct |
0 ms |
348 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
348 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
0 ms |
348 KB |
Output is correct |
4 |
Correct |
0 ms |
348 KB |
Output is correct |
5 |
Correct |
1 ms |
348 KB |
Output is correct |
6 |
Correct |
0 ms |
348 KB |
Output is correct |
7 |
Correct |
0 ms |
348 KB |
Output is correct |
8 |
Correct |
0 ms |
348 KB |
Output is correct |
9 |
Correct |
0 ms |
348 KB |
Output is correct |
10 |
Correct |
0 ms |
348 KB |
Output is correct |
11 |
Correct |
1 ms |
348 KB |
Output is correct |
12 |
Correct |
0 ms |
348 KB |
Output is correct |
13 |
Correct |
1 ms |
348 KB |
Output is correct |
14 |
Correct |
0 ms |
348 KB |
Output is correct |
15 |
Correct |
0 ms |
348 KB |
Output is correct |
16 |
Correct |
0 ms |
348 KB |
Output is correct |
17 |
Correct |
0 ms |
348 KB |
Output is correct |
18 |
Correct |
0 ms |
348 KB |
Output is correct |
19 |
Correct |
0 ms |
348 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
344 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
0 ms |
348 KB |
Output is correct |
4 |
Correct |
0 ms |
348 KB |
Output is correct |
5 |
Correct |
0 ms |
348 KB |
Output is correct |
6 |
Correct |
0 ms |
348 KB |
Output is correct |
7 |
Correct |
1 ms |
348 KB |
Output is correct |
8 |
Correct |
0 ms |
348 KB |
Output is correct |
9 |
Correct |
1 ms |
348 KB |
Output is correct |
10 |
Correct |
0 ms |
348 KB |
Output is correct |
11 |
Correct |
1 ms |
348 KB |
Output is correct |
12 |
Correct |
0 ms |
348 KB |
Output is correct |
13 |
Correct |
0 ms |
348 KB |
Output is correct |
14 |
Correct |
0 ms |
348 KB |
Output is correct |
15 |
Correct |
1 ms |
348 KB |
Output is correct |
16 |
Correct |
0 ms |
348 KB |
Output is correct |
17 |
Correct |
0 ms |
348 KB |
Output is correct |
18 |
Correct |
0 ms |
348 KB |
Output is correct |
19 |
Correct |
0 ms |
348 KB |
Output is correct |
20 |
Correct |
0 ms |
348 KB |
Output is correct |
21 |
Correct |
1 ms |
348 KB |
Output is correct |
22 |
Correct |
0 ms |
348 KB |
Output is correct |
23 |
Correct |
1 ms |
348 KB |
Output is correct |
24 |
Correct |
0 ms |
348 KB |
Output is correct |
25 |
Correct |
0 ms |
348 KB |
Output is correct |
26 |
Correct |
0 ms |
348 KB |
Output is correct |
27 |
Correct |
0 ms |
348 KB |
Output is correct |
28 |
Correct |
0 ms |
348 KB |
Output is correct |
29 |
Correct |
0 ms |
348 KB |
Output is correct |
30 |
Correct |
0 ms |
348 KB |
Output is correct |
31 |
Correct |
1 ms |
348 KB |
Output is correct |
32 |
Correct |
1 ms |
408 KB |
Output is correct |
33 |
Correct |
1 ms |
604 KB |
Output is correct |
34 |
Correct |
1 ms |
604 KB |
Output is correct |
35 |
Correct |
1 ms |
860 KB |
Output is correct |
36 |
Correct |
2 ms |
860 KB |
Output is correct |
37 |
Correct |
2 ms |
860 KB |
Output is correct |
38 |
Correct |
3 ms |
856 KB |
Output is correct |
39 |
Correct |
2 ms |
860 KB |
Output is correct |
40 |
Correct |
1 ms |
860 KB |
Output is correct |
41 |
Correct |
1 ms |
860 KB |
Output is correct |
42 |
Correct |
1 ms |
860 KB |
Output is correct |
43 |
Correct |
2 ms |
860 KB |
Output is correct |
44 |
Correct |
2 ms |
980 KB |
Output is correct |
45 |
Correct |
2 ms |
860 KB |
Output is correct |
46 |
Correct |
2 ms |
1116 KB |
Output is correct |
47 |
Correct |
2 ms |
1092 KB |
Output is correct |
48 |
Correct |
2 ms |
980 KB |
Output is correct |
49 |
Correct |
2 ms |
860 KB |
Output is correct |
50 |
Correct |
2 ms |
860 KB |
Output is correct |
51 |
Correct |
2 ms |
860 KB |
Output is correct |
52 |
Correct |
2 ms |
860 KB |
Output is correct |
53 |
Correct |
2 ms |
860 KB |
Output is correct |
54 |
Correct |
2 ms |
1116 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
344 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
0 ms |
348 KB |
Output is correct |
4 |
Correct |
0 ms |
348 KB |
Output is correct |
5 |
Correct |
0 ms |
348 KB |
Output is correct |
6 |
Correct |
0 ms |
348 KB |
Output is correct |
7 |
Correct |
1 ms |
348 KB |
Output is correct |
8 |
Correct |
0 ms |
348 KB |
Output is correct |
9 |
Correct |
1 ms |
348 KB |
Output is correct |
10 |
Correct |
0 ms |
348 KB |
Output is correct |
11 |
Correct |
1 ms |
348 KB |
Output is correct |
12 |
Correct |
0 ms |
348 KB |
Output is correct |
13 |
Correct |
0 ms |
348 KB |
Output is correct |
14 |
Correct |
0 ms |
348 KB |
Output is correct |
15 |
Correct |
1 ms |
348 KB |
Output is correct |
16 |
Correct |
0 ms |
348 KB |
Output is correct |
17 |
Correct |
0 ms |
348 KB |
Output is correct |
18 |
Correct |
0 ms |
348 KB |
Output is correct |
19 |
Correct |
0 ms |
348 KB |
Output is correct |
20 |
Correct |
0 ms |
348 KB |
Output is correct |
21 |
Correct |
1 ms |
348 KB |
Output is correct |
22 |
Correct |
0 ms |
348 KB |
Output is correct |
23 |
Correct |
1 ms |
348 KB |
Output is correct |
24 |
Correct |
0 ms |
348 KB |
Output is correct |
25 |
Correct |
0 ms |
348 KB |
Output is correct |
26 |
Correct |
0 ms |
348 KB |
Output is correct |
27 |
Correct |
0 ms |
348 KB |
Output is correct |
28 |
Correct |
0 ms |
348 KB |
Output is correct |
29 |
Correct |
0 ms |
348 KB |
Output is correct |
30 |
Correct |
0 ms |
348 KB |
Output is correct |
31 |
Correct |
1 ms |
348 KB |
Output is correct |
32 |
Correct |
1 ms |
408 KB |
Output is correct |
33 |
Correct |
1 ms |
604 KB |
Output is correct |
34 |
Correct |
1 ms |
604 KB |
Output is correct |
35 |
Correct |
1 ms |
860 KB |
Output is correct |
36 |
Correct |
2 ms |
860 KB |
Output is correct |
37 |
Correct |
2 ms |
860 KB |
Output is correct |
38 |
Correct |
3 ms |
856 KB |
Output is correct |
39 |
Correct |
2 ms |
860 KB |
Output is correct |
40 |
Correct |
1 ms |
860 KB |
Output is correct |
41 |
Correct |
1 ms |
860 KB |
Output is correct |
42 |
Correct |
1 ms |
860 KB |
Output is correct |
43 |
Correct |
2 ms |
860 KB |
Output is correct |
44 |
Correct |
2 ms |
980 KB |
Output is correct |
45 |
Correct |
2 ms |
860 KB |
Output is correct |
46 |
Correct |
2 ms |
1116 KB |
Output is correct |
47 |
Correct |
2 ms |
1092 KB |
Output is correct |
48 |
Correct |
2 ms |
980 KB |
Output is correct |
49 |
Correct |
2 ms |
860 KB |
Output is correct |
50 |
Correct |
2 ms |
860 KB |
Output is correct |
51 |
Correct |
2 ms |
860 KB |
Output is correct |
52 |
Correct |
2 ms |
860 KB |
Output is correct |
53 |
Correct |
2 ms |
860 KB |
Output is correct |
54 |
Correct |
2 ms |
1116 KB |
Output is correct |
55 |
Correct |
5 ms |
1884 KB |
Output is correct |
56 |
Correct |
18 ms |
6344 KB |
Output is correct |
57 |
Correct |
30 ms |
10504 KB |
Output is correct |
58 |
Correct |
40 ms |
13260 KB |
Output is correct |
59 |
Correct |
58 ms |
17484 KB |
Output is correct |
60 |
Correct |
82 ms |
21732 KB |
Output is correct |
61 |
Correct |
96 ms |
25032 KB |
Output is correct |
62 |
Correct |
103 ms |
27336 KB |
Output is correct |
63 |
Correct |
122 ms |
32620 KB |
Output is correct |
64 |
Correct |
123 ms |
34076 KB |
Output is correct |
65 |
Correct |
60 ms |
32080 KB |
Output is correct |
66 |
Correct |
60 ms |
32084 KB |
Output is correct |
67 |
Correct |
60 ms |
32080 KB |
Output is correct |
68 |
Correct |
111 ms |
31176 KB |
Output is correct |
69 |
Correct |
124 ms |
30668 KB |
Output is correct |
70 |
Correct |
114 ms |
30668 KB |
Output is correct |
71 |
Correct |
135 ms |
40804 KB |
Output is correct |
72 |
Correct |
143 ms |
41012 KB |
Output is correct |
73 |
Correct |
113 ms |
37096 KB |
Output is correct |
74 |
Correct |
114 ms |
38684 KB |
Output is correct |
75 |
Correct |
117 ms |
36676 KB |
Output is correct |
76 |
Correct |
123 ms |
36744 KB |
Output is correct |
77 |
Correct |
124 ms |
35012 KB |
Output is correct |
78 |
Correct |
121 ms |
36040 KB |
Output is correct |
79 |
Correct |
105 ms |
35952 KB |
Output is correct |