#include <bits/stdc++.h>
using namespace std;
#define ll long long
int p[300005];
int b[300005];
ll a[300005];
priority_queue<pair<long long, int> > pq[300005];
vector<int> ch[300005];
void dfs(int v){
int big = 0;
for(int& x : ch[v]){
dfs(x);
if(pq[big].size() < pq[x].size()) big = x;
}
if(big == 0){
pq[v].push({0, -1});
pq[v].push({a[v], 1});
pq[v].push({a[v], 1});
return;
}
swap(pq[big], pq[v]);
int g = ch[v].size();
for(auto& x : ch[v]){
if(x == big) continue;
while(!pq[x].empty()){
pq[v].push(pq[x].top());
pq[x].pop();
}
}
ll t;
while(g > -1){
t = pq[v].top().first;
g -= pq[v].top().second;
pq[v].pop();
}
pq[v].push({t + a[v], 1});
pq[v].push({t + a[v], 1});
}
int main(){
ios_base::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
int n, m;
cin>>n>>m;
ll y = 0;
for(int i = 2; i <= n + m; i++){
cin>>p[i]>>a[i];
y += a[i];
ch[p[i]].push_back(i);
}
dfs(1);
ll t = pq[1].top().first;
while(!pq[1].empty()){
y += (t - pq[1].top().first) * pq[1].top().second;
pq[1].pop();
}
cout<<y;
}
Compilation message
fireworks.cpp: In function 'void dfs(int)':
fireworks.cpp:39:18: warning: 't' may be used uninitialized in this function [-Wmaybe-uninitialized]
pq[v].push({t + a[v], 1});
~~^~~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
17 ms |
17016 KB |
Output is correct |
2 |
Correct |
17 ms |
16760 KB |
Output is correct |
3 |
Correct |
17 ms |
16896 KB |
Output is correct |
4 |
Correct |
17 ms |
16888 KB |
Output is correct |
5 |
Correct |
22 ms |
16888 KB |
Output is correct |
6 |
Correct |
18 ms |
16888 KB |
Output is correct |
7 |
Correct |
22 ms |
16860 KB |
Output is correct |
8 |
Correct |
21 ms |
16760 KB |
Output is correct |
9 |
Correct |
23 ms |
16888 KB |
Output is correct |
10 |
Correct |
17 ms |
16760 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
17 ms |
16760 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
17 ms |
17016 KB |
Output is correct |
2 |
Correct |
17 ms |
16760 KB |
Output is correct |
3 |
Correct |
17 ms |
16896 KB |
Output is correct |
4 |
Correct |
17 ms |
16888 KB |
Output is correct |
5 |
Correct |
22 ms |
16888 KB |
Output is correct |
6 |
Correct |
18 ms |
16888 KB |
Output is correct |
7 |
Correct |
22 ms |
16860 KB |
Output is correct |
8 |
Correct |
21 ms |
16760 KB |
Output is correct |
9 |
Correct |
23 ms |
16888 KB |
Output is correct |
10 |
Correct |
17 ms |
16760 KB |
Output is correct |
11 |
Incorrect |
17 ms |
16760 KB |
Output isn't correct |
12 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
17 ms |
17016 KB |
Output is correct |
2 |
Correct |
17 ms |
16760 KB |
Output is correct |
3 |
Correct |
17 ms |
16896 KB |
Output is correct |
4 |
Correct |
17 ms |
16888 KB |
Output is correct |
5 |
Correct |
22 ms |
16888 KB |
Output is correct |
6 |
Correct |
18 ms |
16888 KB |
Output is correct |
7 |
Correct |
22 ms |
16860 KB |
Output is correct |
8 |
Correct |
21 ms |
16760 KB |
Output is correct |
9 |
Correct |
23 ms |
16888 KB |
Output is correct |
10 |
Correct |
17 ms |
16760 KB |
Output is correct |
11 |
Incorrect |
17 ms |
16760 KB |
Output isn't correct |
12 |
Halted |
0 ms |
0 KB |
- |