Submission #875540

#TimeUsernameProblemLanguageResultExecution timeMemory
875540NeroZeinFireworks (APIO16_fireworks)C++17
0 / 100
0 ms348 KiB
#include "bits/stdc++.h" using namespace std; #ifdef Nero #include "Deb.h" #else #define deb(...) #endif const int N = 5003; long long dis[N]; long long mn[N], mx[N]; vector<pair<int, int>> g[N]; void dfs(int v) { if (g[v].size() == 0) { mn[v] = mx[v] = dis[v]; return; } mn[v] = LLONG_MAX; mx[v] = -LLONG_MAX; for (auto [u, w] : g[v]) { dis[u] = dis[v] + w; dfs(u); mn[v] = min(mn[v], mn[u]); mx[v] = max(mx[v], mx[u]); } } long long dfs2(int v, long long sub, const long long req) { long long ret = 0; for (auto [u, w] : g[v]) { long long t = 0, r = 1; if (mn[u] - sub > req) { t = min((long long) w, mn[u] - sub - req); } else if (mx[u] - sub < req) { t = min((long long) w, req - (mx[u] - sub)); r = -1; } ret += t + dfs2(u, sub + t * r, req); } return ret; } int main() { ios::sync_with_stdio(false); cin.tie(nullptr); int n, m; cin >> n >> m; for(int i = 2; i <= n + m; ++i) { int p, c; cin >> p >> c; g[p].emplace_back(i, c); } dfs(1); long long ans = LLONG_MAX; for (int i = n + 1; i <= n + m; ++i) { ans = min(ans, dfs2(1, 0, dis[i])); //deb(dis[i]) deb(dfs2(1, 0, dis[i])) cout << '\n'; } 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...