#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;
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 |
Incorrect |
1 ms |
384 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
384 KB |
Output is correct |
2 |
Correct |
3 ms |
384 KB |
Output is correct |
3 |
Correct |
4 ms |
512 KB |
Output is correct |
4 |
Correct |
7 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 |
9 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 |
14 ms |
896 KB |
Output is correct |
11 |
Correct |
15 ms |
896 KB |
Output is correct |
12 |
Correct |
16 ms |
1024 KB |
Output is correct |
13 |
Correct |
17 ms |
1044 KB |
Output is correct |
14 |
Correct |
21 ms |
1020 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 |
23 ms |
1024 KB |
Output is correct |
19 |
Correct |
21 ms |
1024 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
1 ms |
384 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
1 ms |
384 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |