#include<bits/stdc++.h>
using namespace std;
#define pb push_back
using lli=long long int;
#define deb(x) cout<<#x<<": "<<x<<endl;
#define endl '\n'
const lli INF=1e18;
vector<vector<lli>> dp;
void dfs(lli x, vector<vector<pair<lli,lli>>> &sons){
for(auto y: sons[x]){
dfs(y.first, sons);
}
if(sons[x].size()==0){
dp[x][0]=0;
return;
}
for(lli i=0; i<=300; ++i){
dp[x][i]=0;
for(auto y: sons[x]){
lli aux=INF;
for(lli j=0; j<=i; ++j){
aux=min(aux, abs(y.second-j)+dp[y.first][i-j]);
}
dp[x][i]+=aux;
}
// deb(x);
// deb(i);
}
}
void solve(){
lli n,m;
cin>>n>>m;
dp.clear();
dp.resize(n+m+1,vector<lli> (301,INF));
vector<vector<pair<lli,lli>>> sons (n+m+1);
for(lli i=2; i<=n+m; ++i){
lli p, c;
cin>>p>>c;
sons[p].pb({i, c});
}
dfs(1, sons);
lli ans=INF;
for(lli x: dp[1]){
ans=min(ans, x);
}
cout<<ans<<endl;
}
int main(){
ios_base::sync_with_stdio(0);
cin.tie(0);
lli t=1;
// cin>>t;
while(t--){
solve();
}
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
0 ms |
344 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
348 KB |
Output is correct |
2 |
Correct |
2 ms |
348 KB |
Output is correct |
3 |
Correct |
2 ms |
348 KB |
Output is correct |
4 |
Correct |
4 ms |
604 KB |
Output is correct |
5 |
Correct |
4 ms |
604 KB |
Output is correct |
6 |
Correct |
5 ms |
604 KB |
Output is correct |
7 |
Correct |
6 ms |
604 KB |
Output is correct |
8 |
Correct |
6 ms |
860 KB |
Output is correct |
9 |
Correct |
7 ms |
860 KB |
Output is correct |
10 |
Correct |
7 ms |
972 KB |
Output is correct |
11 |
Correct |
8 ms |
860 KB |
Output is correct |
12 |
Correct |
9 ms |
1084 KB |
Output is correct |
13 |
Correct |
14 ms |
1112 KB |
Output is correct |
14 |
Correct |
10 ms |
1116 KB |
Output is correct |
15 |
Correct |
11 ms |
1116 KB |
Output is correct |
16 |
Correct |
11 ms |
1184 KB |
Output is correct |
17 |
Correct |
10 ms |
1184 KB |
Output is correct |
18 |
Correct |
11 ms |
1116 KB |
Output is correct |
19 |
Correct |
15 ms |
1116 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
0 ms |
344 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
0 ms |
344 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |