Submission #1327600

#TimeUsernameProblemLanguageResultExecution timeMemory
1327600absolut3Fireworks (APIO16_fireworks)C++20
0 / 100
1 ms332 KiB
//#pragma GCC optimize("O3")
#include <bits/stdc++.h>

using namespace std;

#define ll long long
#define ld long double
#define pb push_back
#define all(x) (x).begin(), (x).end()
#define srt(x) sort(all(x))
#define sz(x) (ll)(x.size())
#define pii pair < int, int >
#define pll pair < ll, ll >

#define debug(x) cerr << (#x) << " = " << (x) << endl

const int mod = 1e9 + 7;
const int N = 305;
const ll OO = 1e18;

template<typename T>
bool umn (T &fi, T se) { return fi > se ? (fi = se, 1) : 0; }

template<typename T>
bool umx (T &fi, T se) { return fi < se ? (fi = se, 1) : 0; }

int n, m, add[N];
ll w[N];
vector < int > a[N];
vector < pii > g[N];

ll calc (int i, int v) {
    ll res = OO;
    for (int j : a[v])
        res = min(res, (ll)abs(j - i));
    return res;
}

void dfs (int v) {
    for (auto [u, wi] : g[v]) {
        w[u] = w[v] + wi;
        dfs(u);
        add[v] += add[u];
    }

    if (v >= n) {
        a[v].pb(w[v]);
    } else {
        ll mn = OO;
        for (int i = w[v]; i <= 305; ++i) {
            ll res = 0;
            for (auto [u, wi] : g[v])
                res += calc(i, u);
            umn(mn, res);
        }
        for (int i = w[v]; i <= 305; ++i) {
            ll res = 0;
            for (auto [u, wi] : g[v])
                res += calc(i, u);
            if (res == mn)
                a[v].pb(i);
        }
        add[v] += mn;
    }
}

void slv () {
    cin >> n >> m;
    for (int i = 1, p, c; i < n + m; ++i) {
        cin >> p >> c;
        g[p - 1].pb({i, c});
    }
    dfs(0);
    cout << add[0];
}

signed main () {
    ios_base::sync_with_stdio(false);
    cin.tie(nullptr);
    int test = 1;
//    cin >> test;
    while (test--) {
        slv();
        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...