Submission #792818

#TimeUsernameProblemLanguageResultExecution timeMemory
792818IvanJFireworks (APIO16_fireworks)C++17
7 / 100
5 ms7380 KiB
#include<bits/stdc++.h> #define pb push_back #define x first #define y second #define all(a) (a).begin(), (a).end() using namespace std; typedef long long ll; typedef pair<int, int> ii; const int maxn = 3e5 + 5; struct Data { int idx; ll lo, hi, ans; }; int n, m; ll w[maxn]; ll depth[maxn]; Data dp[maxn]; vector<pair<int, ll>> adj[maxn]; void dfs(int x) { for(auto p : adj[x]) depth[p.x] = p.y + depth[x], w[p.x] = p.y, dfs(p.x); } Data solve(int x) { if(adj[x].size() == 0) return dp[x] = {x, depth[x], depth[x], 0}; ll sum = 0; vector<Data> ch; for(auto p : adj[x]) ch.pb(solve(p.x)), sum += ch.back().ans; set<ll> vals; for(Data D : ch) vals.insert(D.lo), vals.insert(D.hi); vector<ll> v; ll ans = 1e18; for(ll val : vals) { ll sol = 0; for(Data D : ch) { if(D.lo <= val && val <= D.hi) continue; if(val > D.hi) sol += val - D.hi; else { if(D.lo - val > w[D.idx]) {sol = 1e18;break;} else sol += D.lo - val; } } if(sol < ans) v.clear(), ans = sol; if(sol == ans) v.pb(val); } Data ret; ret.idx = x; ret.ans = ans + sum; ret.lo = v[0]; ret.hi = v.back(); //cout << x + 1 << " -> " << ret.lo << " " << ret.hi << " " << ret.ans << "\n"; return dp[x] = ret; } int main() { scanf("%d%d", &n, &m); for(int i = 1;i < n + m;i++) { int p, c; scanf("%d%d", &p, &c); p--; adj[p].pb({i, c}); } dfs(0); Data ans = solve(0); printf("%lld\n", ans.ans); return 0; }

Compilation message (stderr)

fireworks.cpp: In function 'int main()':
fireworks.cpp:70:7: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
   70 |  scanf("%d%d", &n, &m);
      |  ~~~~~^~~~~~~~~~~~~~~~
fireworks.cpp:73:8: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
   73 |   scanf("%d%d", &p, &c);
      |   ~~~~~^~~~~~~~~~~~~~~~
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...