#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) {
S[v] = 1;
int maxV = -1, ix = -1;
for(pi &e : Ed[v]) {
int w, c; tie(w, c) = e;
if(w == P[v]) continue;
dfs(w);
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[v] || 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);
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);
^
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
13748 KB |
Output is correct |
2 |
Correct |
3 ms |
13748 KB |
Output is correct |
3 |
Correct |
3 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 |
0 ms |
13748 KB |
Output is correct |
9 |
Correct |
0 ms |
13748 KB |
Output is correct |
10 |
Correct |
0 ms |
13748 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
13748 KB |
Output is correct |
2 |
Correct |
3 ms |
13748 KB |
Output is correct |
3 |
Correct |
3 ms |
13748 KB |
Output is correct |
4 |
Correct |
3 ms |
13748 KB |
Output is correct |
5 |
Correct |
3 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 |
0 ms |
13748 KB |
Output is correct |
10 |
Correct |
0 ms |
13748 KB |
Output is correct |
11 |
Correct |
0 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 |
3 ms |
13880 KB |
Output is correct |
16 |
Correct |
0 ms |
13880 KB |
Output is correct |
17 |
Correct |
0 ms |
13880 KB |
Output is correct |
18 |
Correct |
3 ms |
13880 KB |
Output is correct |
19 |
Correct |
0 ms |
13880 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
13748 KB |
Output is correct |
2 |
Correct |
3 ms |
13748 KB |
Output is correct |
3 |
Correct |
3 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 |
0 ms |
13748 KB |
Output is correct |
9 |
Correct |
0 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 |
3 ms |
13748 KB |
Output is correct |
13 |
Correct |
3 ms |
13748 KB |
Output is correct |
14 |
Correct |
3 ms |
13748 KB |
Output is correct |
15 |
Correct |
3 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 |
0 ms |
13748 KB |
Output is correct |
19 |
Correct |
0 ms |
13748 KB |
Output is correct |
20 |
Correct |
0 ms |
13748 KB |
Output is correct |
21 |
Correct |
0 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 |
3 ms |
13880 KB |
Output is correct |
26 |
Correct |
0 ms |
13880 KB |
Output is correct |
27 |
Correct |
0 ms |
13880 KB |
Output is correct |
28 |
Correct |
3 ms |
13880 KB |
Output is correct |
29 |
Correct |
0 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 |
3 ms |
14224 KB |
Output is correct |
36 |
Correct |
6 ms |
14328 KB |
Output is correct |
37 |
Correct |
3 ms |
14352 KB |
Output is correct |
38 |
Correct |
3 ms |
14360 KB |
Output is correct |
39 |
Correct |
6 ms |
14360 KB |
Output is correct |
40 |
Correct |
6 ms |
14768 KB |
Output is correct |
41 |
Correct |
3 ms |
14768 KB |
Output is correct |
42 |
Correct |
6 ms |
13880 KB |
Output is correct |
43 |
Correct |
3 ms |
15008 KB |
Output is correct |
44 |
Correct |
6 ms |
14852 KB |
Output is correct |
45 |
Correct |
6 ms |
14856 KB |
Output is correct |
46 |
Correct |
9 ms |
15456 KB |
Output is correct |
47 |
Correct |
3 ms |
15468 KB |
Output is correct |
48 |
Correct |
3 ms |
15284 KB |
Output is correct |
49 |
Correct |
6 ms |
15304 KB |
Output is correct |
50 |
Correct |
6 ms |
15268 KB |
Output is correct |
51 |
Correct |
6 ms |
15264 KB |
Output is correct |
52 |
Correct |
3 ms |
15276 KB |
Output is correct |
53 |
Correct |
3 ms |
15420 KB |
Output is correct |
54 |
Correct |
6 ms |
15304 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
13748 KB |
Output is correct |
2 |
Correct |
3 ms |
13748 KB |
Output is correct |
3 |
Correct |
3 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 |
0 ms |
13748 KB |
Output is correct |
9 |
Correct |
0 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 |
3 ms |
13748 KB |
Output is correct |
13 |
Correct |
3 ms |
13748 KB |
Output is correct |
14 |
Correct |
3 ms |
13748 KB |
Output is correct |
15 |
Correct |
3 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 |
0 ms |
13748 KB |
Output is correct |
19 |
Correct |
0 ms |
13748 KB |
Output is correct |
20 |
Correct |
0 ms |
13748 KB |
Output is correct |
21 |
Correct |
0 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 |
3 ms |
13880 KB |
Output is correct |
26 |
Correct |
0 ms |
13880 KB |
Output is correct |
27 |
Correct |
0 ms |
13880 KB |
Output is correct |
28 |
Correct |
3 ms |
13880 KB |
Output is correct |
29 |
Correct |
0 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 |
3 ms |
14224 KB |
Output is correct |
36 |
Correct |
6 ms |
14328 KB |
Output is correct |
37 |
Correct |
3 ms |
14352 KB |
Output is correct |
38 |
Correct |
3 ms |
14360 KB |
Output is correct |
39 |
Correct |
6 ms |
14360 KB |
Output is correct |
40 |
Correct |
6 ms |
14768 KB |
Output is correct |
41 |
Correct |
3 ms |
14768 KB |
Output is correct |
42 |
Correct |
6 ms |
13880 KB |
Output is correct |
43 |
Correct |
3 ms |
15008 KB |
Output is correct |
44 |
Correct |
6 ms |
14852 KB |
Output is correct |
45 |
Correct |
6 ms |
14856 KB |
Output is correct |
46 |
Correct |
9 ms |
15456 KB |
Output is correct |
47 |
Correct |
3 ms |
15468 KB |
Output is correct |
48 |
Correct |
3 ms |
15284 KB |
Output is correct |
49 |
Correct |
6 ms |
15304 KB |
Output is correct |
50 |
Correct |
6 ms |
15268 KB |
Output is correct |
51 |
Correct |
6 ms |
15264 KB |
Output is correct |
52 |
Correct |
3 ms |
15276 KB |
Output is correct |
53 |
Correct |
3 ms |
15420 KB |
Output is correct |
54 |
Correct |
6 ms |
15304 KB |
Output is correct |
55 |
Correct |
6 ms |
15668 KB |
Output is correct |
56 |
Correct |
53 ms |
21436 KB |
Output is correct |
57 |
Correct |
89 ms |
22856 KB |
Output is correct |
58 |
Correct |
149 ms |
29480 KB |
Output is correct |
59 |
Correct |
209 ms |
30700 KB |
Output is correct |
60 |
Correct |
273 ms |
32524 KB |
Output is correct |
61 |
Correct |
309 ms |
44788 KB |
Output is correct |
62 |
Correct |
339 ms |
45556 KB |
Output is correct |
63 |
Correct |
386 ms |
46912 KB |
Output is correct |
64 |
Correct |
439 ms |
47668 KB |
Output is correct |
65 |
Correct |
179 ms |
83932 KB |
Output is correct |
66 |
Correct |
146 ms |
83932 KB |
Output is correct |
67 |
Correct |
216 ms |
23412 KB |
Output is correct |
68 |
Correct |
269 ms |
98888 KB |
Output is correct |
69 |
Correct |
313 ms |
93516 KB |
Output is correct |
70 |
Correct |
393 ms |
93520 KB |
Output is correct |
71 |
Correct |
349 ms |
119464 KB |
Output is correct |
72 |
Correct |
476 ms |
119444 KB |
Output is correct |
73 |
Correct |
369 ms |
115732 KB |
Output is correct |
74 |
Correct |
313 ms |
118792 KB |
Output is correct |
75 |
Correct |
409 ms |
116692 KB |
Output is correct |
76 |
Correct |
336 ms |
116756 KB |
Output is correct |
77 |
Correct |
403 ms |
116224 KB |
Output is correct |
78 |
Correct |
369 ms |
115888 KB |
Output is correct |
79 |
Correct |
356 ms |
111172 KB |
Output is correct |