#pragma GCC optimize("O3,unroll-loops")
#pragma GCC target("avx2,bmi,bmi2,lzcnt,popcnt")
#include "bits/stdc++.h"
using namespace std;
using ll = long long;
using ld = long double;
#define rep(i, a, b) for (auto i = (a); i < (b); ++i)
#define per(i, a, b) for (auto i = (b); i-- > (a);)
#define all(x) begin(x), end(x)
#define rall(x) (x).rbegin(), (x).rend()
#define sz(x) static_cast<int>((x).size())
template <class T>
bool uin(T& a, const T& b) {
return a > b ? a = b, true : false;
}
template <class T>
bool uax(T& a, const T& b) {
return a < b ? a = b, true : false;
}
mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
signed main() {
ios::sync_with_stdio(false);
cin.tie(nullptr);
int n, m;
cin >> n >> m;
swap(n, m);
n += m;
vector<int> p(n), c(n);
rep(i, 1, n) {
cin >> p[i] >> c[i];
--p[i];
}
struct node {
int a;
ll b;
priority_queue<ll> q;
node() : a{0}, b{0} {}
node(int x) : a{1}, b{-x}, q(2, x) {}
void merge(node& o) {
a += o.a;
b += o.b;
if (sz(q) < sz(o.q)) swap(q, o.q);
while (!o.q.empty()) {
q.push(o.q.top());
o.q.pop();
}
}
};
vector<node> data(n);
per(i, m, n) {
data[i] = node(c[i]);
data[p[i]].merge(data[i]);
}
per(i, 1, m) {
const auto x = c[i];
auto& [a, b, q] = data[i];
while (a > 1) {
--a;
b += q.top();
q.pop();
}
const auto one = q.top();
q.pop();
const auto zero = q.top();
q.pop();
q.push(one + x);
q.push(zero + x);
b -= x;
data[p[i]].merge(data[i]);
}
auto& [a, b, q] = data[0];
while (a > 0) {
--a;
b += q.top();
q.pop();
}
cout << b << '\n';
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
212 KB |
Output is correct |
2 |
Correct |
1 ms |
320 KB |
Output is correct |
3 |
Correct |
1 ms |
212 KB |
Output is correct |
4 |
Correct |
1 ms |
324 KB |
Output is correct |
5 |
Correct |
1 ms |
212 KB |
Output is correct |
6 |
Correct |
1 ms |
340 KB |
Output is correct |
7 |
Correct |
1 ms |
340 KB |
Output is correct |
8 |
Correct |
1 ms |
212 KB |
Output is correct |
9 |
Correct |
1 ms |
320 KB |
Output is correct |
10 |
Correct |
1 ms |
212 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
320 KB |
Output is correct |
2 |
Correct |
1 ms |
320 KB |
Output is correct |
3 |
Correct |
1 ms |
348 KB |
Output is correct |
4 |
Correct |
1 ms |
328 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 |
320 KB |
Output is correct |
8 |
Correct |
1 ms |
332 KB |
Output is correct |
9 |
Correct |
1 ms |
340 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 |
324 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 |
320 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
212 KB |
Output is correct |
2 |
Correct |
1 ms |
320 KB |
Output is correct |
3 |
Correct |
1 ms |
212 KB |
Output is correct |
4 |
Correct |
1 ms |
324 KB |
Output is correct |
5 |
Correct |
1 ms |
212 KB |
Output is correct |
6 |
Correct |
1 ms |
340 KB |
Output is correct |
7 |
Correct |
1 ms |
340 KB |
Output is correct |
8 |
Correct |
1 ms |
212 KB |
Output is correct |
9 |
Correct |
1 ms |
320 KB |
Output is correct |
10 |
Correct |
1 ms |
212 KB |
Output is correct |
11 |
Correct |
1 ms |
320 KB |
Output is correct |
12 |
Correct |
1 ms |
320 KB |
Output is correct |
13 |
Correct |
1 ms |
348 KB |
Output is correct |
14 |
Correct |
1 ms |
328 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 |
320 KB |
Output is correct |
18 |
Correct |
1 ms |
332 KB |
Output is correct |
19 |
Correct |
1 ms |
340 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 |
324 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 |
320 KB |
Output is correct |
30 |
Correct |
1 ms |
340 KB |
Output is correct |
31 |
Correct |
1 ms |
340 KB |
Output is correct |
32 |
Correct |
1 ms |
464 KB |
Output is correct |
33 |
Correct |
1 ms |
460 KB |
Output is correct |
34 |
Correct |
2 ms |
592 KB |
Output is correct |
35 |
Correct |
2 ms |
596 KB |
Output is correct |
36 |
Correct |
2 ms |
724 KB |
Output is correct |
37 |
Correct |
2 ms |
724 KB |
Output is correct |
38 |
Correct |
2 ms |
844 KB |
Output is correct |
39 |
Correct |
3 ms |
724 KB |
Output is correct |
40 |
Correct |
2 ms |
596 KB |
Output is correct |
41 |
Correct |
2 ms |
596 KB |
Output is correct |
42 |
Correct |
2 ms |
596 KB |
Output is correct |
43 |
Correct |
3 ms |
724 KB |
Output is correct |
44 |
Correct |
3 ms |
724 KB |
Output is correct |
45 |
Correct |
3 ms |
724 KB |
Output is correct |
46 |
Correct |
4 ms |
940 KB |
Output is correct |
47 |
Correct |
3 ms |
980 KB |
Output is correct |
48 |
Correct |
2 ms |
924 KB |
Output is correct |
49 |
Correct |
3 ms |
976 KB |
Output is correct |
50 |
Correct |
3 ms |
724 KB |
Output is correct |
51 |
Correct |
2 ms |
720 KB |
Output is correct |
52 |
Correct |
3 ms |
852 KB |
Output is correct |
53 |
Correct |
3 ms |
852 KB |
Output is correct |
54 |
Correct |
2 ms |
852 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
212 KB |
Output is correct |
2 |
Correct |
1 ms |
320 KB |
Output is correct |
3 |
Correct |
1 ms |
212 KB |
Output is correct |
4 |
Correct |
1 ms |
324 KB |
Output is correct |
5 |
Correct |
1 ms |
212 KB |
Output is correct |
6 |
Correct |
1 ms |
340 KB |
Output is correct |
7 |
Correct |
1 ms |
340 KB |
Output is correct |
8 |
Correct |
1 ms |
212 KB |
Output is correct |
9 |
Correct |
1 ms |
320 KB |
Output is correct |
10 |
Correct |
1 ms |
212 KB |
Output is correct |
11 |
Correct |
1 ms |
320 KB |
Output is correct |
12 |
Correct |
1 ms |
320 KB |
Output is correct |
13 |
Correct |
1 ms |
348 KB |
Output is correct |
14 |
Correct |
1 ms |
328 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 |
320 KB |
Output is correct |
18 |
Correct |
1 ms |
332 KB |
Output is correct |
19 |
Correct |
1 ms |
340 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 |
324 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 |
320 KB |
Output is correct |
30 |
Correct |
1 ms |
340 KB |
Output is correct |
31 |
Correct |
1 ms |
340 KB |
Output is correct |
32 |
Correct |
1 ms |
464 KB |
Output is correct |
33 |
Correct |
1 ms |
460 KB |
Output is correct |
34 |
Correct |
2 ms |
592 KB |
Output is correct |
35 |
Correct |
2 ms |
596 KB |
Output is correct |
36 |
Correct |
2 ms |
724 KB |
Output is correct |
37 |
Correct |
2 ms |
724 KB |
Output is correct |
38 |
Correct |
2 ms |
844 KB |
Output is correct |
39 |
Correct |
3 ms |
724 KB |
Output is correct |
40 |
Correct |
2 ms |
596 KB |
Output is correct |
41 |
Correct |
2 ms |
596 KB |
Output is correct |
42 |
Correct |
2 ms |
596 KB |
Output is correct |
43 |
Correct |
3 ms |
724 KB |
Output is correct |
44 |
Correct |
3 ms |
724 KB |
Output is correct |
45 |
Correct |
3 ms |
724 KB |
Output is correct |
46 |
Correct |
4 ms |
940 KB |
Output is correct |
47 |
Correct |
3 ms |
980 KB |
Output is correct |
48 |
Correct |
2 ms |
924 KB |
Output is correct |
49 |
Correct |
3 ms |
976 KB |
Output is correct |
50 |
Correct |
3 ms |
724 KB |
Output is correct |
51 |
Correct |
2 ms |
720 KB |
Output is correct |
52 |
Correct |
3 ms |
852 KB |
Output is correct |
53 |
Correct |
3 ms |
852 KB |
Output is correct |
54 |
Correct |
2 ms |
852 KB |
Output is correct |
55 |
Correct |
5 ms |
1620 KB |
Output is correct |
56 |
Correct |
22 ms |
5568 KB |
Output is correct |
57 |
Correct |
38 ms |
9348 KB |
Output is correct |
58 |
Correct |
50 ms |
11924 KB |
Output is correct |
59 |
Correct |
62 ms |
15628 KB |
Output is correct |
60 |
Correct |
81 ms |
19348 KB |
Output is correct |
61 |
Correct |
90 ms |
22372 KB |
Output is correct |
62 |
Correct |
125 ms |
24608 KB |
Output is correct |
63 |
Correct |
154 ms |
29048 KB |
Output is correct |
64 |
Correct |
132 ms |
30400 KB |
Output is correct |
65 |
Correct |
72 ms |
21580 KB |
Output is correct |
66 |
Correct |
70 ms |
21580 KB |
Output is correct |
67 |
Correct |
78 ms |
21616 KB |
Output is correct |
68 |
Correct |
134 ms |
28368 KB |
Output is correct |
69 |
Correct |
134 ms |
29004 KB |
Output is correct |
70 |
Correct |
164 ms |
28976 KB |
Output is correct |
71 |
Correct |
186 ms |
43588 KB |
Output is correct |
72 |
Correct |
161 ms |
43764 KB |
Output is correct |
73 |
Correct |
153 ms |
40312 KB |
Output is correct |
74 |
Correct |
154 ms |
40816 KB |
Output is correct |
75 |
Correct |
156 ms |
39212 KB |
Output is correct |
76 |
Correct |
171 ms |
39180 KB |
Output is correct |
77 |
Correct |
152 ms |
38604 KB |
Output is correct |
78 |
Correct |
180 ms |
38044 KB |
Output is correct |
79 |
Correct |
158 ms |
36560 KB |
Output is correct |