This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#include<bits/stdc++.h>
using namespace std;
long long a,b,c,d,e,i,j,ii,jj,zx,xc,dp[309][309],dep[309],E[309],EI,Z,L[309],l;
const long long N=1000000000000000000LL;
pair <long long, long long> msh[309];
vector <pair <long long, long long> > v[5009];
map <long long, long long> m;
map <long long, long long>::iterator tt;
void dfsst(long long q, long long w){
for(vector <pair <long long, long long> >::iterator it=v[q].begin(); it!=v[q].end(); it++){
if((*it).first==w) continue;
dep[(*it).first]=dep[q]+(*it).second;
msh[(*it).first]=make_pair(q,(*it).second);
dfsst((*it).first,q);
}
}
void dfs(long long q, long long w){
if(q>a){
/*for(j=0; j<=300; j++){
dp[q][j]=abs(j-msh[q].second);
//if(j<=5) cout<<q<<" "<<j<<" "<<dp[q][j]<<endl;
}*/
dp[q][0]=0;
for(j=1; j<=300; j++){
dp[q][j]=N;
}
return;
}
for(vector <pair <long long, long long> >::iterator it=v[q].begin(); it!=v[q].end(); it++){
if((*it).first==w) continue;
dfs((*it).first,q);
l=(*it).second;
ii=(*it).first;
for(j=0; j<=300; j++){
e=N;
for(jj=0; jj<=j; jj++){
e=min(e,dp[ii][jj]+abs(l-j+jj));
}
dp[q][j]=dp[q][j]+e;
}
}
}
int main(){
ios_base::sync_with_stdio(false),cin.tie(0),cout.tie(0);
cin>>a>>b;
for(i=2; i<=a; i++){
cin>>c>>d;
v[c].push_back(make_pair(i,d));
}
for(i=a+1; i<=a+b; i++){
cin>>c>>d;
v[c].push_back(make_pair(i,d));
EI++;
E[EI]=d;
}
if(a==1){
sort(E+1,E+EI+1);
c=E[EI/2+EI%2];
e=0;
for(i=1; i<=EI; i++){
e+=abs(c-E[i]);
}
cout<<e;
return 0;
}
dfsst(1,0);
for(i=a+1; i<=a+b; i++){
m[dep[i]]=1;
}
dfs(1,0);
e=N;
for(j=0; j<=300; j++){
e=min(e,dp[1][j]);
}
cout<<e;
return 0;
}
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |