//雪花飄飄北風嘯嘯
//天地一片蒼茫
#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
#include <ext/rope>
using namespace std;
using namespace __gnu_pbds;
using namespace __gnu_cxx;
#define ll long long
#define ii pair<ll,ll>
#define iii pair<ii,ll>
#define fi first
#define se second
#define endl '\n'
#define debug(x) cout << #x << " is " << x << endl;
#define rep(x,start,end) for(auto x=(start)-((start)>(end));x!=(end)-((start)>(end));((start)<(end)?x++:x--))
#define all(x) (x).begin(),(x).end()
#define sz(x) (int)(x).size()
ll MAX(ll a){return a;}
ll MIN(ll a){return a;}
template<typename... Args>
ll MAX(ll a,Args... args){return max(a,MAX(args...));}
template<typename... Args>
ll MIN(ll a,Args... args){return min(a,MIN(args...));}
#define indexed_set tree<ll,null_type,less<ll>,rb_tree_tag,tree_order_statistics_node_update>
mt19937 rng(chrono::system_clock::now().time_since_epoch().count());
int n,m;
vector<ii> al[300005];
struct dat{
ll l,r,h;
dat(){
l=r=h=0;
}
};
dat dfs(int i){
dat res=dat();
if (n<i) return res;
vector<ll> pos;
vector<dat> dats;
for (auto &it:al[i]){
auto temp=dfs(it.fi);
temp.l+=it.se,temp.r+=it.se;
pos.push_back(temp.l);
pos.push_back(temp.r);
dats.push_back(temp);
}
sort(all(pos));
int idx=sz(pos)/2;
res.l=pos[idx-1],res.r=pos[idx];
for (auto &it:dats){
if (res.l<it.l) res.h+=it.l-res.l;
else if (it.r<res.l) res.h+=res.l-it.r;
res.h+=it.h;
}
//cout<<i<<" "<<res.l<<" "<<res.r<<" "<<res.h<<endl;
return res;
}
int main(){
ios::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
cin>>n>>m;
int a,b;
rep(x,2,n+m+1){
cin>>a>>b;
al[a].push_back(ii(x,b));
}
cout<<dfs(1).h<<endl;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
4 ms |
7424 KB |
Output is correct |
2 |
Correct |
5 ms |
7424 KB |
Output is correct |
3 |
Correct |
5 ms |
7424 KB |
Output is correct |
4 |
Correct |
5 ms |
7424 KB |
Output is correct |
5 |
Correct |
4 ms |
7424 KB |
Output is correct |
6 |
Correct |
4 ms |
7424 KB |
Output is correct |
7 |
Correct |
5 ms |
7424 KB |
Output is correct |
8 |
Correct |
5 ms |
7424 KB |
Output is correct |
9 |
Correct |
5 ms |
7424 KB |
Output is correct |
10 |
Correct |
5 ms |
7424 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
5 ms |
7424 KB |
Output is correct |
2 |
Correct |
5 ms |
7424 KB |
Output is correct |
3 |
Incorrect |
5 ms |
7424 KB |
Output isn't correct |
4 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
4 ms |
7424 KB |
Output is correct |
2 |
Correct |
5 ms |
7424 KB |
Output is correct |
3 |
Correct |
5 ms |
7424 KB |
Output is correct |
4 |
Correct |
5 ms |
7424 KB |
Output is correct |
5 |
Correct |
4 ms |
7424 KB |
Output is correct |
6 |
Correct |
4 ms |
7424 KB |
Output is correct |
7 |
Correct |
5 ms |
7424 KB |
Output is correct |
8 |
Correct |
5 ms |
7424 KB |
Output is correct |
9 |
Correct |
5 ms |
7424 KB |
Output is correct |
10 |
Correct |
5 ms |
7424 KB |
Output is correct |
11 |
Correct |
5 ms |
7424 KB |
Output is correct |
12 |
Correct |
5 ms |
7424 KB |
Output is correct |
13 |
Incorrect |
5 ms |
7424 KB |
Output isn't correct |
14 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
4 ms |
7424 KB |
Output is correct |
2 |
Correct |
5 ms |
7424 KB |
Output is correct |
3 |
Correct |
5 ms |
7424 KB |
Output is correct |
4 |
Correct |
5 ms |
7424 KB |
Output is correct |
5 |
Correct |
4 ms |
7424 KB |
Output is correct |
6 |
Correct |
4 ms |
7424 KB |
Output is correct |
7 |
Correct |
5 ms |
7424 KB |
Output is correct |
8 |
Correct |
5 ms |
7424 KB |
Output is correct |
9 |
Correct |
5 ms |
7424 KB |
Output is correct |
10 |
Correct |
5 ms |
7424 KB |
Output is correct |
11 |
Correct |
5 ms |
7424 KB |
Output is correct |
12 |
Correct |
5 ms |
7424 KB |
Output is correct |
13 |
Incorrect |
5 ms |
7424 KB |
Output isn't correct |
14 |
Halted |
0 ms |
0 KB |
- |