Submission #966467

#TimeUsernameProblemLanguageResultExecution timeMemory
966467steveonalexFireworks (APIO16_fireworks)C++17
19 / 100
20 ms1716 KiB
#include <bits/stdc++.h>
 
using namespace std;
 
typedef long long ll;
typedef unsigned long long ull;
 
#define ALL(v) (v).begin(), (v).end()
#define MASK(i) (1LL << (i))
#define GETBIT(mask, i) (((mask) >> (i)) & 1)
 
// mt19937_64 rng(chrono::steady_clock::now().time_since_epoch().count());
mt19937_64 rng(1);
ll rngesus(ll l, ll r){return ((ull) rng()) % (r - l + 1) + l;}
 
ll max(ll a, ll b){return (a > b) ? a : b;}
ll min(ll a, ll b){return (a < b) ? a : b;}
 
ll LASTBIT(ll mask){return mask & (-mask);}
ll pop_cnt(ll mask){return __builtin_popcountll(mask);}
ll ctz(ll mask){return __builtin_ctzll(mask);}
ll clz(ll mask){return __builtin_clzll(mask);}
ll logOf(ll mask){return 63 - clz(mask);}
 
template <class T1, class T2>
    bool minimize(T1 &a, T2 b){
        if (a > b){a = b; return true;}
        return false;
    }
template <class T1, class T2>
    bool maximize(T1 &a, T2 b){
        if (a < b){a = b; return true;}
        return false;
    }
template <class T>
    void printArr(T& a, string separator = " ", string finish = "\n", ostream& out = cout){
        for(auto i: a) out << i << separator;
        out << finish;
    }
template <class T>
    void remove_dup(vector<T> &a){
        sort(ALL(a));
        a.resize(unique(ALL(a)) - a.begin());
    }

const int N = 310;
const ll INF = 1e18 + 69;
int n, m;
vector<pair<int,int>> graph[N];
ll dp[N][N];

void dfs(int u){
    if (u > n){
        for(int i = 1; i<N; ++i) dp[u][i] = INF;
        return;
    }
    for(pair<int, int> v: graph[u]){
        dfs(v.first);
        ll f[N];

        for(int i = 0; i<N; ++i){
            f[i] = INF;
            for(int j = 0; j<=i; ++j){
                minimize(f[i], dp[v.first][j] + abs(v.second - (i - j)));
            }
        }
        for(int i = 0; i<N; ++i) dp[u][i] += f[i];
    }
}

int main(void){
    ios::sync_with_stdio(0); cin.tie(0); cout.tie(0);

    cin >> n >> m;
    for(int i = 2; i<=n+m; ++i){
        int p, c; cin >> p >> c;
        graph[p].push_back({i, c});
    }

    dfs(1);

    ll ans = INF;
    for(int i = 0; i<N; ++i) minimize(ans, dp[1][i]);

    cout << ans << "\n";

    return 0;
}
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...