#include<bits/stdc++.h>
using namespace std;
#define pb push_back
using lli=long long int;
#define deb(x) cout<<#x<<": "<<x<<endl;
#define endl '\n'
const lli INF=1e18;
vector<vector<lli>> dp;
void dfs(lli x, vector<vector<pair<lli,lli>>> &sons){
for(auto y: sons[x]){
dfs(y.first, sons);
}
if(sons[x].size()==0){
dp[x][0]=0;
return;
}
for(lli i=0; i<=300; ++i){
dp[x][i]=0;
for(auto y: sons[x]){
lli aux=INF;
for(lli j=0; j<=i; ++j){
aux=min(aux, abs(y.second-j)+dp[y.first][i-j]);
}
dp[x][i]+=aux;
}
// deb(x);
// deb(i);
}
}
void solve(){
lli n,m;
cin>>n>>m;
if(n==1){
vector<lli> v(m);
for(lli i=0; i<m; ++i){
lli p, c;
cin>>p>>c;
v[i]=c;
}
lli ans=INF;
for(lli i=0; i<m; ++i){
lli aux=0;
for(lli j=0; j<m; ++j){
aux+=abs(v[i]-v[j]);
}
ans=min(ans, aux);
}
cout<<ans<<endl;
}
else{
dp.clear();
dp.resize(n+m+1,vector<lli> (301,INF));
vector<vector<pair<lli,lli>>> sons (n+m+1);
for(lli i=2; i<=n+m; ++i){
lli p, c;
cin>>p>>c;
sons[p].pb({i, c});
}
dfs(1, sons);
lli ans=INF;
for(lli x: dp[1]){
ans=min(ans, x);
}
cout<<ans<<endl;
}
}
int main(){
ios_base::sync_with_stdio(0);
cin.tie(0);
lli t=1;
// cin>>t;
while(t--){
solve();
}
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
344 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
0 ms |
348 KB |
Output is correct |
4 |
Correct |
0 ms |
408 KB |
Output is correct |
5 |
Correct |
0 ms |
348 KB |
Output is correct |
6 |
Correct |
0 ms |
348 KB |
Output is correct |
7 |
Correct |
0 ms |
348 KB |
Output is correct |
8 |
Correct |
0 ms |
348 KB |
Output is correct |
9 |
Correct |
0 ms |
348 KB |
Output is correct |
10 |
Correct |
0 ms |
348 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
348 KB |
Output is correct |
2 |
Correct |
2 ms |
540 KB |
Output is correct |
3 |
Correct |
2 ms |
348 KB |
Output is correct |
4 |
Correct |
3 ms |
604 KB |
Output is correct |
5 |
Correct |
4 ms |
604 KB |
Output is correct |
6 |
Correct |
5 ms |
604 KB |
Output is correct |
7 |
Correct |
5 ms |
828 KB |
Output is correct |
8 |
Correct |
6 ms |
860 KB |
Output is correct |
9 |
Correct |
7 ms |
860 KB |
Output is correct |
10 |
Correct |
8 ms |
860 KB |
Output is correct |
11 |
Correct |
8 ms |
860 KB |
Output is correct |
12 |
Correct |
9 ms |
856 KB |
Output is correct |
13 |
Correct |
10 ms |
1112 KB |
Output is correct |
14 |
Correct |
14 ms |
1116 KB |
Output is correct |
15 |
Correct |
15 ms |
1116 KB |
Output is correct |
16 |
Correct |
11 ms |
1116 KB |
Output is correct |
17 |
Correct |
11 ms |
1116 KB |
Output is correct |
18 |
Correct |
11 ms |
1116 KB |
Output is correct |
19 |
Correct |
12 ms |
1116 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
344 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
0 ms |
348 KB |
Output is correct |
4 |
Correct |
0 ms |
408 KB |
Output is correct |
5 |
Correct |
0 ms |
348 KB |
Output is correct |
6 |
Correct |
0 ms |
348 KB |
Output is correct |
7 |
Correct |
0 ms |
348 KB |
Output is correct |
8 |
Correct |
0 ms |
348 KB |
Output is correct |
9 |
Correct |
0 ms |
348 KB |
Output is correct |
10 |
Correct |
0 ms |
348 KB |
Output is correct |
11 |
Correct |
1 ms |
348 KB |
Output is correct |
12 |
Correct |
2 ms |
540 KB |
Output is correct |
13 |
Correct |
2 ms |
348 KB |
Output is correct |
14 |
Correct |
3 ms |
604 KB |
Output is correct |
15 |
Correct |
4 ms |
604 KB |
Output is correct |
16 |
Correct |
5 ms |
604 KB |
Output is correct |
17 |
Correct |
5 ms |
828 KB |
Output is correct |
18 |
Correct |
6 ms |
860 KB |
Output is correct |
19 |
Correct |
7 ms |
860 KB |
Output is correct |
20 |
Correct |
8 ms |
860 KB |
Output is correct |
21 |
Correct |
8 ms |
860 KB |
Output is correct |
22 |
Correct |
9 ms |
856 KB |
Output is correct |
23 |
Correct |
10 ms |
1112 KB |
Output is correct |
24 |
Correct |
14 ms |
1116 KB |
Output is correct |
25 |
Correct |
15 ms |
1116 KB |
Output is correct |
26 |
Correct |
11 ms |
1116 KB |
Output is correct |
27 |
Correct |
11 ms |
1116 KB |
Output is correct |
28 |
Correct |
11 ms |
1116 KB |
Output is correct |
29 |
Correct |
12 ms |
1116 KB |
Output is correct |
30 |
Incorrect |
18 ms |
1592 KB |
Output isn't correct |
31 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
344 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
0 ms |
348 KB |
Output is correct |
4 |
Correct |
0 ms |
408 KB |
Output is correct |
5 |
Correct |
0 ms |
348 KB |
Output is correct |
6 |
Correct |
0 ms |
348 KB |
Output is correct |
7 |
Correct |
0 ms |
348 KB |
Output is correct |
8 |
Correct |
0 ms |
348 KB |
Output is correct |
9 |
Correct |
0 ms |
348 KB |
Output is correct |
10 |
Correct |
0 ms |
348 KB |
Output is correct |
11 |
Correct |
1 ms |
348 KB |
Output is correct |
12 |
Correct |
2 ms |
540 KB |
Output is correct |
13 |
Correct |
2 ms |
348 KB |
Output is correct |
14 |
Correct |
3 ms |
604 KB |
Output is correct |
15 |
Correct |
4 ms |
604 KB |
Output is correct |
16 |
Correct |
5 ms |
604 KB |
Output is correct |
17 |
Correct |
5 ms |
828 KB |
Output is correct |
18 |
Correct |
6 ms |
860 KB |
Output is correct |
19 |
Correct |
7 ms |
860 KB |
Output is correct |
20 |
Correct |
8 ms |
860 KB |
Output is correct |
21 |
Correct |
8 ms |
860 KB |
Output is correct |
22 |
Correct |
9 ms |
856 KB |
Output is correct |
23 |
Correct |
10 ms |
1112 KB |
Output is correct |
24 |
Correct |
14 ms |
1116 KB |
Output is correct |
25 |
Correct |
15 ms |
1116 KB |
Output is correct |
26 |
Correct |
11 ms |
1116 KB |
Output is correct |
27 |
Correct |
11 ms |
1116 KB |
Output is correct |
28 |
Correct |
11 ms |
1116 KB |
Output is correct |
29 |
Correct |
12 ms |
1116 KB |
Output is correct |
30 |
Incorrect |
18 ms |
1592 KB |
Output isn't correct |
31 |
Halted |
0 ms |
0 KB |
- |