#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(ll l, ll 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);
^
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
13744 KB |
Output is correct |
2 |
Correct |
3 ms |
13744 KB |
Output is correct |
3 |
Correct |
3 ms |
13744 KB |
Output is correct |
4 |
Correct |
3 ms |
13744 KB |
Output is correct |
5 |
Correct |
3 ms |
13744 KB |
Output is correct |
6 |
Correct |
0 ms |
13744 KB |
Output is correct |
7 |
Correct |
0 ms |
13744 KB |
Output is correct |
8 |
Correct |
0 ms |
13744 KB |
Output is correct |
9 |
Correct |
0 ms |
13744 KB |
Output is correct |
10 |
Correct |
0 ms |
13744 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
13744 KB |
Output is correct |
2 |
Correct |
3 ms |
13744 KB |
Output is correct |
3 |
Correct |
0 ms |
13744 KB |
Output is correct |
4 |
Correct |
3 ms |
13744 KB |
Output is correct |
5 |
Correct |
0 ms |
13744 KB |
Output is correct |
6 |
Correct |
0 ms |
13744 KB |
Output is correct |
7 |
Correct |
0 ms |
13744 KB |
Output is correct |
8 |
Correct |
3 ms |
13744 KB |
Output is correct |
9 |
Correct |
0 ms |
13744 KB |
Output is correct |
10 |
Correct |
0 ms |
13744 KB |
Output is correct |
11 |
Correct |
0 ms |
13744 KB |
Output is correct |
12 |
Correct |
0 ms |
13876 KB |
Output is correct |
13 |
Correct |
0 ms |
13876 KB |
Output is correct |
14 |
Correct |
0 ms |
13744 KB |
Output is correct |
15 |
Correct |
3 ms |
13876 KB |
Output is correct |
16 |
Correct |
0 ms |
13876 KB |
Output is correct |
17 |
Correct |
0 ms |
13876 KB |
Output is correct |
18 |
Correct |
3 ms |
13876 KB |
Output is correct |
19 |
Correct |
3 ms |
13876 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
13744 KB |
Output is correct |
2 |
Correct |
3 ms |
13744 KB |
Output is correct |
3 |
Correct |
3 ms |
13744 KB |
Output is correct |
4 |
Correct |
3 ms |
13744 KB |
Output is correct |
5 |
Correct |
3 ms |
13744 KB |
Output is correct |
6 |
Correct |
0 ms |
13744 KB |
Output is correct |
7 |
Correct |
0 ms |
13744 KB |
Output is correct |
8 |
Correct |
0 ms |
13744 KB |
Output is correct |
9 |
Correct |
0 ms |
13744 KB |
Output is correct |
10 |
Correct |
0 ms |
13744 KB |
Output is correct |
11 |
Correct |
3 ms |
13744 KB |
Output is correct |
12 |
Correct |
3 ms |
13744 KB |
Output is correct |
13 |
Correct |
0 ms |
13744 KB |
Output is correct |
14 |
Correct |
3 ms |
13744 KB |
Output is correct |
15 |
Correct |
0 ms |
13744 KB |
Output is correct |
16 |
Correct |
0 ms |
13744 KB |
Output is correct |
17 |
Correct |
0 ms |
13744 KB |
Output is correct |
18 |
Correct |
3 ms |
13744 KB |
Output is correct |
19 |
Correct |
0 ms |
13744 KB |
Output is correct |
20 |
Correct |
0 ms |
13744 KB |
Output is correct |
21 |
Correct |
0 ms |
13744 KB |
Output is correct |
22 |
Correct |
0 ms |
13876 KB |
Output is correct |
23 |
Correct |
0 ms |
13876 KB |
Output is correct |
24 |
Correct |
0 ms |
13744 KB |
Output is correct |
25 |
Correct |
3 ms |
13876 KB |
Output is correct |
26 |
Correct |
0 ms |
13876 KB |
Output is correct |
27 |
Correct |
0 ms |
13876 KB |
Output is correct |
28 |
Correct |
3 ms |
13876 KB |
Output is correct |
29 |
Correct |
3 ms |
13876 KB |
Output is correct |
30 |
Correct |
3 ms |
13880 KB |
Output is correct |
31 |
Correct |
3 ms |
13884 KB |
Output is correct |
32 |
Correct |
3 ms |
14056 KB |
Output is correct |
33 |
Correct |
6 ms |
14076 KB |
Output is correct |
34 |
Correct |
3 ms |
14192 KB |
Output is correct |
35 |
Correct |
3 ms |
14220 KB |
Output is correct |
36 |
Correct |
9 ms |
14324 KB |
Output is correct |
37 |
Correct |
6 ms |
14348 KB |
Output is correct |
38 |
Correct |
0 ms |
14356 KB |
Output is correct |
39 |
Correct |
3 ms |
14356 KB |
Output is correct |
40 |
Correct |
3 ms |
14848 KB |
Output is correct |
41 |
Correct |
3 ms |
14844 KB |
Output is correct |
42 |
Correct |
0 ms |
13876 KB |
Output is correct |
43 |
Correct |
3 ms |
15044 KB |
Output is correct |
44 |
Correct |
0 ms |
14872 KB |
Output is correct |
45 |
Correct |
6 ms |
14876 KB |
Output is correct |
46 |
Correct |
0 ms |
15452 KB |
Output is correct |
47 |
Correct |
9 ms |
15464 KB |
Output is correct |
48 |
Correct |
6 ms |
15280 KB |
Output is correct |
49 |
Correct |
3 ms |
15300 KB |
Output is correct |
50 |
Correct |
6 ms |
15264 KB |
Output is correct |
51 |
Correct |
6 ms |
15260 KB |
Output is correct |
52 |
Correct |
0 ms |
15272 KB |
Output is correct |
53 |
Correct |
6 ms |
15416 KB |
Output is correct |
54 |
Correct |
9 ms |
15300 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
13744 KB |
Output is correct |
2 |
Correct |
3 ms |
13744 KB |
Output is correct |
3 |
Correct |
3 ms |
13744 KB |
Output is correct |
4 |
Correct |
3 ms |
13744 KB |
Output is correct |
5 |
Correct |
3 ms |
13744 KB |
Output is correct |
6 |
Correct |
0 ms |
13744 KB |
Output is correct |
7 |
Correct |
0 ms |
13744 KB |
Output is correct |
8 |
Correct |
0 ms |
13744 KB |
Output is correct |
9 |
Correct |
0 ms |
13744 KB |
Output is correct |
10 |
Correct |
0 ms |
13744 KB |
Output is correct |
11 |
Correct |
3 ms |
13744 KB |
Output is correct |
12 |
Correct |
3 ms |
13744 KB |
Output is correct |
13 |
Correct |
0 ms |
13744 KB |
Output is correct |
14 |
Correct |
3 ms |
13744 KB |
Output is correct |
15 |
Correct |
0 ms |
13744 KB |
Output is correct |
16 |
Correct |
0 ms |
13744 KB |
Output is correct |
17 |
Correct |
0 ms |
13744 KB |
Output is correct |
18 |
Correct |
3 ms |
13744 KB |
Output is correct |
19 |
Correct |
0 ms |
13744 KB |
Output is correct |
20 |
Correct |
0 ms |
13744 KB |
Output is correct |
21 |
Correct |
0 ms |
13744 KB |
Output is correct |
22 |
Correct |
0 ms |
13876 KB |
Output is correct |
23 |
Correct |
0 ms |
13876 KB |
Output is correct |
24 |
Correct |
0 ms |
13744 KB |
Output is correct |
25 |
Correct |
3 ms |
13876 KB |
Output is correct |
26 |
Correct |
0 ms |
13876 KB |
Output is correct |
27 |
Correct |
0 ms |
13876 KB |
Output is correct |
28 |
Correct |
3 ms |
13876 KB |
Output is correct |
29 |
Correct |
3 ms |
13876 KB |
Output is correct |
30 |
Correct |
3 ms |
13880 KB |
Output is correct |
31 |
Correct |
3 ms |
13884 KB |
Output is correct |
32 |
Correct |
3 ms |
14056 KB |
Output is correct |
33 |
Correct |
6 ms |
14076 KB |
Output is correct |
34 |
Correct |
3 ms |
14192 KB |
Output is correct |
35 |
Correct |
3 ms |
14220 KB |
Output is correct |
36 |
Correct |
9 ms |
14324 KB |
Output is correct |
37 |
Correct |
6 ms |
14348 KB |
Output is correct |
38 |
Correct |
0 ms |
14356 KB |
Output is correct |
39 |
Correct |
3 ms |
14356 KB |
Output is correct |
40 |
Correct |
3 ms |
14848 KB |
Output is correct |
41 |
Correct |
3 ms |
14844 KB |
Output is correct |
42 |
Correct |
0 ms |
13876 KB |
Output is correct |
43 |
Correct |
3 ms |
15044 KB |
Output is correct |
44 |
Correct |
0 ms |
14872 KB |
Output is correct |
45 |
Correct |
6 ms |
14876 KB |
Output is correct |
46 |
Correct |
0 ms |
15452 KB |
Output is correct |
47 |
Correct |
9 ms |
15464 KB |
Output is correct |
48 |
Correct |
6 ms |
15280 KB |
Output is correct |
49 |
Correct |
3 ms |
15300 KB |
Output is correct |
50 |
Correct |
6 ms |
15264 KB |
Output is correct |
51 |
Correct |
6 ms |
15260 KB |
Output is correct |
52 |
Correct |
0 ms |
15272 KB |
Output is correct |
53 |
Correct |
6 ms |
15416 KB |
Output is correct |
54 |
Correct |
9 ms |
15300 KB |
Output is correct |
55 |
Correct |
13 ms |
15664 KB |
Output is correct |
56 |
Correct |
63 ms |
21380 KB |
Output is correct |
57 |
Correct |
113 ms |
22812 KB |
Output is correct |
58 |
Correct |
146 ms |
29440 KB |
Output is correct |
59 |
Correct |
193 ms |
30684 KB |
Output is correct |
60 |
Correct |
266 ms |
32632 KB |
Output is correct |
61 |
Correct |
299 ms |
44776 KB |
Output is correct |
62 |
Correct |
303 ms |
45568 KB |
Output is correct |
63 |
Correct |
403 ms |
46880 KB |
Output is correct |
64 |
Correct |
466 ms |
47620 KB |
Output is correct |
65 |
Correct |
196 ms |
88616 KB |
Output is correct |
66 |
Correct |
159 ms |
88620 KB |
Output is correct |
67 |
Correct |
213 ms |
23424 KB |
Output is correct |
68 |
Correct |
263 ms |
101224 KB |
Output is correct |
69 |
Correct |
376 ms |
95468 KB |
Output is correct |
70 |
Correct |
349 ms |
95468 KB |
Output is correct |
71 |
Correct |
346 ms |
119460 KB |
Output is correct |
72 |
Correct |
393 ms |
119440 KB |
Output is correct |
73 |
Correct |
303 ms |
115728 KB |
Output is correct |
74 |
Correct |
336 ms |
118784 KB |
Output is correct |
75 |
Correct |
406 ms |
116688 KB |
Output is correct |
76 |
Correct |
316 ms |
116752 KB |
Output is correct |
77 |
Correct |
329 ms |
116220 KB |
Output is correct |
78 |
Correct |
373 ms |
115880 KB |
Output is correct |
79 |
Correct |
333 ms |
111168 KB |
Output is correct |