#include <bits/stdc++.h>
#define CHECK cout << "ok" << endl
#define finish(x) return cout << x << endl, 0
typedef long long ll;
typedef long double ldb;
const int md = 1e9 + 7;
const ll inf = 1e18;
const int OO = 0;
const int OOO = 1;
using namespace std;
struct edge {
int to, w;
edge() {}
edge(int tt, int ww) {
to = tt;
w = ww;
}
};
struct func {
multiset<ll> s;
ll st;
func() {
clear();
}
void clear() {
s.clear();
s.insert(0);
s.insert(0);
st = 0;
}
void insert(ll x) {
s.insert(x);
}
void upd(const func &a) {
st += a.st;
for (const auto &i : a.s)
s.insert(i);
}
void shift(ll x) {
st += x;
vector<ll> v;
if (s.size()) v.push_back(*prev(s.end())), s.erase(prev(s.end()));
if (s.size()) v.push_back(*prev(s.end())), s.erase(prev(s.end()));
while (v.size()) {
s.insert(s.end(), v.back() + x);
v.pop_back();
}
}
void print() {
cout << "s: ";
for (const auto &i : s) cout << i << " "; cout << '\n';
cout << "start: " << st << '\n';
}
};
int n;
vector<vector<edge>> g;
vector<int> sz;
void dfs(int v, int pre, func &f) {
int big = -1, sh;
for (const auto &i : g[v])
if (i.to != pre && (big == -1 || sz[big] < sz[i.to])) big = i.to, sh = i.w;
if (big == -1) {
f = func();
return;
}
dfs(big, v, f);
f.shift(sh);
func f2;
for (const auto &i : g[v]) {
if (i.to != pre && i.to != big) {
f2.clear();
dfs(i.to, v, f2);
f2.shift(i.w);
f.upd(f2);
}
}
for (const auto &i : g[v])
if (i.to != pre && i.to != big)
f.s.erase(prev(f.s.end()));
if (OO) {
cout << "finishing node " << v << '\n';
f.print();
cout << '\n';
}
}
int dfs(int v = 0, int prev = 0) {
sz[v] = 1;
for (const auto &i : g[v])
if (i.to != prev)
sz[v] += dfs(i.to, v);
return sz[v];
}
int main() {
ios::sync_with_stdio(0), cin.tie(0);
int useless;
cin >> n >> useless;
n += useless;
g.resize(n);
for (int i = 1, p, w; i < n; i++) {
cin >> p >> w;
--p;
g[p].push_back(edge(i, w));
g[i].push_back(edge(p, w));
}
sz.resize(n);
dfs();
func f;
dfs(0, 0, f);
ll ans = f.st;
ll pos = 0;
ll slope = (ll)f.s.size() - 1;
auto it = f.s.begin();
while (slope > 0) {
ans -= slope * (*it - pos);
pos = *it;
slope--;
it++;
}
finish(ans);
}
Compilation message
fireworks.cpp: In member function 'void func::print()':
fireworks.cpp:53:3: warning: this 'for' clause does not guard... [-Wmisleading-indentation]
for (const auto &i : s) cout << i << " "; cout << '\n';
^~~
fireworks.cpp:53:45: note: ...this statement, but the latter is misleadingly indented as if it were guarded by the 'for'
for (const auto &i : s) cout << i << " "; cout << '\n';
^~~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
3 ms |
384 KB |
Output is correct |
2 |
Correct |
2 ms |
384 KB |
Output is correct |
3 |
Correct |
2 ms |
384 KB |
Output is correct |
4 |
Correct |
3 ms |
384 KB |
Output is correct |
5 |
Correct |
3 ms |
384 KB |
Output is correct |
6 |
Correct |
2 ms |
384 KB |
Output is correct |
7 |
Correct |
2 ms |
384 KB |
Output is correct |
8 |
Correct |
3 ms |
432 KB |
Output is correct |
9 |
Correct |
2 ms |
384 KB |
Output is correct |
10 |
Correct |
2 ms |
384 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
384 KB |
Output is correct |
2 |
Correct |
3 ms |
384 KB |
Output is correct |
3 |
Correct |
2 ms |
384 KB |
Output is correct |
4 |
Correct |
2 ms |
384 KB |
Output is correct |
5 |
Correct |
2 ms |
384 KB |
Output is correct |
6 |
Correct |
2 ms |
384 KB |
Output is correct |
7 |
Correct |
2 ms |
384 KB |
Output is correct |
8 |
Correct |
3 ms |
384 KB |
Output is correct |
9 |
Correct |
3 ms |
384 KB |
Output is correct |
10 |
Correct |
2 ms |
384 KB |
Output is correct |
11 |
Correct |
3 ms |
384 KB |
Output is correct |
12 |
Correct |
3 ms |
384 KB |
Output is correct |
13 |
Correct |
3 ms |
384 KB |
Output is correct |
14 |
Correct |
2 ms |
384 KB |
Output is correct |
15 |
Correct |
2 ms |
384 KB |
Output is correct |
16 |
Correct |
2 ms |
384 KB |
Output is correct |
17 |
Correct |
2 ms |
384 KB |
Output is correct |
18 |
Correct |
3 ms |
384 KB |
Output is correct |
19 |
Correct |
3 ms |
384 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
3 ms |
384 KB |
Output is correct |
2 |
Correct |
2 ms |
384 KB |
Output is correct |
3 |
Correct |
2 ms |
384 KB |
Output is correct |
4 |
Correct |
3 ms |
384 KB |
Output is correct |
5 |
Correct |
3 ms |
384 KB |
Output is correct |
6 |
Correct |
2 ms |
384 KB |
Output is correct |
7 |
Correct |
2 ms |
384 KB |
Output is correct |
8 |
Correct |
3 ms |
432 KB |
Output is correct |
9 |
Correct |
2 ms |
384 KB |
Output is correct |
10 |
Correct |
2 ms |
384 KB |
Output is correct |
11 |
Correct |
2 ms |
384 KB |
Output is correct |
12 |
Correct |
3 ms |
384 KB |
Output is correct |
13 |
Correct |
2 ms |
384 KB |
Output is correct |
14 |
Correct |
2 ms |
384 KB |
Output is correct |
15 |
Correct |
2 ms |
384 KB |
Output is correct |
16 |
Correct |
2 ms |
384 KB |
Output is correct |
17 |
Correct |
2 ms |
384 KB |
Output is correct |
18 |
Correct |
3 ms |
384 KB |
Output is correct |
19 |
Correct |
3 ms |
384 KB |
Output is correct |
20 |
Correct |
2 ms |
384 KB |
Output is correct |
21 |
Correct |
3 ms |
384 KB |
Output is correct |
22 |
Correct |
3 ms |
384 KB |
Output is correct |
23 |
Correct |
3 ms |
384 KB |
Output is correct |
24 |
Correct |
2 ms |
384 KB |
Output is correct |
25 |
Correct |
2 ms |
384 KB |
Output is correct |
26 |
Correct |
2 ms |
384 KB |
Output is correct |
27 |
Correct |
2 ms |
384 KB |
Output is correct |
28 |
Correct |
3 ms |
384 KB |
Output is correct |
29 |
Correct |
3 ms |
384 KB |
Output is correct |
30 |
Correct |
3 ms |
384 KB |
Output is correct |
31 |
Correct |
5 ms |
512 KB |
Output is correct |
32 |
Correct |
4 ms |
512 KB |
Output is correct |
33 |
Correct |
4 ms |
512 KB |
Output is correct |
34 |
Correct |
6 ms |
640 KB |
Output is correct |
35 |
Correct |
9 ms |
888 KB |
Output is correct |
36 |
Correct |
8 ms |
768 KB |
Output is correct |
37 |
Correct |
9 ms |
896 KB |
Output is correct |
38 |
Correct |
8 ms |
896 KB |
Output is correct |
39 |
Correct |
7 ms |
896 KB |
Output is correct |
40 |
Correct |
7 ms |
1920 KB |
Output is correct |
41 |
Correct |
8 ms |
1892 KB |
Output is correct |
42 |
Correct |
6 ms |
768 KB |
Output is correct |
43 |
Correct |
9 ms |
1408 KB |
Output is correct |
44 |
Correct |
10 ms |
1280 KB |
Output is correct |
45 |
Correct |
11 ms |
1280 KB |
Output is correct |
46 |
Correct |
12 ms |
1152 KB |
Output is correct |
47 |
Correct |
10 ms |
1024 KB |
Output is correct |
48 |
Correct |
7 ms |
1024 KB |
Output is correct |
49 |
Correct |
12 ms |
1024 KB |
Output is correct |
50 |
Correct |
8 ms |
908 KB |
Output is correct |
51 |
Correct |
9 ms |
1024 KB |
Output is correct |
52 |
Correct |
7 ms |
1024 KB |
Output is correct |
53 |
Correct |
9 ms |
1024 KB |
Output is correct |
54 |
Correct |
9 ms |
1280 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
3 ms |
384 KB |
Output is correct |
2 |
Correct |
2 ms |
384 KB |
Output is correct |
3 |
Correct |
2 ms |
384 KB |
Output is correct |
4 |
Correct |
3 ms |
384 KB |
Output is correct |
5 |
Correct |
3 ms |
384 KB |
Output is correct |
6 |
Correct |
2 ms |
384 KB |
Output is correct |
7 |
Correct |
2 ms |
384 KB |
Output is correct |
8 |
Correct |
3 ms |
432 KB |
Output is correct |
9 |
Correct |
2 ms |
384 KB |
Output is correct |
10 |
Correct |
2 ms |
384 KB |
Output is correct |
11 |
Correct |
2 ms |
384 KB |
Output is correct |
12 |
Correct |
3 ms |
384 KB |
Output is correct |
13 |
Correct |
2 ms |
384 KB |
Output is correct |
14 |
Correct |
2 ms |
384 KB |
Output is correct |
15 |
Correct |
2 ms |
384 KB |
Output is correct |
16 |
Correct |
2 ms |
384 KB |
Output is correct |
17 |
Correct |
2 ms |
384 KB |
Output is correct |
18 |
Correct |
3 ms |
384 KB |
Output is correct |
19 |
Correct |
3 ms |
384 KB |
Output is correct |
20 |
Correct |
2 ms |
384 KB |
Output is correct |
21 |
Correct |
3 ms |
384 KB |
Output is correct |
22 |
Correct |
3 ms |
384 KB |
Output is correct |
23 |
Correct |
3 ms |
384 KB |
Output is correct |
24 |
Correct |
2 ms |
384 KB |
Output is correct |
25 |
Correct |
2 ms |
384 KB |
Output is correct |
26 |
Correct |
2 ms |
384 KB |
Output is correct |
27 |
Correct |
2 ms |
384 KB |
Output is correct |
28 |
Correct |
3 ms |
384 KB |
Output is correct |
29 |
Correct |
3 ms |
384 KB |
Output is correct |
30 |
Correct |
3 ms |
384 KB |
Output is correct |
31 |
Correct |
5 ms |
512 KB |
Output is correct |
32 |
Correct |
4 ms |
512 KB |
Output is correct |
33 |
Correct |
4 ms |
512 KB |
Output is correct |
34 |
Correct |
6 ms |
640 KB |
Output is correct |
35 |
Correct |
9 ms |
888 KB |
Output is correct |
36 |
Correct |
8 ms |
768 KB |
Output is correct |
37 |
Correct |
9 ms |
896 KB |
Output is correct |
38 |
Correct |
8 ms |
896 KB |
Output is correct |
39 |
Correct |
7 ms |
896 KB |
Output is correct |
40 |
Correct |
7 ms |
1920 KB |
Output is correct |
41 |
Correct |
8 ms |
1892 KB |
Output is correct |
42 |
Correct |
6 ms |
768 KB |
Output is correct |
43 |
Correct |
9 ms |
1408 KB |
Output is correct |
44 |
Correct |
10 ms |
1280 KB |
Output is correct |
45 |
Correct |
11 ms |
1280 KB |
Output is correct |
46 |
Correct |
12 ms |
1152 KB |
Output is correct |
47 |
Correct |
10 ms |
1024 KB |
Output is correct |
48 |
Correct |
7 ms |
1024 KB |
Output is correct |
49 |
Correct |
12 ms |
1024 KB |
Output is correct |
50 |
Correct |
8 ms |
908 KB |
Output is correct |
51 |
Correct |
9 ms |
1024 KB |
Output is correct |
52 |
Correct |
7 ms |
1024 KB |
Output is correct |
53 |
Correct |
9 ms |
1024 KB |
Output is correct |
54 |
Correct |
9 ms |
1280 KB |
Output is correct |
55 |
Correct |
19 ms |
1792 KB |
Output is correct |
56 |
Correct |
82 ms |
5240 KB |
Output is correct |
57 |
Correct |
148 ms |
8796 KB |
Output is correct |
58 |
Correct |
201 ms |
11256 KB |
Output is correct |
59 |
Correct |
288 ms |
14968 KB |
Output is correct |
60 |
Correct |
360 ms |
18296 KB |
Output is correct |
61 |
Correct |
395 ms |
20912 KB |
Output is correct |
62 |
Correct |
461 ms |
23688 KB |
Output is correct |
63 |
Correct |
562 ms |
27640 KB |
Output is correct |
64 |
Correct |
551 ms |
28676 KB |
Output is correct |
65 |
Correct |
291 ms |
93436 KB |
Output is correct |
66 |
Correct |
287 ms |
93312 KB |
Output is correct |
67 |
Correct |
287 ms |
23292 KB |
Output is correct |
68 |
Correct |
491 ms |
67320 KB |
Output is correct |
69 |
Correct |
528 ms |
61432 KB |
Output is correct |
70 |
Correct |
520 ms |
61292 KB |
Output is correct |
71 |
Correct |
578 ms |
40252 KB |
Output is correct |
72 |
Correct |
552 ms |
40136 KB |
Output is correct |
73 |
Correct |
599 ms |
39568 KB |
Output is correct |
74 |
Correct |
617 ms |
39312 KB |
Output is correct |
75 |
Correct |
642 ms |
38776 KB |
Output is correct |
76 |
Correct |
706 ms |
38776 KB |
Output is correct |
77 |
Correct |
844 ms |
38132 KB |
Output is correct |
78 |
Correct |
720 ms |
38904 KB |
Output is correct |
79 |
Correct |
1020 ms |
52052 KB |
Output is correct |