#include <bits/stdc++.h>
using namespace std;
const int dydis = 3e5 + 10;
const long long inf = 1e15;
int n, m, k;
const int MX = 300;
vector<pair<int, int> > gr[dydis];
long long dp[dydis][MX+1] = {};
long long st[dydis] = {};
void dfs(int v) {
if(gr[v].size() == 0) {
for(auto &x : dp[v]) x = inf;
dp[v][0] = 0;
return ;
}
for(auto x : gr[v]) {
dfs(x.first);
}
for(auto x : gr[v]) {
int u = x.first; long long w = x.second;
for(int i = 0; i <= MX; i++) st[i] = inf;
for(int i = 0; i <= MX; i++) {
for(int j = 0; i+j <= MX; j++) {
st[i+j] = min(st[i+j], abs(i-w) + dp[u][j]);
}
}
for(int i = 0; i <= MX; i++) {
dp[v][i] += st[i];
}
}
//cout << "dp[" << v+1 << "]:\n";
// for(int i = 0; i <= MX; i++) {
// cout << i << ": " << dp[v][i] << endl;
// }
}
int main() {
//ifstream in("in.txt");
cin.tie(NULL);
ios_base::sync_with_stdio(false);
cin >> m >> k;
n = m + k;
for(int i = 1; i < n; i++) {
int p, w;
cin >> p >> w; p--;
gr[p].push_back({i, w});
//gr[i].push_back({p, w});
}
dfs(0);
long long ans = inf;
for(int i = 0; i <= MX; i++) {
ans = min(ans, dp[0][i]);
}
cout << ans;
return 0;
}
/*
4 6
1 5
2 5
2 8
3 3
3 2
3 3
2 9
4 4
4 3
*/
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
4 ms |
7380 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
5 ms |
7380 KB |
Output is correct |
2 |
Correct |
7 ms |
7440 KB |
Output is correct |
3 |
Correct |
7 ms |
7508 KB |
Output is correct |
4 |
Correct |
8 ms |
7504 KB |
Output is correct |
5 |
Correct |
9 ms |
7636 KB |
Output is correct |
6 |
Correct |
11 ms |
7616 KB |
Output is correct |
7 |
Correct |
15 ms |
7668 KB |
Output is correct |
8 |
Correct |
13 ms |
7800 KB |
Output is correct |
9 |
Correct |
12 ms |
7764 KB |
Output is correct |
10 |
Correct |
12 ms |
7780 KB |
Output is correct |
11 |
Correct |
14 ms |
7940 KB |
Output is correct |
12 |
Correct |
16 ms |
7920 KB |
Output is correct |
13 |
Correct |
14 ms |
7960 KB |
Output is correct |
14 |
Correct |
18 ms |
8020 KB |
Output is correct |
15 |
Correct |
17 ms |
8000 KB |
Output is correct |
16 |
Correct |
16 ms |
7984 KB |
Output is correct |
17 |
Correct |
16 ms |
7972 KB |
Output is correct |
18 |
Correct |
16 ms |
8012 KB |
Output is correct |
19 |
Correct |
16 ms |
8016 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
4 ms |
7380 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
4 ms |
7380 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |