#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 |
1 |
Correct |
1 ms |
332 KB |
Output is correct |
2 |
Correct |
1 ms |
332 KB |
Output is correct |
3 |
Correct |
1 ms |
336 KB |
Output is correct |
4 |
Correct |
1 ms |
332 KB |
Output is correct |
5 |
Correct |
1 ms |
332 KB |
Output is correct |
6 |
Correct |
1 ms |
332 KB |
Output is correct |
7 |
Correct |
1 ms |
332 KB |
Output is correct |
8 |
Correct |
1 ms |
332 KB |
Output is correct |
9 |
Correct |
1 ms |
332 KB |
Output is correct |
10 |
Correct |
1 ms |
332 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
460 KB |
Output is correct |
2 |
Correct |
4 ms |
544 KB |
Output is correct |
3 |
Correct |
4 ms |
588 KB |
Output is correct |
4 |
Correct |
6 ms |
652 KB |
Output is correct |
5 |
Correct |
7 ms |
716 KB |
Output is correct |
6 |
Correct |
8 ms |
720 KB |
Output is correct |
7 |
Correct |
9 ms |
716 KB |
Output is correct |
8 |
Correct |
10 ms |
844 KB |
Output is correct |
9 |
Correct |
11 ms |
812 KB |
Output is correct |
10 |
Correct |
12 ms |
972 KB |
Output is correct |
11 |
Correct |
25 ms |
1008 KB |
Output is correct |
12 |
Correct |
15 ms |
972 KB |
Output is correct |
13 |
Correct |
16 ms |
1100 KB |
Output is correct |
14 |
Correct |
17 ms |
1080 KB |
Output is correct |
15 |
Correct |
32 ms |
1116 KB |
Output is correct |
16 |
Correct |
18 ms |
1100 KB |
Output is correct |
17 |
Correct |
17 ms |
1100 KB |
Output is correct |
18 |
Correct |
27 ms |
1180 KB |
Output is correct |
19 |
Correct |
17 ms |
1100 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
332 KB |
Output is correct |
2 |
Correct |
1 ms |
332 KB |
Output is correct |
3 |
Correct |
1 ms |
336 KB |
Output is correct |
4 |
Correct |
1 ms |
332 KB |
Output is correct |
5 |
Correct |
1 ms |
332 KB |
Output is correct |
6 |
Correct |
1 ms |
332 KB |
Output is correct |
7 |
Correct |
1 ms |
332 KB |
Output is correct |
8 |
Correct |
1 ms |
332 KB |
Output is correct |
9 |
Correct |
1 ms |
332 KB |
Output is correct |
10 |
Correct |
1 ms |
332 KB |
Output is correct |
11 |
Correct |
1 ms |
460 KB |
Output is correct |
12 |
Correct |
4 ms |
544 KB |
Output is correct |
13 |
Correct |
4 ms |
588 KB |
Output is correct |
14 |
Correct |
6 ms |
652 KB |
Output is correct |
15 |
Correct |
7 ms |
716 KB |
Output is correct |
16 |
Correct |
8 ms |
720 KB |
Output is correct |
17 |
Correct |
9 ms |
716 KB |
Output is correct |
18 |
Correct |
10 ms |
844 KB |
Output is correct |
19 |
Correct |
11 ms |
812 KB |
Output is correct |
20 |
Correct |
12 ms |
972 KB |
Output is correct |
21 |
Correct |
25 ms |
1008 KB |
Output is correct |
22 |
Correct |
15 ms |
972 KB |
Output is correct |
23 |
Correct |
16 ms |
1100 KB |
Output is correct |
24 |
Correct |
17 ms |
1080 KB |
Output is correct |
25 |
Correct |
32 ms |
1116 KB |
Output is correct |
26 |
Correct |
18 ms |
1100 KB |
Output is correct |
27 |
Correct |
17 ms |
1100 KB |
Output is correct |
28 |
Correct |
27 ms |
1180 KB |
Output is correct |
29 |
Correct |
17 ms |
1100 KB |
Output is correct |
30 |
Runtime error |
1 ms |
716 KB |
Execution killed with signal 11 |
31 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
332 KB |
Output is correct |
2 |
Correct |
1 ms |
332 KB |
Output is correct |
3 |
Correct |
1 ms |
336 KB |
Output is correct |
4 |
Correct |
1 ms |
332 KB |
Output is correct |
5 |
Correct |
1 ms |
332 KB |
Output is correct |
6 |
Correct |
1 ms |
332 KB |
Output is correct |
7 |
Correct |
1 ms |
332 KB |
Output is correct |
8 |
Correct |
1 ms |
332 KB |
Output is correct |
9 |
Correct |
1 ms |
332 KB |
Output is correct |
10 |
Correct |
1 ms |
332 KB |
Output is correct |
11 |
Correct |
1 ms |
460 KB |
Output is correct |
12 |
Correct |
4 ms |
544 KB |
Output is correct |
13 |
Correct |
4 ms |
588 KB |
Output is correct |
14 |
Correct |
6 ms |
652 KB |
Output is correct |
15 |
Correct |
7 ms |
716 KB |
Output is correct |
16 |
Correct |
8 ms |
720 KB |
Output is correct |
17 |
Correct |
9 ms |
716 KB |
Output is correct |
18 |
Correct |
10 ms |
844 KB |
Output is correct |
19 |
Correct |
11 ms |
812 KB |
Output is correct |
20 |
Correct |
12 ms |
972 KB |
Output is correct |
21 |
Correct |
25 ms |
1008 KB |
Output is correct |
22 |
Correct |
15 ms |
972 KB |
Output is correct |
23 |
Correct |
16 ms |
1100 KB |
Output is correct |
24 |
Correct |
17 ms |
1080 KB |
Output is correct |
25 |
Correct |
32 ms |
1116 KB |
Output is correct |
26 |
Correct |
18 ms |
1100 KB |
Output is correct |
27 |
Correct |
17 ms |
1100 KB |
Output is correct |
28 |
Correct |
27 ms |
1180 KB |
Output is correct |
29 |
Correct |
17 ms |
1100 KB |
Output is correct |
30 |
Runtime error |
1 ms |
716 KB |
Execution killed with signal 11 |
31 |
Halted |
0 ms |
0 KB |
- |