Submission #487487

# Submission time Handle Problem Language Result Execution time Memory
487487 2021-11-15T16:59:24 Z leaked Fireworks (APIO16_fireworks) C++14
0 / 100
2 ms 4944 KB
#include <bits/stdc++.h>

#define f first
#define s second
#define m_p make_pair
#define pb push_back
#define vec vector
#define sz(x) (int)(x).size()
#define all(x) (x).begin(),(x).end()
#define rall(x) (x).rbegin(),(x).rend()
#define pw(x) (1LL<<(x))
#define fast_iati ios_base::sync_with_stdio(false);cin.tie(0);cout.tie(0);

using namespace std;

template<class T> bool umin(T &a,const T &b){return (a>b?a=b,1:0);}
template<class T> bool umax(T &a,const T &b){return (a<b?a=b,1:0);}
typedef long long ll;
typedef pair<int,int> pii;
typedef pair<ll,ll> pll;
const int N=2e5+1;
const ll inf=1e18;
vec<pii> g[N];
ll h[N];
pll vl[N];
ll k[N],b[N];
ll ans=0;
void dfs(int v){
    if(!sz(g[v])){
        vl[v]={h[v],h[v]};
        k[v]=1,b[v]=-h[v];
        cout<<"D "<<v<<' '<<h[v]<<endl;
    }
    else{
        vec<int> st;
        ll l=-inf,r=inf;
        for(auto &z : g[v]){
            h[z.f]=h[v]+z.s;
            dfs(z.f);
            umax(l,vl[z.f].f-z.s);
//            umin(r,vl[z.f].s+z.s);
            k[v]+=k[z.f];b[v]+=b[z.f];
            st.pb(vl[z.f].f);st.pb(vl[z.f].s);
        }
        ll mn=inf;
        sort(rall(st));
        vec<ll>go;
        ll _k=k[v],_b=b[v];
        for(auto &z : st){
            _k--;_b+=z;
            umin(mn,_k*z+_b);
            if(_k*z+_b==mn) go.pb(z);
        }
        reverse(all(go));
        vl[v]={max(l,go[0]),min(r,go.back())};
        ans+=mn;b[v]=-vl[v].s;k[v]=1;
//        cout<<"DEBUG "<<v<<' '<<mn<<' '<<vl[v].f<<' '<<vl[v].s<<' '<<b[v]<<endl;
    }
}
signed main(){
    fast_iati;
    int n,m;
    cin>>n>>m;
    for(int i=1;i<n+m;i++){
        int v,x;
        cin>>v>>x;--v;
        g[v].pb({i,x});
    }
    for(int i=0;i<n;i++) assert(sz(g[i]));
    for(int i=n;i<n+m;i++) assert(!sz(g[i]));
    dfs(0);
    cout<<ans;
    return 0;
}
# Verdict Execution time Memory Grader output
1 Incorrect 2 ms 4944 KB Output isn't correct
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Incorrect 2 ms 4944 KB Output isn't correct
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Incorrect 2 ms 4944 KB Output isn't correct
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Incorrect 2 ms 4944 KB Output isn't correct
2 Halted 0 ms 0 KB -