Submission #397115

#TimeUsernameProblemLanguageResultExecution timeMemory
397115fedoseevtimofeyWorst Reporter 4 (JOI21_worst_reporter4)C++14
0 / 100
8 ms1868 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; } } dp[u][1] += c[u]; dp[u][h[u]] -= c[u]; dp[u][h[u] + 1] += c[u]; while (dp[u][h[u]] < 0) { auto it = dp[u].find(h[u]); auto jt = prev(it); it->second += jt->second; dp[u].erase(jt); } if (dp[u][h[u]] == 0) dp[u].erase(h[u]); }; dfs(0, -1, dfs); 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...