#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
using namespace std;
using namespace __gnu_pbds;
using i64 = long long;
using d64 = long double;
using pi = pair<int, int>;
using pli = pair<i64, i64>;
using ti = tuple<int, int, int>;
using tli = tuple<i64, i64, i64>;
template <class T>
using ordered_set =
tree<T, null_type, less<T>, rb_tree_tag, tree_order_statistics_node_update>;
#define iterall(cont) cont.begin(), cont.end()
#define prec(n) setprecision(n) << fixed
vector<pli> adj[300001], tr[300001];
int sz[300001];
class slope {
public:
priority_queue<i64> bp;
i64 sac, fz;
slope() {
sac = 0;
fz = 0;
}
void push(i64 x) {
fz += x;
if (bp.empty()) {
bp.push(x);
bp.push(x);
++sac;
return;
}
while (bp.size() > sac + 1) bp.pop();
auto m1 = bp.top();
bp.pop();
auto m2 = bp.top();
bp.pop();
bp.push(m1 + x);
bp.push(m2 + x);
}
};
void mer(slope* s1, slope* s2) {
s1->fz += s2->fz;
s1->sac += s2->sac;
while (!s2->bp.empty()) s1->bp.push(s2->bp.top()), s2->bp.pop();
delete s2;
}
void dfs(int h, int p = -1) {
sz[h] = 1;
for (auto [t, _] : adj[h]) {
if (t == p) continue;
dfs(t, h);
sz[h] += sz[t];
tr[h].emplace_back(t, _);
}
sort(iterall(tr[h]), [](const pli& lhs, const pli& rhs) {
return sz[lhs.first] > sz[rhs.first];
});
}
slope* solve(int h) {
slope* ret = nullptr;
for (auto [t, wf] : tr[h]) {
auto chslp = solve(t);
chslp->push(wf);
if (!ret)
ret = chslp;
else
mer(ret, chslp);
}
return ret ? ret : new slope();
}
int main() {
ios_base::sync_with_stdio(false);
cin.tie(nullptr);
cout.tie(nullptr);
int N, M;
cin >> N >> M;
for (int i = 2; i <= N + M; i++) {
i64 j, w;
cin >> j >> w;
adj[i].emplace_back(j, w);
adj[j].emplace_back(i, w);
}
dfs(1);
auto res = *solve(1);
vector<i64> bp;
while (!res.bp.empty()) bp.emplace_back(res.bp.top()), res.bp.pop();
reverse(iterall(bp));
for (int i = 0; i < res.sac; i++) res.fz -= bp[i];
cout << res.fz << endl;
return 0;
}
Compilation message
fireworks.cpp: In member function 'void slope::push(i64)':
fireworks.cpp:45:26: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
while (bp.size() > sac + 1) bp.pop();
~~~~~~~~~~^~~~~~~~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
13 ms |
14456 KB |
Output is correct |
2 |
Correct |
14 ms |
14456 KB |
Output is correct |
3 |
Correct |
14 ms |
14456 KB |
Output is correct |
4 |
Correct |
13 ms |
14456 KB |
Output is correct |
5 |
Correct |
13 ms |
14456 KB |
Output is correct |
6 |
Correct |
13 ms |
14456 KB |
Output is correct |
7 |
Correct |
13 ms |
14456 KB |
Output is correct |
8 |
Correct |
14 ms |
14456 KB |
Output is correct |
9 |
Correct |
13 ms |
14328 KB |
Output is correct |
10 |
Correct |
13 ms |
14456 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
14 ms |
14456 KB |
Output is correct |
2 |
Correct |
13 ms |
14456 KB |
Output is correct |
3 |
Correct |
13 ms |
14456 KB |
Output is correct |
4 |
Correct |
13 ms |
14456 KB |
Output is correct |
5 |
Correct |
14 ms |
14456 KB |
Output is correct |
6 |
Correct |
14 ms |
14456 KB |
Output is correct |
7 |
Correct |
14 ms |
14488 KB |
Output is correct |
8 |
Correct |
13 ms |
14456 KB |
Output is correct |
9 |
Correct |
13 ms |
14456 KB |
Output is correct |
10 |
Correct |
13 ms |
14456 KB |
Output is correct |
11 |
Correct |
14 ms |
14456 KB |
Output is correct |
12 |
Correct |
15 ms |
14456 KB |
Output is correct |
13 |
Correct |
13 ms |
14456 KB |
Output is correct |
14 |
Correct |
13 ms |
14456 KB |
Output is correct |
15 |
Correct |
14 ms |
14456 KB |
Output is correct |
16 |
Correct |
13 ms |
14456 KB |
Output is correct |
17 |
Correct |
14 ms |
14456 KB |
Output is correct |
18 |
Correct |
13 ms |
14456 KB |
Output is correct |
19 |
Correct |
13 ms |
14456 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
13 ms |
14456 KB |
Output is correct |
2 |
Correct |
14 ms |
14456 KB |
Output is correct |
3 |
Correct |
14 ms |
14456 KB |
Output is correct |
4 |
Correct |
13 ms |
14456 KB |
Output is correct |
5 |
Correct |
13 ms |
14456 KB |
Output is correct |
6 |
Correct |
13 ms |
14456 KB |
Output is correct |
7 |
Correct |
13 ms |
14456 KB |
Output is correct |
8 |
Correct |
14 ms |
14456 KB |
Output is correct |
9 |
Correct |
13 ms |
14328 KB |
Output is correct |
10 |
Correct |
13 ms |
14456 KB |
Output is correct |
11 |
Correct |
14 ms |
14456 KB |
Output is correct |
12 |
Correct |
13 ms |
14456 KB |
Output is correct |
13 |
Correct |
13 ms |
14456 KB |
Output is correct |
14 |
Correct |
13 ms |
14456 KB |
Output is correct |
15 |
Correct |
14 ms |
14456 KB |
Output is correct |
16 |
Correct |
14 ms |
14456 KB |
Output is correct |
17 |
Correct |
14 ms |
14488 KB |
Output is correct |
18 |
Correct |
13 ms |
14456 KB |
Output is correct |
19 |
Correct |
13 ms |
14456 KB |
Output is correct |
20 |
Correct |
13 ms |
14456 KB |
Output is correct |
21 |
Correct |
14 ms |
14456 KB |
Output is correct |
22 |
Correct |
15 ms |
14456 KB |
Output is correct |
23 |
Correct |
13 ms |
14456 KB |
Output is correct |
24 |
Correct |
13 ms |
14456 KB |
Output is correct |
25 |
Correct |
14 ms |
14456 KB |
Output is correct |
26 |
Correct |
13 ms |
14456 KB |
Output is correct |
27 |
Correct |
14 ms |
14456 KB |
Output is correct |
28 |
Correct |
13 ms |
14456 KB |
Output is correct |
29 |
Correct |
13 ms |
14456 KB |
Output is correct |
30 |
Correct |
13 ms |
14456 KB |
Output is correct |
31 |
Correct |
16 ms |
14584 KB |
Output is correct |
32 |
Correct |
14 ms |
14584 KB |
Output is correct |
33 |
Correct |
16 ms |
14584 KB |
Output is correct |
34 |
Correct |
16 ms |
14712 KB |
Output is correct |
35 |
Correct |
17 ms |
14840 KB |
Output is correct |
36 |
Correct |
17 ms |
14844 KB |
Output is correct |
37 |
Correct |
17 ms |
14840 KB |
Output is correct |
38 |
Correct |
17 ms |
14840 KB |
Output is correct |
39 |
Correct |
18 ms |
14840 KB |
Output is correct |
40 |
Correct |
16 ms |
15352 KB |
Output is correct |
41 |
Correct |
17 ms |
15352 KB |
Output is correct |
42 |
Correct |
17 ms |
14840 KB |
Output is correct |
43 |
Correct |
16 ms |
15224 KB |
Output is correct |
44 |
Correct |
18 ms |
15096 KB |
Output is correct |
45 |
Correct |
18 ms |
15096 KB |
Output is correct |
46 |
Correct |
17 ms |
15100 KB |
Output is correct |
47 |
Correct |
17 ms |
15096 KB |
Output is correct |
48 |
Correct |
18 ms |
14968 KB |
Output is correct |
49 |
Correct |
17 ms |
14968 KB |
Output is correct |
50 |
Correct |
16 ms |
14968 KB |
Output is correct |
51 |
Correct |
16 ms |
14968 KB |
Output is correct |
52 |
Correct |
18 ms |
14968 KB |
Output is correct |
53 |
Correct |
16 ms |
14968 KB |
Output is correct |
54 |
Correct |
17 ms |
15096 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
13 ms |
14456 KB |
Output is correct |
2 |
Correct |
14 ms |
14456 KB |
Output is correct |
3 |
Correct |
14 ms |
14456 KB |
Output is correct |
4 |
Correct |
13 ms |
14456 KB |
Output is correct |
5 |
Correct |
13 ms |
14456 KB |
Output is correct |
6 |
Correct |
13 ms |
14456 KB |
Output is correct |
7 |
Correct |
13 ms |
14456 KB |
Output is correct |
8 |
Correct |
14 ms |
14456 KB |
Output is correct |
9 |
Correct |
13 ms |
14328 KB |
Output is correct |
10 |
Correct |
13 ms |
14456 KB |
Output is correct |
11 |
Correct |
14 ms |
14456 KB |
Output is correct |
12 |
Correct |
13 ms |
14456 KB |
Output is correct |
13 |
Correct |
13 ms |
14456 KB |
Output is correct |
14 |
Correct |
13 ms |
14456 KB |
Output is correct |
15 |
Correct |
14 ms |
14456 KB |
Output is correct |
16 |
Correct |
14 ms |
14456 KB |
Output is correct |
17 |
Correct |
14 ms |
14488 KB |
Output is correct |
18 |
Correct |
13 ms |
14456 KB |
Output is correct |
19 |
Correct |
13 ms |
14456 KB |
Output is correct |
20 |
Correct |
13 ms |
14456 KB |
Output is correct |
21 |
Correct |
14 ms |
14456 KB |
Output is correct |
22 |
Correct |
15 ms |
14456 KB |
Output is correct |
23 |
Correct |
13 ms |
14456 KB |
Output is correct |
24 |
Correct |
13 ms |
14456 KB |
Output is correct |
25 |
Correct |
14 ms |
14456 KB |
Output is correct |
26 |
Correct |
13 ms |
14456 KB |
Output is correct |
27 |
Correct |
14 ms |
14456 KB |
Output is correct |
28 |
Correct |
13 ms |
14456 KB |
Output is correct |
29 |
Correct |
13 ms |
14456 KB |
Output is correct |
30 |
Correct |
13 ms |
14456 KB |
Output is correct |
31 |
Correct |
16 ms |
14584 KB |
Output is correct |
32 |
Correct |
14 ms |
14584 KB |
Output is correct |
33 |
Correct |
16 ms |
14584 KB |
Output is correct |
34 |
Correct |
16 ms |
14712 KB |
Output is correct |
35 |
Correct |
17 ms |
14840 KB |
Output is correct |
36 |
Correct |
17 ms |
14844 KB |
Output is correct |
37 |
Correct |
17 ms |
14840 KB |
Output is correct |
38 |
Correct |
17 ms |
14840 KB |
Output is correct |
39 |
Correct |
18 ms |
14840 KB |
Output is correct |
40 |
Correct |
16 ms |
15352 KB |
Output is correct |
41 |
Correct |
17 ms |
15352 KB |
Output is correct |
42 |
Correct |
17 ms |
14840 KB |
Output is correct |
43 |
Correct |
16 ms |
15224 KB |
Output is correct |
44 |
Correct |
18 ms |
15096 KB |
Output is correct |
45 |
Correct |
18 ms |
15096 KB |
Output is correct |
46 |
Correct |
17 ms |
15100 KB |
Output is correct |
47 |
Correct |
17 ms |
15096 KB |
Output is correct |
48 |
Correct |
18 ms |
14968 KB |
Output is correct |
49 |
Correct |
17 ms |
14968 KB |
Output is correct |
50 |
Correct |
16 ms |
14968 KB |
Output is correct |
51 |
Correct |
16 ms |
14968 KB |
Output is correct |
52 |
Correct |
18 ms |
14968 KB |
Output is correct |
53 |
Correct |
16 ms |
14968 KB |
Output is correct |
54 |
Correct |
17 ms |
15096 KB |
Output is correct |
55 |
Correct |
23 ms |
15736 KB |
Output is correct |
56 |
Correct |
58 ms |
19124 KB |
Output is correct |
57 |
Correct |
104 ms |
22236 KB |
Output is correct |
58 |
Correct |
132 ms |
24656 KB |
Output is correct |
59 |
Correct |
182 ms |
27964 KB |
Output is correct |
60 |
Correct |
232 ms |
30928 KB |
Output is correct |
61 |
Correct |
276 ms |
33748 KB |
Output is correct |
62 |
Correct |
323 ms |
39124 KB |
Output is correct |
63 |
Correct |
363 ms |
43496 KB |
Output is correct |
64 |
Correct |
403 ms |
45136 KB |
Output is correct |
65 |
Correct |
195 ms |
76792 KB |
Output is correct |
66 |
Correct |
195 ms |
76892 KB |
Output is correct |
67 |
Correct |
217 ms |
44152 KB |
Output is correct |
68 |
Correct |
267 ms |
65740 KB |
Output is correct |
69 |
Correct |
317 ms |
63204 KB |
Output is correct |
70 |
Correct |
315 ms |
63208 KB |
Output is correct |
71 |
Correct |
301 ms |
54052 KB |
Output is correct |
72 |
Execution timed out |
315 ms |
54140 KB |
Time limit exceeded (wall clock) |
73 |
Halted |
0 ms |
0 KB |
- |