#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());
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/priority_queue.hpp>
template <class T>
using pq = __gnu_pbds::priority_queue<T, less<T>, __gnu_pbds::thin_heap_tag>;
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;
pq<ll> q;
node() : a{0}, b{0} {}
node(int x) : a{1}, b{-x} {
q.push(x);
q.push(x);
}
void merge(node& o) {
a += o.a;
b += o.b;
if (sz(q) < sz(o.q)) q.swap(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';
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
212 KB |
Output is correct |
2 |
Correct |
0 ms |
212 KB |
Output is correct |
3 |
Correct |
1 ms |
340 KB |
Output is correct |
4 |
Correct |
1 ms |
340 KB |
Output is correct |
5 |
Correct |
1 ms |
340 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 |
340 KB |
Output is correct |
9 |
Correct |
1 ms |
340 KB |
Output is correct |
10 |
Correct |
1 ms |
340 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Correct |
0 ms |
340 KB |
Output is correct |
3 |
Correct |
0 ms |
340 KB |
Output is correct |
4 |
Correct |
0 ms |
340 KB |
Output is correct |
5 |
Correct |
1 ms |
340 KB |
Output is correct |
6 |
Correct |
1 ms |
468 KB |
Output is correct |
7 |
Correct |
1 ms |
468 KB |
Output is correct |
8 |
Correct |
1 ms |
468 KB |
Output is correct |
9 |
Correct |
1 ms |
468 KB |
Output is correct |
10 |
Correct |
1 ms |
468 KB |
Output is correct |
11 |
Correct |
1 ms |
468 KB |
Output is correct |
12 |
Correct |
1 ms |
596 KB |
Output is correct |
13 |
Correct |
1 ms |
596 KB |
Output is correct |
14 |
Correct |
1 ms |
596 KB |
Output is correct |
15 |
Correct |
1 ms |
596 KB |
Output is correct |
16 |
Correct |
1 ms |
596 KB |
Output is correct |
17 |
Correct |
1 ms |
596 KB |
Output is correct |
18 |
Correct |
1 ms |
596 KB |
Output is correct |
19 |
Correct |
1 ms |
596 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
212 KB |
Output is correct |
2 |
Correct |
0 ms |
212 KB |
Output is correct |
3 |
Correct |
1 ms |
340 KB |
Output is correct |
4 |
Correct |
1 ms |
340 KB |
Output is correct |
5 |
Correct |
1 ms |
340 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 |
340 KB |
Output is correct |
9 |
Correct |
1 ms |
340 KB |
Output is correct |
10 |
Correct |
1 ms |
340 KB |
Output is correct |
11 |
Correct |
0 ms |
212 KB |
Output is correct |
12 |
Correct |
0 ms |
340 KB |
Output is correct |
13 |
Correct |
0 ms |
340 KB |
Output is correct |
14 |
Correct |
0 ms |
340 KB |
Output is correct |
15 |
Correct |
1 ms |
340 KB |
Output is correct |
16 |
Correct |
1 ms |
468 KB |
Output is correct |
17 |
Correct |
1 ms |
468 KB |
Output is correct |
18 |
Correct |
1 ms |
468 KB |
Output is correct |
19 |
Correct |
1 ms |
468 KB |
Output is correct |
20 |
Correct |
1 ms |
468 KB |
Output is correct |
21 |
Correct |
1 ms |
468 KB |
Output is correct |
22 |
Correct |
1 ms |
596 KB |
Output is correct |
23 |
Correct |
1 ms |
596 KB |
Output is correct |
24 |
Correct |
1 ms |
596 KB |
Output is correct |
25 |
Correct |
1 ms |
596 KB |
Output is correct |
26 |
Correct |
1 ms |
596 KB |
Output is correct |
27 |
Correct |
1 ms |
596 KB |
Output is correct |
28 |
Correct |
1 ms |
596 KB |
Output is correct |
29 |
Correct |
1 ms |
596 KB |
Output is correct |
30 |
Correct |
1 ms |
724 KB |
Output is correct |
31 |
Correct |
2 ms |
1492 KB |
Output is correct |
32 |
Correct |
3 ms |
2004 KB |
Output is correct |
33 |
Correct |
3 ms |
2792 KB |
Output is correct |
34 |
Correct |
5 ms |
3540 KB |
Output is correct |
35 |
Correct |
6 ms |
4308 KB |
Output is correct |
36 |
Correct |
5 ms |
4820 KB |
Output is correct |
37 |
Correct |
6 ms |
5588 KB |
Output is correct |
38 |
Correct |
6 ms |
5844 KB |
Output is correct |
39 |
Correct |
6 ms |
5844 KB |
Output is correct |
40 |
Correct |
5 ms |
5588 KB |
Output is correct |
41 |
Correct |
4 ms |
5588 KB |
Output is correct |
42 |
Correct |
4 ms |
5620 KB |
Output is correct |
43 |
Correct |
6 ms |
5844 KB |
Output is correct |
44 |
Correct |
6 ms |
5844 KB |
Output is correct |
45 |
Correct |
6 ms |
5844 KB |
Output is correct |
46 |
Correct |
7 ms |
6100 KB |
Output is correct |
47 |
Correct |
8 ms |
6100 KB |
Output is correct |
48 |
Correct |
6 ms |
5588 KB |
Output is correct |
49 |
Correct |
8 ms |
6100 KB |
Output is correct |
50 |
Correct |
6 ms |
5204 KB |
Output is correct |
51 |
Correct |
6 ms |
5076 KB |
Output is correct |
52 |
Correct |
9 ms |
5460 KB |
Output is correct |
53 |
Correct |
7 ms |
5716 KB |
Output is correct |
54 |
Correct |
6 ms |
6100 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
212 KB |
Output is correct |
2 |
Correct |
0 ms |
212 KB |
Output is correct |
3 |
Correct |
1 ms |
340 KB |
Output is correct |
4 |
Correct |
1 ms |
340 KB |
Output is correct |
5 |
Correct |
1 ms |
340 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 |
340 KB |
Output is correct |
9 |
Correct |
1 ms |
340 KB |
Output is correct |
10 |
Correct |
1 ms |
340 KB |
Output is correct |
11 |
Correct |
0 ms |
212 KB |
Output is correct |
12 |
Correct |
0 ms |
340 KB |
Output is correct |
13 |
Correct |
0 ms |
340 KB |
Output is correct |
14 |
Correct |
0 ms |
340 KB |
Output is correct |
15 |
Correct |
1 ms |
340 KB |
Output is correct |
16 |
Correct |
1 ms |
468 KB |
Output is correct |
17 |
Correct |
1 ms |
468 KB |
Output is correct |
18 |
Correct |
1 ms |
468 KB |
Output is correct |
19 |
Correct |
1 ms |
468 KB |
Output is correct |
20 |
Correct |
1 ms |
468 KB |
Output is correct |
21 |
Correct |
1 ms |
468 KB |
Output is correct |
22 |
Correct |
1 ms |
596 KB |
Output is correct |
23 |
Correct |
1 ms |
596 KB |
Output is correct |
24 |
Correct |
1 ms |
596 KB |
Output is correct |
25 |
Correct |
1 ms |
596 KB |
Output is correct |
26 |
Correct |
1 ms |
596 KB |
Output is correct |
27 |
Correct |
1 ms |
596 KB |
Output is correct |
28 |
Correct |
1 ms |
596 KB |
Output is correct |
29 |
Correct |
1 ms |
596 KB |
Output is correct |
30 |
Correct |
1 ms |
724 KB |
Output is correct |
31 |
Correct |
2 ms |
1492 KB |
Output is correct |
32 |
Correct |
3 ms |
2004 KB |
Output is correct |
33 |
Correct |
3 ms |
2792 KB |
Output is correct |
34 |
Correct |
5 ms |
3540 KB |
Output is correct |
35 |
Correct |
6 ms |
4308 KB |
Output is correct |
36 |
Correct |
5 ms |
4820 KB |
Output is correct |
37 |
Correct |
6 ms |
5588 KB |
Output is correct |
38 |
Correct |
6 ms |
5844 KB |
Output is correct |
39 |
Correct |
6 ms |
5844 KB |
Output is correct |
40 |
Correct |
5 ms |
5588 KB |
Output is correct |
41 |
Correct |
4 ms |
5588 KB |
Output is correct |
42 |
Correct |
4 ms |
5620 KB |
Output is correct |
43 |
Correct |
6 ms |
5844 KB |
Output is correct |
44 |
Correct |
6 ms |
5844 KB |
Output is correct |
45 |
Correct |
6 ms |
5844 KB |
Output is correct |
46 |
Correct |
7 ms |
6100 KB |
Output is correct |
47 |
Correct |
8 ms |
6100 KB |
Output is correct |
48 |
Correct |
6 ms |
5588 KB |
Output is correct |
49 |
Correct |
8 ms |
6100 KB |
Output is correct |
50 |
Correct |
6 ms |
5204 KB |
Output is correct |
51 |
Correct |
6 ms |
5076 KB |
Output is correct |
52 |
Correct |
9 ms |
5460 KB |
Output is correct |
53 |
Correct |
7 ms |
5716 KB |
Output is correct |
54 |
Correct |
6 ms |
6100 KB |
Output is correct |
55 |
Correct |
15 ms |
14812 KB |
Output is correct |
56 |
Correct |
53 ms |
54740 KB |
Output is correct |
57 |
Correct |
109 ms |
93544 KB |
Output is correct |
58 |
Correct |
165 ms |
118876 KB |
Output is correct |
59 |
Correct |
186 ms |
158788 KB |
Output is correct |
60 |
Correct |
220 ms |
197636 KB |
Output is correct |
61 |
Correct |
301 ms |
224272 KB |
Output is correct |
62 |
Correct |
330 ms |
247580 KB |
Output is correct |
63 |
Correct |
376 ms |
295432 KB |
Output is correct |
64 |
Correct |
387 ms |
312100 KB |
Output is correct |
65 |
Correct |
215 ms |
322128 KB |
Output is correct |
66 |
Correct |
212 ms |
322036 KB |
Output is correct |
67 |
Correct |
206 ms |
322040 KB |
Output is correct |
68 |
Correct |
338 ms |
336220 KB |
Output is correct |
69 |
Correct |
353 ms |
336232 KB |
Output is correct |
70 |
Correct |
377 ms |
336424 KB |
Output is correct |
71 |
Correct |
480 ms |
349920 KB |
Output is correct |
72 |
Correct |
527 ms |
349804 KB |
Output is correct |
73 |
Correct |
423 ms |
349796 KB |
Output is correct |
74 |
Correct |
433 ms |
349932 KB |
Output is correct |
75 |
Correct |
427 ms |
347648 KB |
Output is correct |
76 |
Correct |
459 ms |
348636 KB |
Output is correct |
77 |
Correct |
458 ms |
341440 KB |
Output is correct |
78 |
Correct |
503 ms |
342300 KB |
Output is correct |
79 |
Correct |
525 ms |
350172 KB |
Output is correct |