#include <bits/stdc++.h>
using namespace std;
#define all(x) x.begin(), x.end()
#define pb push_back
#define ln '\n'
#define int long long
template <class _T>
bool chmin(_T &x, const _T &y){
bool flag = false;
if ( x > y ){
x = y; flag |= true;
}
return flag;
}
template <class _T>
bool chmax(_T &x, const _T &y){
bool flag = false;
if ( x < y ){
x = y; flag |= true;
}
return flag;
}
signed main(){
ios_base::sync_with_stdio(false);
cin.tie(nullptr);
int n, m; cin >> n >> m;
vector <pair<int,int>> g[n + m], op;
for ( int i = 1; i < n + m; i++ ){
int x, w; cin >> x >> w;
g[--x].pb({i, w});
}
const int inf = 1e15 + 1, N = 3e2 + 1;
vector <vector<int>> dp(N, vector <int> (N, inf));
function <void(int)> dfs = [&](int x){
if ( g[x].empty() ){
dp[x][0] = 0;
return;
}
for ( auto [to, w]: g[x] ){
dfs(to);
}
for ( int i = 0; i < N; i++ ){
dp[x][i] = 0;
for ( auto [to, w]: g[x] ){
int Mn = inf;
for ( int t = 0; t <= i; t++ ){
chmin(Mn, abs(t - w) + dp[to][i - t]);
}
dp[x][i] = min(inf, dp[x][i] + Mn);
}
}
};
dfs(0);
int Mn = inf;
for ( int i = 0; i < N; i++ ){
chmin(Mn, dp[0][i]);
}
cout << Mn;
cout << '\n';
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
1 ms |
980 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
980 KB |
Output is correct |
2 |
Correct |
3 ms |
980 KB |
Output is correct |
3 |
Correct |
4 ms |
980 KB |
Output is correct |
4 |
Correct |
6 ms |
980 KB |
Output is correct |
5 |
Correct |
7 ms |
980 KB |
Output is correct |
6 |
Correct |
7 ms |
980 KB |
Output is correct |
7 |
Correct |
8 ms |
964 KB |
Output is correct |
8 |
Correct |
10 ms |
1044 KB |
Output is correct |
9 |
Correct |
10 ms |
980 KB |
Output is correct |
10 |
Correct |
13 ms |
1044 KB |
Output is correct |
11 |
Correct |
13 ms |
1044 KB |
Output is correct |
12 |
Correct |
13 ms |
1052 KB |
Output is correct |
13 |
Correct |
16 ms |
984 KB |
Output is correct |
14 |
Correct |
15 ms |
1076 KB |
Output is correct |
15 |
Correct |
17 ms |
1052 KB |
Output is correct |
16 |
Correct |
18 ms |
980 KB |
Output is correct |
17 |
Correct |
16 ms |
980 KB |
Output is correct |
18 |
Correct |
16 ms |
964 KB |
Output is correct |
19 |
Correct |
16 ms |
1048 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
1 ms |
980 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
1 ms |
980 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |