#include <bits/stdc++.h>
using i64 = long long;
constexpr int N = 305;
int n, m;
std::vector<std::pair<int, int>> e[N];
int dp[N][N];
void dfs(int u){
if (e[u].empty()) {
dp[u][0] = 0;
return;
}
for (auto [v, w] : e[u]) {
dfs(v);
}
for (int i = 0; i < N; i++) {
dp[u][i] = 0;
for (auto [v, w] : e[u]) {
int ans = 1e9;
for (int t = 0; t <= i; t++) {
ans = std::min(ans, abs(w - t) + dp[v][i - t]);
}
dp[u][i] += ans;
dp[u][i] = std::min(dp[u][i], int(1e9));
}
}
}
int main(){
std::ios::sync_with_stdio(false);
std::cin.tie(nullptr);
std::cin >> n >> m;
for (int i = 1; i < n + m; i++) {
int p, w;
std::cin >> p >> w;
p--;
e[p].emplace_back(i, w);
}
memset(dp, 0x3f, sizeof dp);
dfs(0);
int ans = 1e9;
for (int i = 0; i < N; i++) {
ans = std::min(ans, dp[0][i]);
}
std::cout << ans << '\n';
return 0;
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
1 ms |
588 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
588 KB |
Output is correct |
2 |
Correct |
3 ms |
588 KB |
Output is correct |
3 |
Correct |
7 ms |
580 KB |
Output is correct |
4 |
Correct |
10 ms |
692 KB |
Output is correct |
5 |
Correct |
10 ms |
588 KB |
Output is correct |
6 |
Correct |
9 ms |
588 KB |
Output is correct |
7 |
Correct |
13 ms |
692 KB |
Output is correct |
8 |
Correct |
12 ms |
588 KB |
Output is correct |
9 |
Correct |
12 ms |
692 KB |
Output is correct |
10 |
Correct |
20 ms |
588 KB |
Output is correct |
11 |
Correct |
15 ms |
692 KB |
Output is correct |
12 |
Correct |
17 ms |
692 KB |
Output is correct |
13 |
Correct |
19 ms |
692 KB |
Output is correct |
14 |
Correct |
25 ms |
692 KB |
Output is correct |
15 |
Correct |
19 ms |
588 KB |
Output is correct |
16 |
Correct |
32 ms |
588 KB |
Output is correct |
17 |
Correct |
19 ms |
672 KB |
Output is correct |
18 |
Correct |
19 ms |
580 KB |
Output is correct |
19 |
Correct |
19 ms |
696 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
1 ms |
588 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
1 ms |
588 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |