#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef pair<ll,ll> pll;
typedef pair<int,int> pii;
#define fi first
#define se second
#define endl '\n'
#define y1 holyshit
#define all(x) x.begin(),x.end()
const int inf=0x3f3f3f3f;
int N,M,P[300010];
ll C[300010],dp[333][333];
vector<pair<int,ll>> ch[300010];
void dfs(int now){
if(now>N){
dp[now][0]=0;
return;
}
for(auto i:ch[now]) dfs(i.fi);
for(int i=0;i<=300;i++){
dp[now][i]=0;
for(auto j:ch[now]){
ll curr=inf;
for(int k=0;k<=i;k++){
curr=min(curr,dp[j.fi][k]+abs(j.se-i+k));
}
dp[now][i]+=curr;
}
}
}
int main(){
ios_base::sync_with_stdio(0); cin.tie(0);
cin>>N>>M;
for(int i=2;i<=N+M;i++){
cin>>P[i]>>C[i];
ch[P[i]].push_back({i,C[i]});
}
memset(dp,inf,sizeof(dp));
dfs(1);
ll ans=inf;
for(int i=0;i<=300;i++) ans=min(ans,dp[1][i]);
cout<<ans;
return 0;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
12 ms |
8192 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
10 ms |
8320 KB |
Output is correct |
2 |
Correct |
11 ms |
8192 KB |
Output is correct |
3 |
Correct |
13 ms |
8320 KB |
Output is correct |
4 |
Correct |
14 ms |
8320 KB |
Output is correct |
5 |
Correct |
16 ms |
8320 KB |
Output is correct |
6 |
Correct |
16 ms |
8320 KB |
Output is correct |
7 |
Correct |
18 ms |
8320 KB |
Output is correct |
8 |
Correct |
19 ms |
8320 KB |
Output is correct |
9 |
Correct |
19 ms |
8320 KB |
Output is correct |
10 |
Correct |
21 ms |
8320 KB |
Output is correct |
11 |
Correct |
22 ms |
8240 KB |
Output is correct |
12 |
Correct |
23 ms |
8320 KB |
Output is correct |
13 |
Correct |
25 ms |
8320 KB |
Output is correct |
14 |
Correct |
25 ms |
8320 KB |
Output is correct |
15 |
Correct |
27 ms |
8320 KB |
Output is correct |
16 |
Correct |
25 ms |
8320 KB |
Output is correct |
17 |
Correct |
26 ms |
8320 KB |
Output is correct |
18 |
Correct |
25 ms |
8320 KB |
Output is correct |
19 |
Correct |
25 ms |
8320 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
12 ms |
8192 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
12 ms |
8192 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |