#include <bits/stdc++.h>
using namespace std;
#define int long long
#define pii pair<int,int>
#define OK puts("OK");
#define NO puts("NO");
#define YES puts("YES");
#define fr first
#define sc second
#define ret return
#define scanl(a) scanf("%lld",&a);
#define scanll(a,b) scanf("%lld %lld",&a, &b);
#define scanlll(a,b,c) scanf("%lld %lld %lld",&a,&b,&c);
#define scan1(a) scanf("%lld",&a);
#define scan2(a,b) scanf("%d %lld",&a, &b);
#define scan3(a,b,c) scanf("%d %d %d",&a,&b,&c);
#define all(s) s.begin(),s.end()
#define allr(s) s.rbegin(),s.rend()
#define pb push_back
#define sz(v) (int)v.size()
#define umax(a,b) a = max(a,b);
#define umin(a,b) a = min(a,b);
#define endi puts("");
#define eps 1e-12
const int N = 1e5+12,INF=1e18+7;
vector <int> der[N],v;
vector <pii> g[N];
void dfs_build(int x,int sum){
if (g[x].empty()){
der[x].pb(sum);
v.pb(sum);
ret ;
}
for (pii to:g[x]){
dfs_build(to.fr,sum+to.sc);
der[x].insert(der[x].end(),all(der[to.fr]));
}
sort(all(der[x]));
}
int get_ans(int x,int sum,int ch){
int r = der[x][der[x].size()/2],ans = 0;
int l = der[x][(der[x].size()-1)/2];
if (l+ch <= sum && sum <= r+ch){}
else if (r+ch < sum){
int z = sum-r-ch;
ans += z;
ch += z;
}
else if (l+ch > sum){
int z = l+ch-sum;
ans += z;
ch -= z;
}
for (pii to:g[x]){
ans += get_ans(to.fr,sum,ch);
}
ret ans;
}
main(){
int n,m,i,j,sum=0,ans=INF;
cin>>n>>m;
for (i=2;i<=m+n;++i){
int x,y;
cin>>x>>y;
g[x].pb({i,y});
}
dfs_build(1,0);
sort(all(v));
for (int x:v){
ans = min(ans,get_ans(1,x,0));
}
cout <<ans;
}
/*
8 7
1 5
2 3
2 8
3 2
3 3
4 6
4 5
5 6
5 3
6 8
7 5
7 1
8 3
8 6
*/
Compilation message
fireworks.cpp:67:1: warning: ISO C++ forbids declaration of 'main' with no type [-Wreturn-type]
67 | main(){
| ^~~~
fireworks.cpp: In function 'int main()':
fireworks.cpp:68:15: warning: unused variable 'j' [-Wunused-variable]
68 | int n,m,i,j,sum=0,ans=INF;
| ^
fireworks.cpp:68:17: warning: unused variable 'sum' [-Wunused-variable]
68 | int n,m,i,j,sum=0,ans=INF;
| ^~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
5 ms |
4940 KB |
Output is correct |
2 |
Correct |
4 ms |
4940 KB |
Output is correct |
3 |
Correct |
5 ms |
4940 KB |
Output is correct |
4 |
Correct |
5 ms |
4940 KB |
Output is correct |
5 |
Correct |
5 ms |
4940 KB |
Output is correct |
6 |
Correct |
4 ms |
4940 KB |
Output is correct |
7 |
Correct |
4 ms |
4940 KB |
Output is correct |
8 |
Correct |
4 ms |
4940 KB |
Output is correct |
9 |
Correct |
4 ms |
4980 KB |
Output is correct |
10 |
Correct |
4 ms |
4940 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
4 ms |
4868 KB |
Output is correct |
2 |
Incorrect |
4 ms |
4940 KB |
Output isn't correct |
3 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
5 ms |
4940 KB |
Output is correct |
2 |
Correct |
4 ms |
4940 KB |
Output is correct |
3 |
Correct |
5 ms |
4940 KB |
Output is correct |
4 |
Correct |
5 ms |
4940 KB |
Output is correct |
5 |
Correct |
5 ms |
4940 KB |
Output is correct |
6 |
Correct |
4 ms |
4940 KB |
Output is correct |
7 |
Correct |
4 ms |
4940 KB |
Output is correct |
8 |
Correct |
4 ms |
4940 KB |
Output is correct |
9 |
Correct |
4 ms |
4980 KB |
Output is correct |
10 |
Correct |
4 ms |
4940 KB |
Output is correct |
11 |
Correct |
4 ms |
4868 KB |
Output is correct |
12 |
Incorrect |
4 ms |
4940 KB |
Output isn't correct |
13 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
5 ms |
4940 KB |
Output is correct |
2 |
Correct |
4 ms |
4940 KB |
Output is correct |
3 |
Correct |
5 ms |
4940 KB |
Output is correct |
4 |
Correct |
5 ms |
4940 KB |
Output is correct |
5 |
Correct |
5 ms |
4940 KB |
Output is correct |
6 |
Correct |
4 ms |
4940 KB |
Output is correct |
7 |
Correct |
4 ms |
4940 KB |
Output is correct |
8 |
Correct |
4 ms |
4940 KB |
Output is correct |
9 |
Correct |
4 ms |
4980 KB |
Output is correct |
10 |
Correct |
4 ms |
4940 KB |
Output is correct |
11 |
Correct |
4 ms |
4868 KB |
Output is correct |
12 |
Incorrect |
4 ms |
4940 KB |
Output isn't correct |
13 |
Halted |
0 ms |
0 KB |
- |