#include<bits/stdc++.h>
using namespace std;
#define int long long
#define rep(i,n) for(int i=0;i<n;i++)
#define REP(i,n) for(int i=1;i<n;i++)
#define rev(i,n) for(int i=n-1;i>=0;i--)
#define all(v) v.begin(),v.end()
#define P pair<int,int>
#define len(s) (int)s.size()
template<class T> inline bool chmin(T &a, T b){
if(a>b){a=b;return true;}
return false;
}
template<class T> inline bool chmax(T &a, T b){
if(a<b){a=b;return true;}
return false;
}
constexpr int mod = 1e9+7;
constexpr long long inf = 3e18;
int N,M;
vector<P>G[305];
int dp[305][305];
void dfs(int x,int p){
bool flag=false;
for(auto i:G[x])if(i.first!=p){dfs(i.first,x);flag=1;}
if(!flag){
fill(dp[x]+1,dp[x]+301,inf);
}
rep(k,301){
for(auto i:G[x])if(i.first!=p){
int mn=inf;
rep(j,k+1){
chmin(mn,dp[i.first][j]+abs((k-j)-i.second));
}
dp[x][k]+=mn;
}
}
}
signed main(){
cin.tie(0);ios::sync_with_stdio(false);
cin>>N>>M;
if(N==1){
vector<int>v(M);
for(int &i:v)cin>>i>>i;
sort(all(v));
int med=v[len(v)/2];
int ans=0;
for(int &i:v)ans+=abs(med-i);
cout<<ans<<endl;
return 0;
}
REP(i,N+M){
int a,b;cin>>a>>b;a--;
G[a].push_back({i,b});
}
dfs(0,0);
int ans=inf;
rep(i,301)chmin(ans,dp[0][i]);
cout<<ans<<endl;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
384 KB |
Output is correct |
2 |
Correct |
0 ms |
384 KB |
Output is correct |
3 |
Correct |
0 ms |
384 KB |
Output is correct |
4 |
Correct |
1 ms |
384 KB |
Output is correct |
5 |
Correct |
0 ms |
384 KB |
Output is correct |
6 |
Correct |
1 ms |
384 KB |
Output is correct |
7 |
Correct |
0 ms |
384 KB |
Output is correct |
8 |
Correct |
0 ms |
384 KB |
Output is correct |
9 |
Correct |
1 ms |
384 KB |
Output is correct |
10 |
Correct |
0 ms |
384 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
384 KB |
Output is correct |
2 |
Correct |
2 ms |
384 KB |
Output is correct |
3 |
Correct |
4 ms |
512 KB |
Output is correct |
4 |
Correct |
6 ms |
512 KB |
Output is correct |
5 |
Correct |
7 ms |
640 KB |
Output is correct |
6 |
Correct |
8 ms |
640 KB |
Output is correct |
7 |
Correct |
11 ms |
768 KB |
Output is correct |
8 |
Correct |
11 ms |
768 KB |
Output is correct |
9 |
Correct |
11 ms |
768 KB |
Output is correct |
10 |
Correct |
13 ms |
896 KB |
Output is correct |
11 |
Correct |
14 ms |
896 KB |
Output is correct |
12 |
Correct |
17 ms |
1024 KB |
Output is correct |
13 |
Correct |
16 ms |
1024 KB |
Output is correct |
14 |
Correct |
17 ms |
1024 KB |
Output is correct |
15 |
Correct |
18 ms |
1024 KB |
Output is correct |
16 |
Correct |
18 ms |
1024 KB |
Output is correct |
17 |
Correct |
18 ms |
1024 KB |
Output is correct |
18 |
Correct |
18 ms |
1024 KB |
Output is correct |
19 |
Correct |
19 ms |
1024 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
384 KB |
Output is correct |
2 |
Correct |
0 ms |
384 KB |
Output is correct |
3 |
Correct |
0 ms |
384 KB |
Output is correct |
4 |
Correct |
1 ms |
384 KB |
Output is correct |
5 |
Correct |
0 ms |
384 KB |
Output is correct |
6 |
Correct |
1 ms |
384 KB |
Output is correct |
7 |
Correct |
0 ms |
384 KB |
Output is correct |
8 |
Correct |
0 ms |
384 KB |
Output is correct |
9 |
Correct |
1 ms |
384 KB |
Output is correct |
10 |
Correct |
0 ms |
384 KB |
Output is correct |
11 |
Correct |
1 ms |
384 KB |
Output is correct |
12 |
Correct |
2 ms |
384 KB |
Output is correct |
13 |
Correct |
4 ms |
512 KB |
Output is correct |
14 |
Correct |
6 ms |
512 KB |
Output is correct |
15 |
Correct |
7 ms |
640 KB |
Output is correct |
16 |
Correct |
8 ms |
640 KB |
Output is correct |
17 |
Correct |
11 ms |
768 KB |
Output is correct |
18 |
Correct |
11 ms |
768 KB |
Output is correct |
19 |
Correct |
11 ms |
768 KB |
Output is correct |
20 |
Correct |
13 ms |
896 KB |
Output is correct |
21 |
Correct |
14 ms |
896 KB |
Output is correct |
22 |
Correct |
17 ms |
1024 KB |
Output is correct |
23 |
Correct |
16 ms |
1024 KB |
Output is correct |
24 |
Correct |
17 ms |
1024 KB |
Output is correct |
25 |
Correct |
18 ms |
1024 KB |
Output is correct |
26 |
Correct |
18 ms |
1024 KB |
Output is correct |
27 |
Correct |
18 ms |
1024 KB |
Output is correct |
28 |
Correct |
18 ms |
1024 KB |
Output is correct |
29 |
Correct |
19 ms |
1024 KB |
Output is correct |
30 |
Runtime error |
1 ms |
512 KB |
Execution killed with signal 11 (could be triggered by violating memory limits) |
31 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
384 KB |
Output is correct |
2 |
Correct |
0 ms |
384 KB |
Output is correct |
3 |
Correct |
0 ms |
384 KB |
Output is correct |
4 |
Correct |
1 ms |
384 KB |
Output is correct |
5 |
Correct |
0 ms |
384 KB |
Output is correct |
6 |
Correct |
1 ms |
384 KB |
Output is correct |
7 |
Correct |
0 ms |
384 KB |
Output is correct |
8 |
Correct |
0 ms |
384 KB |
Output is correct |
9 |
Correct |
1 ms |
384 KB |
Output is correct |
10 |
Correct |
0 ms |
384 KB |
Output is correct |
11 |
Correct |
1 ms |
384 KB |
Output is correct |
12 |
Correct |
2 ms |
384 KB |
Output is correct |
13 |
Correct |
4 ms |
512 KB |
Output is correct |
14 |
Correct |
6 ms |
512 KB |
Output is correct |
15 |
Correct |
7 ms |
640 KB |
Output is correct |
16 |
Correct |
8 ms |
640 KB |
Output is correct |
17 |
Correct |
11 ms |
768 KB |
Output is correct |
18 |
Correct |
11 ms |
768 KB |
Output is correct |
19 |
Correct |
11 ms |
768 KB |
Output is correct |
20 |
Correct |
13 ms |
896 KB |
Output is correct |
21 |
Correct |
14 ms |
896 KB |
Output is correct |
22 |
Correct |
17 ms |
1024 KB |
Output is correct |
23 |
Correct |
16 ms |
1024 KB |
Output is correct |
24 |
Correct |
17 ms |
1024 KB |
Output is correct |
25 |
Correct |
18 ms |
1024 KB |
Output is correct |
26 |
Correct |
18 ms |
1024 KB |
Output is correct |
27 |
Correct |
18 ms |
1024 KB |
Output is correct |
28 |
Correct |
18 ms |
1024 KB |
Output is correct |
29 |
Correct |
19 ms |
1024 KB |
Output is correct |
30 |
Runtime error |
1 ms |
512 KB |
Execution killed with signal 11 (could be triggered by violating memory limits) |
31 |
Halted |
0 ms |
0 KB |
- |