This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#include <bits/stdc++.h>
using namespace std;
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
using namespace __gnu_pbds;
#include <ext/rope>
using namespace __gnu_cxx;
typedef tree<long long, null_type, less<long long>,
rb_tree_tag, tree_order_statistics_node_update> pbds;
//less_equal for identical elements
#define DEBUG
#ifdef DEBUG
#define debug(...) printf(__VA_ARGS__);
#else
#define debug(...)
#endif
#define sf scanf
#define pf printf
#define fi first
#define se second
#define pb emplace_back
#define sz(x) (int)x.size()
#define mnto(x,y) x=min(x,(__typeof__(x))y)
#define mxto(x,y) x=max(x,(__typeof__(x))y)
#define INF 1023456789
#define LINF 1023456789123456789
#define all(x) x.begin(), x.end()
typedef long long ll;
typedef long double ld;
typedef pair<int, int> ii;
typedef pair<ll, ll> pll;
typedef tuple<int, int, int> iii;
typedef tuple<int, int, int, int> iiii;
typedef vector<int> vi;
typedef vector<ii> vii;
typedef vector<pll> vll;
mt19937 rng(time(0));
int n,m,p[300005],c[300005];
ll a[300005],b[300005];
priority_queue<ll> pq[300005];
void join(int x,int y){ //join x to y
a[x]+=a[y];
b[x]+=b[y];
if(sz(pq[x])<sz(pq[y]))swap(pq[x],pq[y]);
while(!pq[y].empty()){
pq[x].push(pq[y].top());
pq[y].pop();
}
}
void print(int x){
pf("%d: %lld %lld\n",x,a[x],b[x]);
while(!pq[x].empty()){
pf("%lld ",pq[x].top());
pq[0].push(pq[x].top());
pq[x].pop();
}
pf("\n");
while(!pq[0].empty()){
pq[x].push(pq[0].top());
pq[0].pop();
}
}
int main(){
sf("%d%d",&n,&m);
for(int i=2;i<=n+m;++i){
sf("%d%d",&p[i],&c[i]);
}
for(int i=n+m;i>n;--i){ //leaf nodes
pq[i].push(c[i]);
pq[i].push(c[i]);
a[i]=1;b[i]=-c[i];
//print(i);
join(p[i],i);
}
for(int i=n;i>=2;--i){
while(a[i]>1){
--a[i];
b[i]+=pq[i].top();
pq[i].pop();
}
ll p0=pq[i].top();
pq[i].pop();
ll p1=pq[i].top();
pq[i].pop();
pq[i].push(p1+c[i]);
pq[i].push(p0+c[i]);
b[i]-=c[i];
//print(i);
join(p[i],i);
}
while(a[1]){
--a[1];
b[1]+=pq[1].top();
pq[1].pop();
}
pf("%lld\n",b[1]);
}
/*
4 6
1 5
2 5
2 8
3 3
3 2
3 3
2 9
4 4
4 3
*/
Compilation message (stderr)
fireworks.cpp: In function 'int main()':
fireworks.cpp:71:4: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
71 | sf("%d%d",&n,&m);
| ^
fireworks.cpp:73:5: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
73 | sf("%d%d",&p[i],&c[i]);
| ^
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |