#include <bits/stdc++.h>
using namespace std;
#define pii pair<int,int>
#define fs first
#define sc second
#define ll long long
const ll inf = 1e9;
const int mxn = 330;
int N,M;
vector<pii> tree[mxn];
ll dp[mxn][mxn],dep[mxn];
pii par[mxn];
bitset<mxn> boom;
void dfs(int now){
fill(dp[now],dp[now]+mxn,inf);
if(boom[now])dp[now][dep[now]] = 0;
else fill(dp[now],dp[now]+mxn,0);
for(auto [nxt,w]:tree[now]){
//cerr<<now<<":"<<nxt<<endl;
dep[nxt] = dep[now]+w;
dfs(nxt);
for(int i = 0;i<mxn;i++){
dp[now][i] += dp[nxt][i];
}
}
ll tmp[mxn];
fill(tmp,tmp+mxn,inf);
for(int i = 0;i<mxn;i++){
for(int j = 0;j<mxn;j++){
if(i-j>=-par[now].sc)tmp[i] = min(tmp[i],dp[now][j]+abs(i-j));
}
}
//cerr<<now<<' '<<par[now].sc<<":";for(int j = 0;j<=30;j++)cerr<<(tmp[j]>=inf?-1:tmp[j])<<' ';cerr<<endl;
for(int i = 0;i<mxn;i++)dp[now][i] = tmp[i];
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] = pii(p,c);
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 |
Runtime error |
1 ms |
600 KB |
Execution killed with signal 11 |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
348 KB |
Output is correct |
2 |
Correct |
4 ms |
572 KB |
Output is correct |
3 |
Correct |
6 ms |
468 KB |
Output is correct |
4 |
Correct |
10 ms |
860 KB |
Output is correct |
5 |
Correct |
12 ms |
784 KB |
Output is correct |
6 |
Correct |
19 ms |
776 KB |
Output is correct |
7 |
Correct |
15 ms |
860 KB |
Output is correct |
8 |
Correct |
23 ms |
856 KB |
Output is correct |
9 |
Correct |
28 ms |
860 KB |
Output is correct |
10 |
Correct |
22 ms |
860 KB |
Output is correct |
11 |
Correct |
23 ms |
856 KB |
Output is correct |
12 |
Correct |
26 ms |
1024 KB |
Output is correct |
13 |
Correct |
27 ms |
1112 KB |
Output is correct |
14 |
Correct |
29 ms |
1884 KB |
Output is correct |
15 |
Correct |
40 ms |
1372 KB |
Output is correct |
16 |
Correct |
30 ms |
1112 KB |
Output is correct |
17 |
Correct |
30 ms |
1264 KB |
Output is correct |
18 |
Correct |
30 ms |
1116 KB |
Output is correct |
19 |
Correct |
30 ms |
1116 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Runtime error |
1 ms |
600 KB |
Execution killed with signal 11 |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Runtime error |
1 ms |
600 KB |
Execution killed with signal 11 |
2 |
Halted |
0 ms |
0 KB |
- |