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;
const int nx=305;
int n, m, p, c, dp[nx][nx], ans=INT_MAX;
vector<pair<int, int>> d[nx];
void dfs(int u)
{
if (u>n) return dp[u][0]=0, void();
for (auto [v, w]:d[u]) dfs(v);
for (int i=0; i<nx; i++)
{
int sm=0;
for (auto [v, w]:d[u])
{
int vl=1e9;
for (int x=0; x<=i; x++) vl=min(vl, dp[v][i-x]+abs(w-x));
sm=min(sm+vl, (int)1e9);
}
dp[u][i]=sm;
}
//for (int i=0; i<nx; i++) cout<<u<<' '<<i<<' '<<dp[u][i]<<'\n';
}
int main()
{
cin.tie(NULL)->sync_with_stdio(false);
cin>>n>>m;
for (int i=1; i<=n+m; i++) for (int j=0; j<nx; j++) dp[i][j]=1e9;
for (int i=2; i<=n+m; i++) cin>>p>>c, d[p].push_back({i, c});
dfs(1);
for (int i=0; i<nx; i++) ans=min(ans, dp[1][i]);
cout<<ans;
}
# | 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... |