#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;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
0 ms |
348 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
348 KB |
Output is correct |
2 |
Correct |
2 ms |
516 KB |
Output is correct |
3 |
Correct |
4 ms |
544 KB |
Output is correct |
4 |
Correct |
5 ms |
348 KB |
Output is correct |
5 |
Correct |
6 ms |
612 KB |
Output is correct |
6 |
Correct |
7 ms |
604 KB |
Output is correct |
7 |
Correct |
9 ms |
600 KB |
Output is correct |
8 |
Correct |
9 ms |
604 KB |
Output is correct |
9 |
Correct |
10 ms |
712 KB |
Output is correct |
10 |
Correct |
11 ms |
604 KB |
Output is correct |
11 |
Correct |
12 ms |
764 KB |
Output is correct |
12 |
Correct |
13 ms |
604 KB |
Output is correct |
13 |
Correct |
14 ms |
860 KB |
Output is correct |
14 |
Correct |
14 ms |
860 KB |
Output is correct |
15 |
Correct |
15 ms |
600 KB |
Output is correct |
16 |
Correct |
15 ms |
600 KB |
Output is correct |
17 |
Correct |
15 ms |
836 KB |
Output is correct |
18 |
Correct |
15 ms |
604 KB |
Output is correct |
19 |
Correct |
15 ms |
600 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
0 ms |
348 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
0 ms |
348 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |