Submission #397110

#TimeUsernameProblemLanguageResultExecution timeMemory
397110fedoseevtimofeyWorst Reporter 4 (JOI21_worst_reporter4)C++14
0 / 100
9 ms2508 KiB
#include <iostream> #include <string> #include <vector> #include <queue> #include <deque> #include <stack> #include <set> #include <map> #include <unordered_map> #include <unordered_set> #include <cstring> #include <cmath> #include <cstdlib> #include <algorithm> #include <random> #include <iomanip> #include <functional> #include <cassert> #include <bitset> #include <chrono> using namespace std; typedef long long ll; int main() { ios_base::sync_with_stdio(false); cin.tie(0); #ifdef LOCAL freopen("input.txt", "r", stdin); #endif int n; cin >> n; vector <int> a(n), h(n), c(n); vector <vector <int>> g(n); for (int i = 0; i < n; ++i) { cin >> a[i] >> h[i] >> c[i]; --a[i]; if (a[i] != i) { g[a[i]].push_back(i); } } vector <map <int, ll>> dp(n); auto dfs = [&] (int u, int p, auto&& dfs) -> void { for (auto v : g[u]) { if (v != p) { dfs(v, u, dfs); if (dp[v].size() > dp[u].size()) swap(dp[u], dp[v]); for (auto p : dp[v]) dp[u][p.first] += p.second; } } ll extra = 0; for (auto p : dp[u]) { if (p.first <= h[u]) { extra += p.second; } else { break; } } if (extra > c[u]) { dp[u][1] += c[u]; dp[u][h[u]] -= c[u]; dp[u][h[u] + 1] += c[u]; } else { while (!dp[u].empty() && dp[u].begin()->first <= h[u]) { dp[u].erase(dp[u].begin()); } dp[u][1] += extra; dp[u][h[u] + 1] += c[u]; } /*cout << "start " << u + 1 << endl; for (auto p : dp[u]) { cout << p.first << ' ' << p.second << '\n'; } cout << "finish\n";*/ }; dfs(0, -1, dfs); //cout << dp[0].begin()->second << '\n'; ll ans = 2e18; ll sum = 0; for (auto p : dp[0]) { sum += p.second; ans = min(ans, sum); } cout << ans << '\n'; }
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...