제출 #1166887

#제출 시각아이디문제언어결과실행 시간메모리
1166887mychecksedadFireworks (APIO16_fireworks)C++20
100 / 100
142 ms125512 KiB
/* Author : Mychecksdead */ #include<bits/stdc++.h> using namespace std; #define ll long long int #define MOD (1000000000+7) #define MOD1 (998244353) #define pb push_back #define all(x) x.begin(), x.end() #define en cout << '\n' #define ff first #define ss second #define pii pair<int,int> #define vi vector<int> const int N = 1e6+100, M = 1e5+10, K = 52, MX = 30; const ll INF = 1e13; int n, m; ll a[N], b[N]; vector<pair<int, ll>> g[N]; priority_queue<ll> pq[N]; void merge(int a, int b){ if(pq[a].size() > pq[b].size()){ while(pq[b].size()){ pq[a].push(pq[b].top()); pq[b].pop(); } }else{ swap(a, b); while(pq[b].size()){ pq[a].push(pq[b].top()); pq[b].pop(); } pq[a].swap(pq[b]); } } void dfs(int v){ if(v > n) return; for(auto [u, l]: g[v]){ dfs(u); if(u <= n){ ll L = pq[u].top(); pq[u].pop(); ll R = pq[u].top(); pq[u].pop(); L += l; R += l; b[u] -= l; pq[u].push(L); pq[u].push(R); } a[v] += a[u]; b[v] += b[u]; merge(v, u); } while(a[v] > 1){ b[v] += pq[v].top(); pq[v].pop(); a[v]--; } } void solve(){ cin >> n >> m; for(int i = 2; i <= n + m; ++i){ int p, c; cin >> p >> c; g[p].pb({i, c}); if(i <= n) a[i] = 0, b[i] = 0; else a[i] = 1, b[i] = -c, pq[i].push(c), pq[i].push(c); } dfs(1); cout << pq[1].top() * a[1] + b[1]; } int main(){ cin.tie(0); ios::sync_with_stdio(0); int tt = 1, aa; // freopen("in.txt", "r", stdin); // freopen("out.txt", "w", stdout); while(tt--){ solve(); } cerr<<"time taken : "<<(float)clock()/CLOCKS_PER_SEC<<" seconds\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...