#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef pair<int, int> pii;
#define fi first
#define se second
#define mp make_pair
#define fastIO ios::sync_with_stdio(false);cin.tie(0);cout.tie(0);
const int N = 400;
const int C = 2 * N * N;
const int M = 2 * C;
const ll inf = (ll)1e18;
vector<pii> T[N];
ll dp[N][M];
void dfs(int u){
if(T[u].empty()){
for(int i = 0 ; i < M; i ++ ){
dp[u][i] = abs(i-C);
}
return;
}
for(int i = 0 ; i < M; i ++ ){
dp[u][i]=0;
}
for(auto x : T[u]){
dfs(x.fi);
for(int j = 0; j < M; j ++ ){
if(j < x.se) dp[u][j] = inf;
else dp[u][j] += dp[x.fi][j-x.se];
dp[u][j]=min(dp[u][j],inf);
}
}
ll di = inf;
for(int i = 0 ; i < M; i ++ ){
di ++ ;
if(di < dp[u][i]){
dp[u][i] = di;
}
else{
di = dp[u][i];
}
}
di = inf;
for(int i = M - 1; i >= 0 ; i -- ){
di ++ ;
if(di < dp[u][i]){
dp[u][i] = di;
}
else{
di = dp[u][i];
}
}
}
int main(){
fastIO;
int n, m;
cin >> n >> m;
for(int i = 2; i <= n + m ; i ++ ){
int p, c;
cin >> p >> c;
T[p].push_back(mp(i, c));
}
dfs(1);
ll res = inf;
for(int i = 0 ; i < M; i ++ ){
res = min(res, dp[1][i]);
}
cout << res << "\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... |