#include <bits/stdc++.h>
using namespace std;
typedef pair<int, long long> ii;
vector<ii> adj[300005];
long long val[300005];
long long pw[300005];
priority_queue<long long> pq[300005];
void dfs(int u){
if(adj[u].size() == 0){
pq[u].push(pw[u]);
pq[u].push(pw[u]);
val[u] = 0;
return;
}
long long highest = 0;
for(long long i = 0;i < adj[u].size();i++){
int v = adj[u][i].first;
long long w = adj[u][i].second;
dfs(v);
if(pq[u].empty())
highest += val[v];
else if(pq[u].top() > pq[v].top())
highest += val[v] + abs(pq[v].top() - pq[u].top());
else
highest += val[v] + (i) * abs(pq[v].top() - pq[u].top());
if(pq[v].size() > pq[u].size()) swap(pq[v],pq[u]);
while(!pq[v].empty()){
pq[u].push(pq[v].top());
pq[v].pop();
}
}
long long flatr;
///flatening out the line
for(long long i = 0;i < adj[u].size();i++){
flatr = pq[u].top();
pq[u].pop();
highest -= (adj[u].size() - i-1) * abs(flatr - pq[u].top());
}
val[u] = highest;
long long flatl = pq[u].top();
pq[u].pop();
pq[u].push(pw[u] + flatl);
pq[u].push(pw[u] + flatr);
}
int main()
{
//freopen("i.txt","r",stdin);
ios_base::sync_with_stdio(false);
cin.tie(0);
int n, m;
cin >> n >> m;
n += m;
for(int i = 1;i < n;i++){
long long a, b;
cin >> a >> b;
adj[a-1].push_back(ii(i,b));
pw[i] = b;
}
pw[0] = 0;
fill(val,val+n,0);
dfs(0);
cout << val[0];
return 0;
}
Compilation message
fireworks.cpp: In function 'void dfs(int)':
fireworks.cpp:16:27: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
for(long long i = 0;i < adj[u].size();i++){
~~^~~~~~~~~~~~~~~
fireworks.cpp:18:19: warning: unused variable 'w' [-Wunused-variable]
long long w = adj[u][i].second;
^
fireworks.cpp:34:27: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
for(long long i = 0;i < adj[u].size();i++){
~~^~~~~~~~~~~~~~~
fireworks.cpp:43:22: warning: 'flatr' may be used uninitialized in this function [-Wmaybe-uninitialized]
pq[u].push(pw[u] + flatr);
~~~~~~^~~~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
17 ms |
16896 KB |
Output is correct |
2 |
Correct |
17 ms |
16768 KB |
Output is correct |
3 |
Correct |
17 ms |
16868 KB |
Output is correct |
4 |
Correct |
18 ms |
16768 KB |
Output is correct |
5 |
Correct |
21 ms |
16768 KB |
Output is correct |
6 |
Correct |
20 ms |
16768 KB |
Output is correct |
7 |
Correct |
23 ms |
16760 KB |
Output is correct |
8 |
Correct |
22 ms |
16768 KB |
Output is correct |
9 |
Correct |
20 ms |
16892 KB |
Output is correct |
10 |
Correct |
17 ms |
16768 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
20 ms |
17016 KB |
Output is correct |
2 |
Correct |
20 ms |
16768 KB |
Output is correct |
3 |
Correct |
20 ms |
16760 KB |
Output is correct |
4 |
Correct |
18 ms |
16768 KB |
Output is correct |
5 |
Correct |
19 ms |
16768 KB |
Output is correct |
6 |
Correct |
21 ms |
16812 KB |
Output is correct |
7 |
Correct |
20 ms |
16768 KB |
Output is correct |
8 |
Correct |
15 ms |
16768 KB |
Output is correct |
9 |
Correct |
19 ms |
16768 KB |
Output is correct |
10 |
Correct |
20 ms |
16768 KB |
Output is correct |
11 |
Correct |
19 ms |
16896 KB |
Output is correct |
12 |
Correct |
19 ms |
16768 KB |
Output is correct |
13 |
Correct |
20 ms |
16860 KB |
Output is correct |
14 |
Correct |
19 ms |
16768 KB |
Output is correct |
15 |
Correct |
19 ms |
16768 KB |
Output is correct |
16 |
Correct |
21 ms |
16888 KB |
Output is correct |
17 |
Correct |
19 ms |
16896 KB |
Output is correct |
18 |
Correct |
21 ms |
16768 KB |
Output is correct |
19 |
Correct |
22 ms |
16896 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
17 ms |
16896 KB |
Output is correct |
2 |
Correct |
17 ms |
16768 KB |
Output is correct |
3 |
Correct |
17 ms |
16868 KB |
Output is correct |
4 |
Correct |
18 ms |
16768 KB |
Output is correct |
5 |
Correct |
21 ms |
16768 KB |
Output is correct |
6 |
Correct |
20 ms |
16768 KB |
Output is correct |
7 |
Correct |
23 ms |
16760 KB |
Output is correct |
8 |
Correct |
22 ms |
16768 KB |
Output is correct |
9 |
Correct |
20 ms |
16892 KB |
Output is correct |
10 |
Correct |
17 ms |
16768 KB |
Output is correct |
11 |
Correct |
20 ms |
17016 KB |
Output is correct |
12 |
Correct |
20 ms |
16768 KB |
Output is correct |
13 |
Correct |
20 ms |
16760 KB |
Output is correct |
14 |
Correct |
18 ms |
16768 KB |
Output is correct |
15 |
Correct |
19 ms |
16768 KB |
Output is correct |
16 |
Correct |
21 ms |
16812 KB |
Output is correct |
17 |
Correct |
20 ms |
16768 KB |
Output is correct |
18 |
Correct |
15 ms |
16768 KB |
Output is correct |
19 |
Correct |
19 ms |
16768 KB |
Output is correct |
20 |
Correct |
20 ms |
16768 KB |
Output is correct |
21 |
Correct |
19 ms |
16896 KB |
Output is correct |
22 |
Correct |
19 ms |
16768 KB |
Output is correct |
23 |
Correct |
20 ms |
16860 KB |
Output is correct |
24 |
Correct |
19 ms |
16768 KB |
Output is correct |
25 |
Correct |
19 ms |
16768 KB |
Output is correct |
26 |
Correct |
21 ms |
16888 KB |
Output is correct |
27 |
Correct |
19 ms |
16896 KB |
Output is correct |
28 |
Correct |
21 ms |
16768 KB |
Output is correct |
29 |
Correct |
22 ms |
16896 KB |
Output is correct |
30 |
Correct |
21 ms |
17024 KB |
Output is correct |
31 |
Correct |
15 ms |
16768 KB |
Output is correct |
32 |
Correct |
21 ms |
16944 KB |
Output is correct |
33 |
Correct |
20 ms |
17024 KB |
Output is correct |
34 |
Correct |
23 ms |
17024 KB |
Output is correct |
35 |
Correct |
25 ms |
17024 KB |
Output is correct |
36 |
Correct |
25 ms |
17144 KB |
Output is correct |
37 |
Correct |
22 ms |
17144 KB |
Output is correct |
38 |
Correct |
26 ms |
17196 KB |
Output is correct |
39 |
Correct |
24 ms |
17280 KB |
Output is correct |
40 |
Correct |
22 ms |
17664 KB |
Output is correct |
41 |
Correct |
24 ms |
17784 KB |
Output is correct |
42 |
Correct |
20 ms |
17152 KB |
Output is correct |
43 |
Correct |
23 ms |
17408 KB |
Output is correct |
44 |
Correct |
26 ms |
17408 KB |
Output is correct |
45 |
Correct |
21 ms |
17408 KB |
Output is correct |
46 |
Correct |
20 ms |
17340 KB |
Output is correct |
47 |
Correct |
26 ms |
17408 KB |
Output is correct |
48 |
Correct |
25 ms |
17280 KB |
Output is correct |
49 |
Correct |
21 ms |
17272 KB |
Output is correct |
50 |
Correct |
22 ms |
17152 KB |
Output is correct |
51 |
Correct |
21 ms |
17152 KB |
Output is correct |
52 |
Correct |
23 ms |
17152 KB |
Output is correct |
53 |
Correct |
19 ms |
17280 KB |
Output is correct |
54 |
Correct |
25 ms |
17280 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
17 ms |
16896 KB |
Output is correct |
2 |
Correct |
17 ms |
16768 KB |
Output is correct |
3 |
Correct |
17 ms |
16868 KB |
Output is correct |
4 |
Correct |
18 ms |
16768 KB |
Output is correct |
5 |
Correct |
21 ms |
16768 KB |
Output is correct |
6 |
Correct |
20 ms |
16768 KB |
Output is correct |
7 |
Correct |
23 ms |
16760 KB |
Output is correct |
8 |
Correct |
22 ms |
16768 KB |
Output is correct |
9 |
Correct |
20 ms |
16892 KB |
Output is correct |
10 |
Correct |
17 ms |
16768 KB |
Output is correct |
11 |
Correct |
20 ms |
17016 KB |
Output is correct |
12 |
Correct |
20 ms |
16768 KB |
Output is correct |
13 |
Correct |
20 ms |
16760 KB |
Output is correct |
14 |
Correct |
18 ms |
16768 KB |
Output is correct |
15 |
Correct |
19 ms |
16768 KB |
Output is correct |
16 |
Correct |
21 ms |
16812 KB |
Output is correct |
17 |
Correct |
20 ms |
16768 KB |
Output is correct |
18 |
Correct |
15 ms |
16768 KB |
Output is correct |
19 |
Correct |
19 ms |
16768 KB |
Output is correct |
20 |
Correct |
20 ms |
16768 KB |
Output is correct |
21 |
Correct |
19 ms |
16896 KB |
Output is correct |
22 |
Correct |
19 ms |
16768 KB |
Output is correct |
23 |
Correct |
20 ms |
16860 KB |
Output is correct |
24 |
Correct |
19 ms |
16768 KB |
Output is correct |
25 |
Correct |
19 ms |
16768 KB |
Output is correct |
26 |
Correct |
21 ms |
16888 KB |
Output is correct |
27 |
Correct |
19 ms |
16896 KB |
Output is correct |
28 |
Correct |
21 ms |
16768 KB |
Output is correct |
29 |
Correct |
22 ms |
16896 KB |
Output is correct |
30 |
Correct |
21 ms |
17024 KB |
Output is correct |
31 |
Correct |
15 ms |
16768 KB |
Output is correct |
32 |
Correct |
21 ms |
16944 KB |
Output is correct |
33 |
Correct |
20 ms |
17024 KB |
Output is correct |
34 |
Correct |
23 ms |
17024 KB |
Output is correct |
35 |
Correct |
25 ms |
17024 KB |
Output is correct |
36 |
Correct |
25 ms |
17144 KB |
Output is correct |
37 |
Correct |
22 ms |
17144 KB |
Output is correct |
38 |
Correct |
26 ms |
17196 KB |
Output is correct |
39 |
Correct |
24 ms |
17280 KB |
Output is correct |
40 |
Correct |
22 ms |
17664 KB |
Output is correct |
41 |
Correct |
24 ms |
17784 KB |
Output is correct |
42 |
Correct |
20 ms |
17152 KB |
Output is correct |
43 |
Correct |
23 ms |
17408 KB |
Output is correct |
44 |
Correct |
26 ms |
17408 KB |
Output is correct |
45 |
Correct |
21 ms |
17408 KB |
Output is correct |
46 |
Correct |
20 ms |
17340 KB |
Output is correct |
47 |
Correct |
26 ms |
17408 KB |
Output is correct |
48 |
Correct |
25 ms |
17280 KB |
Output is correct |
49 |
Correct |
21 ms |
17272 KB |
Output is correct |
50 |
Correct |
22 ms |
17152 KB |
Output is correct |
51 |
Correct |
21 ms |
17152 KB |
Output is correct |
52 |
Correct |
23 ms |
17152 KB |
Output is correct |
53 |
Correct |
19 ms |
17280 KB |
Output is correct |
54 |
Correct |
25 ms |
17280 KB |
Output is correct |
55 |
Correct |
31 ms |
17912 KB |
Output is correct |
56 |
Correct |
79 ms |
21052 KB |
Output is correct |
57 |
Correct |
106 ms |
23924 KB |
Output is correct |
58 |
Correct |
132 ms |
25964 KB |
Output is correct |
59 |
Correct |
187 ms |
29004 KB |
Output is correct |
60 |
Correct |
231 ms |
31996 KB |
Output is correct |
61 |
Correct |
325 ms |
34508 KB |
Output is correct |
62 |
Correct |
388 ms |
36252 KB |
Output is correct |
63 |
Correct |
409 ms |
39840 KB |
Output is correct |
64 |
Correct |
416 ms |
40812 KB |
Output is correct |
65 |
Correct |
152 ms |
68768 KB |
Output is correct |
66 |
Correct |
155 ms |
68676 KB |
Output is correct |
67 |
Correct |
191 ms |
35916 KB |
Output is correct |
68 |
Correct |
232 ms |
55920 KB |
Output is correct |
69 |
Correct |
362 ms |
53740 KB |
Output is correct |
70 |
Correct |
311 ms |
53740 KB |
Output is correct |
71 |
Correct |
251 ms |
53504 KB |
Output is correct |
72 |
Correct |
308 ms |
53936 KB |
Output is correct |
73 |
Correct |
223 ms |
49248 KB |
Output is correct |
74 |
Correct |
245 ms |
49504 KB |
Output is correct |
75 |
Correct |
209 ms |
47716 KB |
Output is correct |
76 |
Correct |
240 ms |
47716 KB |
Output is correct |
77 |
Correct |
228 ms |
47144 KB |
Output is correct |
78 |
Correct |
238 ms |
45024 KB |
Output is correct |
79 |
Correct |
321 ms |
46948 KB |
Output is correct |