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<iostream>
#include<algorithm>
#include<vector>
using namespace std;
typedef long long ll;
#define chmin(a,b) a=min(a,b)
#define chmax(a,b) a=max(a,b)
#define rep(i,n) for(int i=0;i<n;i++)
#define mod 1000000007
#define mad(a,b) a=(a+b)%mod
#define N 310
ll n,m;
vector<pair<ll,ll> > g[N];
ll dp[N][N];
void dfs(ll x){
if(x>n){
rep(i,N)dp[x][i]=1e9;
dp[x][0]=0;
return;
}
rep(i,N)dp[x][i]=0;
for(auto e:g[x]){
dfs(e.first);
rep(i,N){
ll mi=1e9;
for(int j=0;j<=i;j++){
chmin(mi,dp[e.first][j]+abs((i-j)-e.second));
}
dp[x][i]+=mi;
}
}
}
int main(){
cin>>n>>m;
for(int i=2;i<=n+m;i++){
ll p,c; cin>>p>>c;
g[p].push_back(make_pair(i,c));
}
dfs(1);
ll ans=1e17;
rep(i,N){
chmin(ans,dp[1][i]);
}
cout<<ans<<endl;
}
# | 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... |