#include <bits/stdc++.h>
using namespace std;
#define int long long
const int MAXN = 5e3 + 25;
vector <pair <int, int>> adj[MAXN];
int dp[MAXN][301], n, m;
void dfs (int pos) {
for (auto j : adj[pos]) {
dfs(j.first);
}
for (int i = 0; i <= 300; i++) {
int sum = 0;
for (auto j : adj[pos]) {
int mn = 1e18;
for(int k=0;k<=i;k++)mn=min(mn,abs(j.second-i+k)+dp[j.first][k]);
sum += mn;
}
dp[pos][i] = sum;
}
}
signed main () {
cin >> n >> m;
n += m;
for (int i = 2; i <= n; i++) {
int x, y; cin >> x >> y;
adj[x].push_back({i, y});
}
dfs(1);
int mn = 1e18;
for (int i = 0; i <= 300; i++) mn = min(mn, dp[1][i]);
cout<<mn;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
0 ms |
348 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
1 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 |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
0 ms |
348 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |