#include<iostream>
#include<algorithm>
#include<vector>
using namespace std;
typedef long long ll;
#define chmin(a,b) a=min(a,b)
#define chmax(a,b) a=max(a,b)
#define rep(i,n) for(int i=0;i<n;i++)
#define mod 1000000007
#define mad(a,b) a=(a+b)%mod
#define N 310
ll n,m;
vector<pair<ll,ll> > g[N];
ll dp[N][N];
void dfs(ll x){
if(x>n){
rep(i,N)dp[x][i]=1e9;
dp[x][0]=0;
return;
}
rep(i,N)dp[x][i]=0;
for(auto e:g[x]){
dfs(e.first);
rep(i,N){
ll mi=1e9;
for(int j=0;j<=i;j++){
chmin(mi,dp[e.first][j]+abs((i-j)-e.second));
}
dp[x][i]+=mi;
}
}
}
int main(){
cin>>n>>m;
for(int i=2;i<=n+m;i++){
ll p,c; cin>>p>>c;
g[p].push_back(make_pair(i,c));
}
dfs(1);
ll ans=1e17;
rep(i,N){
chmin(ans,dp[1][i]);
}
cout<<ans<<endl;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
5 ms |
376 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
5 ms |
376 KB |
Output is correct |
2 |
Correct |
7 ms |
504 KB |
Output is correct |
3 |
Correct |
8 ms |
504 KB |
Output is correct |
4 |
Correct |
13 ms |
504 KB |
Output is correct |
5 |
Correct |
12 ms |
632 KB |
Output is correct |
6 |
Correct |
13 ms |
632 KB |
Output is correct |
7 |
Correct |
14 ms |
760 KB |
Output is correct |
8 |
Correct |
15 ms |
760 KB |
Output is correct |
9 |
Correct |
17 ms |
760 KB |
Output is correct |
10 |
Correct |
19 ms |
892 KB |
Output is correct |
11 |
Correct |
19 ms |
888 KB |
Output is correct |
12 |
Correct |
21 ms |
1016 KB |
Output is correct |
13 |
Correct |
21 ms |
1016 KB |
Output is correct |
14 |
Correct |
24 ms |
1144 KB |
Output is correct |
15 |
Correct |
23 ms |
1144 KB |
Output is correct |
16 |
Correct |
23 ms |
1144 KB |
Output is correct |
17 |
Correct |
23 ms |
1016 KB |
Output is correct |
18 |
Correct |
24 ms |
1016 KB |
Output is correct |
19 |
Correct |
23 ms |
1016 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
5 ms |
376 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
5 ms |
376 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |