This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#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';
}
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |