Submission #943851

#TimeUsernameProblemLanguageResultExecution timeMemory
943851kimFireworks (APIO16_fireworks)C++17
7 / 100
14 ms32344 KiB
#include<bits/stdc++.h> using namespace std; #define endl '\n' using ll=long long; struct A{ multiset<ll> L,R; ll mn; A():mn(0){} void insert(ll x1,ll x2,ll c){ mn+=c; if(L.empty()) L.insert(x1), R.insert(x2); else if(x2<*L.rbegin()){ mn+=*L.rbegin()-x2; L.emplace(x1), L.emplace(x2); R.emplace(*L.rbegin()), L.erase(prev(L.end())); } else if(x1>*R.begin()){ mn+=x1-*R.begin(); R.emplace(x1), R.emplace(x2); L.emplace(*R.begin()), R.erase(R.begin()); } else L.emplace(x1), R.emplace(x2); } }t[300005]; int p[300005]; ll c[300005]; int main(){ ios::sync_with_stdio(false); cin.tie(0); int n,m; cin>>n>>m; for(int i=2;i<=n+m;++i) cin>>p[i]>>c[i]; for(int i=n+m;i>n;--i) t[p[i]].insert(c[i],c[i],0); for(int i=n;i>1;--i){ ll x1=*t[i].L.rbegin(), x2=*t[i].R.begin(); t[p[i]].insert(x1+c[i],x2+c[i],t[i].mn); } cout<<t[1].mn; }
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...