#include <bits/stdc++.h>
using namespace std;
#define REP(i,n) for(int (i)=0;(i)<(int)(n);(i)++)
#define REPO(i,n) for(int (i)=1; (i)<=(int)(n); (i)++)
#define SZ(v) ((int)(v).size())
#define ALL(v) (v).begin(),(v).end()
#define one first
#define two second
typedef long long ll;
typedef pair<int, int> pi;
const int INF = 0x3f2f1f0f;
const ll LINF = 1ll * INF * INF;
const int MAX_N = 3e5 + 100;
int N, M, S[MAX_N], P[MAX_N], C[MAX_N];
vector<pi> Ed[MAX_N];
struct SL {
priority_queue<ll> Ls;
priority_queue<ll, vector<ll>, greater<ll> > Rs;
ll baseL, baseR, ans;
SL() {
baseL = baseR = ans = 0;
while(!Ls.empty()) Ls.pop();
while(!Rs.empty()) Rs.pop();
}
void update() {
while(Ls.top()+baseL > Rs.top()+baseR) {
ll a = Ls.top()+baseL;
ll b = Rs.top()+baseR;
ans += a-b;
Ls.pop(); Rs.pop();
Ls.push(b-baseL); Rs.push(a-baseR);
}
}
void add(int l, int r) {
Ls.push(l-baseL), Rs.push(r-baseR);
update();
}
void add(SL o) {
ans += o.ans;
while(!o.Ls.empty()) Ls.push(o.Ls.top() + o.baseL - baseL), o.Ls.pop();
while(!o.Rs.empty()) Rs.push(o.Rs.top() + o.baseR - baseR), o.Rs.pop();
update();
}
void addLine(int b) {
baseR += b; baseL += 0;
ll l = Ls.top(), r = Rs.top();
while(!Rs.empty()) Rs.pop();
Ls.pop();
Ls.push(l+b);
Rs.push(r);
update();
}
};
vector<SL> Ss; int Ix[MAX_N];
void dfs(int v, int p) {
S[v] = 1;
int maxV = -1, ix = -1;
for(pi &e : Ed[v]) {
int w, c; tie(w, c) = e;
if(w == p) continue;
dfs(w, v);
S[v] += S[w];
if(maxV < S[w]) maxV = S[w], ix = w;
}
if(ix == -1) {
int c = C[v];
SL node; node.add(c, c);
Ix[v] = Ss.size();
Ss.push_back(node);
} else {
Ix[v] = Ix[ix];
for(pi &e : Ed[v]) {
int w, c; tie(w, c) = e;
if(w == p || w == ix) continue;
Ss[Ix[v]].add(Ss[Ix[w]]);
}
Ss[Ix[v]].addLine(C[v]);
}
}
int main() {
cin >> N >> M; N += M;
for(int i=2; i<=N; i++) {
int p, c; scanf("%d%d", &p, &c);
P[i] = p; C[i] = c;
Ed[i].push_back(pi(p, c));
Ed[p].push_back(pi(i, c));
}
dfs(1, 0);
printf("%lld\n", Ss[Ix[1]].ans);
return 0;
}
Compilation message
fireworks.cpp: In function 'int main()':
fireworks.cpp:87:34: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
int p, c; scanf("%d%d", &p, &c);
^
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
3 ms |
13748 KB |
Output is correct |
2 |
Correct |
0 ms |
13748 KB |
Output is correct |
3 |
Correct |
0 ms |
13748 KB |
Output is correct |
4 |
Correct |
3 ms |
13748 KB |
Output is correct |
5 |
Correct |
0 ms |
13748 KB |
Output is correct |
6 |
Correct |
0 ms |
13748 KB |
Output is correct |
7 |
Correct |
0 ms |
13748 KB |
Output is correct |
8 |
Correct |
0 ms |
13748 KB |
Output is correct |
9 |
Correct |
3 ms |
13748 KB |
Output is correct |
10 |
Correct |
0 ms |
13748 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
3 ms |
13748 KB |
Output is correct |
2 |
Correct |
0 ms |
13748 KB |
Output is correct |
3 |
Correct |
0 ms |
13748 KB |
Output is correct |
4 |
Correct |
0 ms |
13748 KB |
Output is correct |
5 |
Correct |
0 ms |
13748 KB |
Output is correct |
6 |
Correct |
0 ms |
13748 KB |
Output is correct |
7 |
Correct |
0 ms |
13748 KB |
Output is correct |
8 |
Correct |
3 ms |
13748 KB |
Output is correct |
9 |
Correct |
0 ms |
13748 KB |
Output is correct |
10 |
Correct |
6 ms |
13748 KB |
Output is correct |
11 |
Correct |
3 ms |
13748 KB |
Output is correct |
12 |
Correct |
0 ms |
13880 KB |
Output is correct |
13 |
Correct |
3 ms |
13880 KB |
Output is correct |
14 |
Correct |
0 ms |
13748 KB |
Output is correct |
15 |
Correct |
0 ms |
13880 KB |
Output is correct |
16 |
Correct |
0 ms |
13880 KB |
Output is correct |
17 |
Correct |
3 ms |
13880 KB |
Output is correct |
18 |
Correct |
3 ms |
13880 KB |
Output is correct |
19 |
Correct |
6 ms |
13880 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
3 ms |
13748 KB |
Output is correct |
2 |
Correct |
0 ms |
13748 KB |
Output is correct |
3 |
Correct |
0 ms |
13748 KB |
Output is correct |
4 |
Correct |
3 ms |
13748 KB |
Output is correct |
5 |
Correct |
0 ms |
13748 KB |
Output is correct |
6 |
Correct |
0 ms |
13748 KB |
Output is correct |
7 |
Correct |
0 ms |
13748 KB |
Output is correct |
8 |
Correct |
0 ms |
13748 KB |
Output is correct |
9 |
Correct |
3 ms |
13748 KB |
Output is correct |
10 |
Correct |
0 ms |
13748 KB |
Output is correct |
11 |
Correct |
3 ms |
13748 KB |
Output is correct |
12 |
Correct |
0 ms |
13748 KB |
Output is correct |
13 |
Correct |
0 ms |
13748 KB |
Output is correct |
14 |
Correct |
0 ms |
13748 KB |
Output is correct |
15 |
Correct |
0 ms |
13748 KB |
Output is correct |
16 |
Correct |
0 ms |
13748 KB |
Output is correct |
17 |
Correct |
0 ms |
13748 KB |
Output is correct |
18 |
Correct |
3 ms |
13748 KB |
Output is correct |
19 |
Correct |
0 ms |
13748 KB |
Output is correct |
20 |
Correct |
6 ms |
13748 KB |
Output is correct |
21 |
Correct |
3 ms |
13748 KB |
Output is correct |
22 |
Correct |
0 ms |
13880 KB |
Output is correct |
23 |
Correct |
3 ms |
13880 KB |
Output is correct |
24 |
Correct |
0 ms |
13748 KB |
Output is correct |
25 |
Correct |
0 ms |
13880 KB |
Output is correct |
26 |
Correct |
0 ms |
13880 KB |
Output is correct |
27 |
Correct |
3 ms |
13880 KB |
Output is correct |
28 |
Correct |
3 ms |
13880 KB |
Output is correct |
29 |
Correct |
6 ms |
13880 KB |
Output is correct |
30 |
Correct |
0 ms |
13884 KB |
Output is correct |
31 |
Correct |
3 ms |
13888 KB |
Output is correct |
32 |
Correct |
3 ms |
14060 KB |
Output is correct |
33 |
Correct |
3 ms |
14080 KB |
Output is correct |
34 |
Correct |
3 ms |
14196 KB |
Output is correct |
35 |
Correct |
6 ms |
14224 KB |
Output is correct |
36 |
Correct |
3 ms |
14328 KB |
Output is correct |
37 |
Correct |
6 ms |
14352 KB |
Output is correct |
38 |
Correct |
9 ms |
14360 KB |
Output is correct |
39 |
Correct |
6 ms |
14360 KB |
Output is correct |
40 |
Correct |
3 ms |
14844 KB |
Output is correct |
41 |
Correct |
6 ms |
14844 KB |
Output is correct |
42 |
Correct |
3 ms |
13880 KB |
Output is correct |
43 |
Correct |
6 ms |
15052 KB |
Output is correct |
44 |
Correct |
6 ms |
14876 KB |
Output is correct |
45 |
Correct |
3 ms |
14880 KB |
Output is correct |
46 |
Correct |
9 ms |
15456 KB |
Output is correct |
47 |
Correct |
3 ms |
15468 KB |
Output is correct |
48 |
Correct |
6 ms |
15284 KB |
Output is correct |
49 |
Correct |
13 ms |
15304 KB |
Output is correct |
50 |
Correct |
13 ms |
15268 KB |
Output is correct |
51 |
Correct |
3 ms |
15264 KB |
Output is correct |
52 |
Correct |
6 ms |
15276 KB |
Output is correct |
53 |
Correct |
6 ms |
15420 KB |
Output is correct |
54 |
Correct |
13 ms |
15304 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
3 ms |
13748 KB |
Output is correct |
2 |
Correct |
0 ms |
13748 KB |
Output is correct |
3 |
Correct |
0 ms |
13748 KB |
Output is correct |
4 |
Correct |
3 ms |
13748 KB |
Output is correct |
5 |
Correct |
0 ms |
13748 KB |
Output is correct |
6 |
Correct |
0 ms |
13748 KB |
Output is correct |
7 |
Correct |
0 ms |
13748 KB |
Output is correct |
8 |
Correct |
0 ms |
13748 KB |
Output is correct |
9 |
Correct |
3 ms |
13748 KB |
Output is correct |
10 |
Correct |
0 ms |
13748 KB |
Output is correct |
11 |
Correct |
3 ms |
13748 KB |
Output is correct |
12 |
Correct |
0 ms |
13748 KB |
Output is correct |
13 |
Correct |
0 ms |
13748 KB |
Output is correct |
14 |
Correct |
0 ms |
13748 KB |
Output is correct |
15 |
Correct |
0 ms |
13748 KB |
Output is correct |
16 |
Correct |
0 ms |
13748 KB |
Output is correct |
17 |
Correct |
0 ms |
13748 KB |
Output is correct |
18 |
Correct |
3 ms |
13748 KB |
Output is correct |
19 |
Correct |
0 ms |
13748 KB |
Output is correct |
20 |
Correct |
6 ms |
13748 KB |
Output is correct |
21 |
Correct |
3 ms |
13748 KB |
Output is correct |
22 |
Correct |
0 ms |
13880 KB |
Output is correct |
23 |
Correct |
3 ms |
13880 KB |
Output is correct |
24 |
Correct |
0 ms |
13748 KB |
Output is correct |
25 |
Correct |
0 ms |
13880 KB |
Output is correct |
26 |
Correct |
0 ms |
13880 KB |
Output is correct |
27 |
Correct |
3 ms |
13880 KB |
Output is correct |
28 |
Correct |
3 ms |
13880 KB |
Output is correct |
29 |
Correct |
6 ms |
13880 KB |
Output is correct |
30 |
Correct |
0 ms |
13884 KB |
Output is correct |
31 |
Correct |
3 ms |
13888 KB |
Output is correct |
32 |
Correct |
3 ms |
14060 KB |
Output is correct |
33 |
Correct |
3 ms |
14080 KB |
Output is correct |
34 |
Correct |
3 ms |
14196 KB |
Output is correct |
35 |
Correct |
6 ms |
14224 KB |
Output is correct |
36 |
Correct |
3 ms |
14328 KB |
Output is correct |
37 |
Correct |
6 ms |
14352 KB |
Output is correct |
38 |
Correct |
9 ms |
14360 KB |
Output is correct |
39 |
Correct |
6 ms |
14360 KB |
Output is correct |
40 |
Correct |
3 ms |
14844 KB |
Output is correct |
41 |
Correct |
6 ms |
14844 KB |
Output is correct |
42 |
Correct |
3 ms |
13880 KB |
Output is correct |
43 |
Correct |
6 ms |
15052 KB |
Output is correct |
44 |
Correct |
6 ms |
14876 KB |
Output is correct |
45 |
Correct |
3 ms |
14880 KB |
Output is correct |
46 |
Correct |
9 ms |
15456 KB |
Output is correct |
47 |
Correct |
3 ms |
15468 KB |
Output is correct |
48 |
Correct |
6 ms |
15284 KB |
Output is correct |
49 |
Correct |
13 ms |
15304 KB |
Output is correct |
50 |
Correct |
13 ms |
15268 KB |
Output is correct |
51 |
Correct |
3 ms |
15264 KB |
Output is correct |
52 |
Correct |
6 ms |
15276 KB |
Output is correct |
53 |
Correct |
6 ms |
15420 KB |
Output is correct |
54 |
Correct |
13 ms |
15304 KB |
Output is correct |
55 |
Correct |
16 ms |
15668 KB |
Output is correct |
56 |
Correct |
53 ms |
21436 KB |
Output is correct |
57 |
Correct |
119 ms |
22864 KB |
Output is correct |
58 |
Correct |
136 ms |
29496 KB |
Output is correct |
59 |
Correct |
199 ms |
30720 KB |
Output is correct |
60 |
Correct |
266 ms |
32528 KB |
Output is correct |
61 |
Correct |
316 ms |
44804 KB |
Output is correct |
62 |
Correct |
313 ms |
45576 KB |
Output is correct |
63 |
Correct |
416 ms |
46928 KB |
Output is correct |
64 |
Correct |
436 ms |
47684 KB |
Output is correct |
65 |
Correct |
186 ms |
88620 KB |
Output is correct |
66 |
Correct |
176 ms |
88616 KB |
Output is correct |
67 |
Correct |
223 ms |
23428 KB |
Output is correct |
68 |
Correct |
303 ms |
101228 KB |
Output is correct |
69 |
Correct |
383 ms |
95472 KB |
Output is correct |
70 |
Correct |
369 ms |
95476 KB |
Output is correct |
71 |
Correct |
366 ms |
119464 KB |
Output is correct |
72 |
Correct |
369 ms |
119444 KB |
Output is correct |
73 |
Correct |
349 ms |
115732 KB |
Output is correct |
74 |
Correct |
379 ms |
118792 KB |
Output is correct |
75 |
Correct |
343 ms |
116692 KB |
Output is correct |
76 |
Correct |
373 ms |
116756 KB |
Output is correct |
77 |
Correct |
356 ms |
116224 KB |
Output is correct |
78 |
Correct |
396 ms |
115888 KB |
Output is correct |
79 |
Correct |
383 ms |
111172 KB |
Output is correct |