#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const int N = 4e5 + 30, MOD = 1e9 + 7;
int n,m;
vector<pair<int,int>> g[N];
multiset<ll> st[N];
ll h[N],l[N],r[N];
int mx[N];
void merge(int v,int to){
mx[v]++;
for(int j:st[to]){
st[v].insert(j);
}
}
ll dist(int to,ll x){
if(x >= r[to]){
return h[to] + x - r[to];
}
return h[to] + l[to] - x;
}
void dfs(int v,int W = 0){
if(g[v].empty()){
st[v].insert(W);
st[v].insert(W);
mx[v] = 1;
h[v] = 0;
l[v] = r[v] = W;
return;
}
for(auto [to,w]:g[v]){
dfs(to,w);
merge(v,to);
}
while(mx[v] > 1){
mx[v]--;
st[v].erase(st[v].find(*st[v].rbegin()));
}
ll x = (*st[v].rbegin());
st[v].erase(st[v].find(*st[v].rbegin()));
ll y = (*st[v].rbegin());
st[v].erase(st[v].find(*st[v].rbegin()));
st[v].insert(x + W);
st[v].insert(y + W);
r[v] = (*st[v].rbegin());
l[v] = (*(++(st[v].rbegin())));
for(auto [to,w]:g[v]){
h[v] += dist(to,r[v] - W);
}
// cout << v << ' ' << dist(9,3) << ' ' << h[v] << ' ' << l[v] << ' ' << r[v] << ' ' << "| ";
// for(int i:st[v]){
// cout << i << ' ';
// }
// cout << '\n';
}
void test(){
cin >> n >> m;
int P = -1;
for(int i = 2;i <= n + m;i++){
int a,b;
cin >> a >> b;
g[a].push_back({i,b});
if(a == 1){
P = i;
}
}
dfs(1);
cout << h[1] << '\n';
}
int main() {
ios_base::sync_with_stdio(false);cin.tie(0);
int t = 1;
// cin >> t;
while(t--){
test();
}
}
Compilation message
fireworks.cpp: In function 'void test()':
fireworks.cpp:62:9: warning: variable 'P' set but not used [-Wunused-but-set-variable]
62 | int P = -1;
| ^
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
5 ms |
33116 KB |
Output is correct |
2 |
Correct |
5 ms |
33116 KB |
Output is correct |
3 |
Correct |
5 ms |
35164 KB |
Output is correct |
4 |
Correct |
5 ms |
35240 KB |
Output is correct |
5 |
Correct |
6 ms |
33116 KB |
Output is correct |
6 |
Correct |
5 ms |
35164 KB |
Output is correct |
7 |
Correct |
5 ms |
35160 KB |
Output is correct |
8 |
Correct |
5 ms |
33224 KB |
Output is correct |
9 |
Correct |
5 ms |
33248 KB |
Output is correct |
10 |
Correct |
5 ms |
33116 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
5 ms |
33116 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
5 ms |
33116 KB |
Output is correct |
2 |
Correct |
5 ms |
33116 KB |
Output is correct |
3 |
Correct |
5 ms |
35164 KB |
Output is correct |
4 |
Correct |
5 ms |
35240 KB |
Output is correct |
5 |
Correct |
6 ms |
33116 KB |
Output is correct |
6 |
Correct |
5 ms |
35164 KB |
Output is correct |
7 |
Correct |
5 ms |
35160 KB |
Output is correct |
8 |
Correct |
5 ms |
33224 KB |
Output is correct |
9 |
Correct |
5 ms |
33248 KB |
Output is correct |
10 |
Correct |
5 ms |
33116 KB |
Output is correct |
11 |
Incorrect |
5 ms |
33116 KB |
Output isn't correct |
12 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
5 ms |
33116 KB |
Output is correct |
2 |
Correct |
5 ms |
33116 KB |
Output is correct |
3 |
Correct |
5 ms |
35164 KB |
Output is correct |
4 |
Correct |
5 ms |
35240 KB |
Output is correct |
5 |
Correct |
6 ms |
33116 KB |
Output is correct |
6 |
Correct |
5 ms |
35164 KB |
Output is correct |
7 |
Correct |
5 ms |
35160 KB |
Output is correct |
8 |
Correct |
5 ms |
33224 KB |
Output is correct |
9 |
Correct |
5 ms |
33248 KB |
Output is correct |
10 |
Correct |
5 ms |
33116 KB |
Output is correct |
11 |
Incorrect |
5 ms |
33116 KB |
Output isn't correct |
12 |
Halted |
0 ms |
0 KB |
- |