Submission #415533

#TimeUsernameProblemLanguageResultExecution timeMemory
415533Aldas25Fireworks (APIO16_fireworks)C++14
100 / 100
336 ms68840 KiB
#include <bits/stdc++.h> using namespace std; #define FAST_IO ios_base::sync_with_stdio(0); cin.tie(0) #define FOR(i, a, b) for(int i = (a); i <= (b); i++) #define REP(n) FOR(O, 1, (n)) #define pb push_back #define f first #define s second typedef long double ld; typedef long long ll; typedef pair<int, int> pii; typedef pair<int, pii> piii; typedef vector<int> vi; typedef vector<pii> vii; typedef vector<ll> vl; typedef vector<piii> viii; const int MAXN = 300100, MAXK = 30; //const ll MOD = 998244353; const ll INF = 4e18; const ld PI = asin(1) * 2; priority_queue<ll> q[MAXN]; int p[MAXN]; ll c[MAXN]; vi adj[MAXN]; int n, m; void print (int i) { cout << " queue i=" << i << ": "; priority_queue<ll> tmp; while (!q[i].empty()) { ll x = q[i].top(); q[i].pop(); tmp.push(x); cout << " " << x; } while (!tmp.empty()) { ll x = tmp.top(); tmp.pop(); q[i].push(x); //cout << " " << x; } cout << endl; } void dfs (int v) { // cout << " v = " << v << endl; if (n+1 <= v) { REP(2) q[v].push(c[v]); //print(v); return; } for (int u : adj[v]) { dfs(u); if ((int)q[u].size() > (int)q[v].size()) swap(q[u], q[v]); while (!q[u].empty()) { q[v].push(q[u].top()); q[u].pop(); } } int sz = (int)adj[v].size(); REP(sz-1) q[v].pop(); ll fr, to; to = q[v].top(); q[v].pop(); fr = q[v].top(); q[v].pop(); q[v].push(fr+c[v]); q[v].push(to+c[v]); // print(v); } int main() { FAST_IO; cin >> n >> m; FOR(i, 2, n+m) { cin >> p[i] >> c[i]; adj[p[i]].pb(i); } /*FOR(i, 1, n+m) { cout << " i = " << i << " adj: "; for (int x : adj[i]) cout << x << " "; cout << endl; }*/ dfs (1); ll cur = 0; FOR(i, 1, n+m) cur += c[i]; q[1].pop(); while (!q[1].empty()) { cur -= q[1].top(); q[1].pop(); } cout << cur << "\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...