#include <bits/stdc++.h>
using namespace std;
#define pii pair<int,int>
#define fs first
#define sc second
#define ll long long
const int mxn = 330;
int N,M;
vector<pii> tree[mxn];
int dp[mxn][mxn],dep[mxn];
int par[mxn];
bitset<mxn> boom;
void dfs(int now){
if(boom[now]){
for(int i = 0;i<mxn;i++)dp[now][i] = abs(dep[now]-i);
}
for(auto [nxt,w]:tree[now]){
dep[nxt] = dep[now]+w;
dfs(nxt);
for(int i = 0;i<mxn;i++){
int mn = 1e9;
for(int j = 0;j<mxn;j++){
mn = min(mn,dp[nxt][j]+abs(i-j));
}
dp[now][i] += mn;
}
}
return;
}
int main(){
ios::sync_with_stdio(0);cin.tie(0);cout.tie(0);
cin>>N>>M;
for(int i = 2;i<=N+M;i++){
int p,c;
cin>>p>>c;
par[i] = p;
tree[p].push_back(pii(i,c));
}
for(int i = N+1;i<=N+M;i++)boom[i] = true;
dfs(1);
cout<<*min_element(dp[1],dp[1]+mxn)<<'\n';
return 0;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
1 ms |
348 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
348 KB |
Output is correct |
2 |
Correct |
3 ms |
348 KB |
Output is correct |
3 |
Incorrect |
5 ms |
544 KB |
Output isn't correct |
4 |
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 |
1 ms |
348 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |