제출 #739278

#제출 시각아이디문제언어결과실행 시간메모리
739278Alihan_8Fireworks (APIO16_fireworks)C++17
19 / 100
18 ms1076 KiB
#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 timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...