#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);
~~~~~~^~~~~~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
17 ms |
16768 KB |
Output is correct |
2 |
Correct |
17 ms |
16768 KB |
Output is correct |
3 |
Correct |
18 ms |
16768 KB |
Output is correct |
4 |
Correct |
20 ms |
16768 KB |
Output is correct |
5 |
Correct |
18 ms |
16768 KB |
Output is correct |
6 |
Correct |
17 ms |
16768 KB |
Output is correct |
7 |
Correct |
17 ms |
16768 KB |
Output is correct |
8 |
Correct |
18 ms |
16768 KB |
Output is correct |
9 |
Correct |
15 ms |
16768 KB |
Output is correct |
10 |
Correct |
19 ms |
16840 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
21 ms |
16768 KB |
Output is correct |
2 |
Correct |
16 ms |
16780 KB |
Output is correct |
3 |
Correct |
17 ms |
16740 KB |
Output is correct |
4 |
Correct |
18 ms |
16768 KB |
Output is correct |
5 |
Correct |
17 ms |
16768 KB |
Output is correct |
6 |
Correct |
17 ms |
16768 KB |
Output is correct |
7 |
Correct |
18 ms |
16896 KB |
Output is correct |
8 |
Correct |
18 ms |
16768 KB |
Output is correct |
9 |
Correct |
18 ms |
16768 KB |
Output is correct |
10 |
Correct |
18 ms |
16804 KB |
Output is correct |
11 |
Correct |
16 ms |
16896 KB |
Output is correct |
12 |
Correct |
19 ms |
16896 KB |
Output is correct |
13 |
Correct |
18 ms |
16972 KB |
Output is correct |
14 |
Correct |
17 ms |
16768 KB |
Output is correct |
15 |
Correct |
17 ms |
16896 KB |
Output is correct |
16 |
Correct |
18 ms |
16896 KB |
Output is correct |
17 |
Correct |
17 ms |
16896 KB |
Output is correct |
18 |
Correct |
16 ms |
16896 KB |
Output is correct |
19 |
Correct |
18 ms |
16768 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
17 ms |
16768 KB |
Output is correct |
2 |
Correct |
17 ms |
16768 KB |
Output is correct |
3 |
Correct |
18 ms |
16768 KB |
Output is correct |
4 |
Correct |
20 ms |
16768 KB |
Output is correct |
5 |
Correct |
18 ms |
16768 KB |
Output is correct |
6 |
Correct |
17 ms |
16768 KB |
Output is correct |
7 |
Correct |
17 ms |
16768 KB |
Output is correct |
8 |
Correct |
18 ms |
16768 KB |
Output is correct |
9 |
Correct |
15 ms |
16768 KB |
Output is correct |
10 |
Correct |
19 ms |
16840 KB |
Output is correct |
11 |
Correct |
21 ms |
16768 KB |
Output is correct |
12 |
Correct |
16 ms |
16780 KB |
Output is correct |
13 |
Correct |
17 ms |
16740 KB |
Output is correct |
14 |
Correct |
18 ms |
16768 KB |
Output is correct |
15 |
Correct |
17 ms |
16768 KB |
Output is correct |
16 |
Correct |
17 ms |
16768 KB |
Output is correct |
17 |
Correct |
18 ms |
16896 KB |
Output is correct |
18 |
Correct |
18 ms |
16768 KB |
Output is correct |
19 |
Correct |
18 ms |
16768 KB |
Output is correct |
20 |
Correct |
18 ms |
16804 KB |
Output is correct |
21 |
Correct |
16 ms |
16896 KB |
Output is correct |
22 |
Correct |
19 ms |
16896 KB |
Output is correct |
23 |
Correct |
18 ms |
16972 KB |
Output is correct |
24 |
Correct |
17 ms |
16768 KB |
Output is correct |
25 |
Correct |
17 ms |
16896 KB |
Output is correct |
26 |
Correct |
18 ms |
16896 KB |
Output is correct |
27 |
Correct |
17 ms |
16896 KB |
Output is correct |
28 |
Correct |
16 ms |
16896 KB |
Output is correct |
29 |
Correct |
18 ms |
16768 KB |
Output is correct |
30 |
Correct |
19 ms |
16896 KB |
Output is correct |
31 |
Correct |
21 ms |
17152 KB |
Output is correct |
32 |
Correct |
25 ms |
17152 KB |
Output is correct |
33 |
Correct |
23 ms |
17404 KB |
Output is correct |
34 |
Correct |
28 ms |
17528 KB |
Output is correct |
35 |
Correct |
35 ms |
18296 KB |
Output is correct |
36 |
Correct |
27 ms |
18176 KB |
Output is correct |
37 |
Correct |
27 ms |
18040 KB |
Output is correct |
38 |
Correct |
41 ms |
19704 KB |
Output is correct |
39 |
Correct |
29 ms |
18296 KB |
Output is correct |
40 |
Correct |
21 ms |
17784 KB |
Output is correct |
41 |
Correct |
19 ms |
17664 KB |
Output is correct |
42 |
Correct |
19 ms |
17280 KB |
Output is correct |
43 |
Correct |
255 ms |
48120 KB |
Output is correct |
44 |
Correct |
210 ms |
40180 KB |
Output is correct |
45 |
Correct |
212 ms |
39648 KB |
Output is correct |
46 |
Correct |
21 ms |
17408 KB |
Output is correct |
47 |
Correct |
21 ms |
17408 KB |
Output is correct |
48 |
Correct |
22 ms |
17276 KB |
Output is correct |
49 |
Correct |
21 ms |
17280 KB |
Output is correct |
50 |
Correct |
21 ms |
17152 KB |
Output is correct |
51 |
Correct |
21 ms |
17152 KB |
Output is correct |
52 |
Correct |
30 ms |
17784 KB |
Output is correct |
53 |
Correct |
28 ms |
17424 KB |
Output is correct |
54 |
Correct |
20 ms |
17280 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
17 ms |
16768 KB |
Output is correct |
2 |
Correct |
17 ms |
16768 KB |
Output is correct |
3 |
Correct |
18 ms |
16768 KB |
Output is correct |
4 |
Correct |
20 ms |
16768 KB |
Output is correct |
5 |
Correct |
18 ms |
16768 KB |
Output is correct |
6 |
Correct |
17 ms |
16768 KB |
Output is correct |
7 |
Correct |
17 ms |
16768 KB |
Output is correct |
8 |
Correct |
18 ms |
16768 KB |
Output is correct |
9 |
Correct |
15 ms |
16768 KB |
Output is correct |
10 |
Correct |
19 ms |
16840 KB |
Output is correct |
11 |
Correct |
21 ms |
16768 KB |
Output is correct |
12 |
Correct |
16 ms |
16780 KB |
Output is correct |
13 |
Correct |
17 ms |
16740 KB |
Output is correct |
14 |
Correct |
18 ms |
16768 KB |
Output is correct |
15 |
Correct |
17 ms |
16768 KB |
Output is correct |
16 |
Correct |
17 ms |
16768 KB |
Output is correct |
17 |
Correct |
18 ms |
16896 KB |
Output is correct |
18 |
Correct |
18 ms |
16768 KB |
Output is correct |
19 |
Correct |
18 ms |
16768 KB |
Output is correct |
20 |
Correct |
18 ms |
16804 KB |
Output is correct |
21 |
Correct |
16 ms |
16896 KB |
Output is correct |
22 |
Correct |
19 ms |
16896 KB |
Output is correct |
23 |
Correct |
18 ms |
16972 KB |
Output is correct |
24 |
Correct |
17 ms |
16768 KB |
Output is correct |
25 |
Correct |
17 ms |
16896 KB |
Output is correct |
26 |
Correct |
18 ms |
16896 KB |
Output is correct |
27 |
Correct |
17 ms |
16896 KB |
Output is correct |
28 |
Correct |
16 ms |
16896 KB |
Output is correct |
29 |
Correct |
18 ms |
16768 KB |
Output is correct |
30 |
Correct |
19 ms |
16896 KB |
Output is correct |
31 |
Correct |
21 ms |
17152 KB |
Output is correct |
32 |
Correct |
25 ms |
17152 KB |
Output is correct |
33 |
Correct |
23 ms |
17404 KB |
Output is correct |
34 |
Correct |
28 ms |
17528 KB |
Output is correct |
35 |
Correct |
35 ms |
18296 KB |
Output is correct |
36 |
Correct |
27 ms |
18176 KB |
Output is correct |
37 |
Correct |
27 ms |
18040 KB |
Output is correct |
38 |
Correct |
41 ms |
19704 KB |
Output is correct |
39 |
Correct |
29 ms |
18296 KB |
Output is correct |
40 |
Correct |
21 ms |
17784 KB |
Output is correct |
41 |
Correct |
19 ms |
17664 KB |
Output is correct |
42 |
Correct |
19 ms |
17280 KB |
Output is correct |
43 |
Correct |
255 ms |
48120 KB |
Output is correct |
44 |
Correct |
210 ms |
40180 KB |
Output is correct |
45 |
Correct |
212 ms |
39648 KB |
Output is correct |
46 |
Correct |
21 ms |
17408 KB |
Output is correct |
47 |
Correct |
21 ms |
17408 KB |
Output is correct |
48 |
Correct |
22 ms |
17276 KB |
Output is correct |
49 |
Correct |
21 ms |
17280 KB |
Output is correct |
50 |
Correct |
21 ms |
17152 KB |
Output is correct |
51 |
Correct |
21 ms |
17152 KB |
Output is correct |
52 |
Correct |
30 ms |
17784 KB |
Output is correct |
53 |
Correct |
28 ms |
17424 KB |
Output is correct |
54 |
Correct |
20 ms |
17280 KB |
Output is correct |
55 |
Correct |
78 ms |
23900 KB |
Output is correct |
56 |
Correct |
534 ms |
70564 KB |
Output is correct |
57 |
Correct |
1174 ms |
122304 KB |
Output is correct |
58 |
Correct |
1346 ms |
141260 KB |
Output is correct |
59 |
Execution timed out |
2068 ms |
213012 KB |
Time limit exceeded |
60 |
Halted |
0 ms |
0 KB |
- |