Submission #752951

#TimeUsernameProblemLanguageResultExecution timeMemory
752951benjaminkleynFireworks (APIO16_fireworks)C++17
7 / 100
7 ms7380 KiB
#include <bits/stdc++.h> using namespace std; typedef long long ll; const int MAX = 300010; int n, m; int p[2 * MAX]; ll c[2 * MAX]; vector<pair<int, ll>> g[MAX]; struct state { ll cost, distance, wiggle_room; }; pair<ll,ll> dp(int u) { if (u > n) return {0, 0}; ll res = 0; vector<ll> times; ll min_val = 0; for (auto [v, l] : g[u]) { auto [t, d] = dp(v); res += t; times.push_back(l + d); min_val = max(min_val, d); } vector<ll> t = times; sort(t.begin(), t.end()); if (min_val >= t[t.size() / 2]) { ll best = min_val; for (ll x : times) res += abs(best - x); return {res, best}; } ll best = t[(t.size() - 1) / 2]; for (ll x : times) res += abs(best - x); return {res, best}; } int main() { cin.tie(0)->sync_with_stdio(0); cin >> n >> m; p[1] = -1, c[1] = 0; for (int i = 2; i <= n + m; i++) { cin >> p[i] >> c[i]; g[p[i]].push_back({i, c[i]}); } cout << dp(1).first << '\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...