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;
#define ll long long
const int N = 305;
const ll inf = 2e18;
vector<array<int, 2>> adj[N];
ll dp[N][N];
int main() {
ios_base::sync_with_stdio(0), cin.tie(0);
int n, m;
cin >> n >> m;
n += m;
for (int i = 2; i <= n; i++) {
int p, c;
cin >> p >> c;
adj[p].push_back({i, c});
}
for (int i = n; i >= 1; i--) {
for (int j = 0; j < N; j++) {
if (adj[i].empty() && j) dp[i][j] = inf;
for (auto [nxt, w] : adj[i]) {
ll mn = 1e18;
for (int k = 0; k <= j; k++) {
mn = min(mn, dp[nxt][k] + abs(j - k - w));
}
dp[i][j] += mn;
dp[i][j] = min(dp[i][j], inf);
}
}
ll mn = *min_element(dp[i], dp[i] + N);
for (int j = N - 1; j >= 0; j--) {
if (dp[i][j] == mn) break;
dp[i][j] = inf;
}
}
cout << *min_element(dp[1], dp[1] + N) << '\n';
return 0;
}
# | 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... |