제출 #1032794

#제출 시각아이디문제언어결과실행 시간메모리
1032794AbitoJobs (BOI24_jobs)C++17
29 / 100
144 ms45492 KiB
#include <bits/stdc++.h>
#define F first
#define S second
#define pb push_back
#define ppb pop_back
#define ep insert
#define endl '\n'
#define elif else if
#define pow pwr
#define sqrt sqrtt
#define int long long
#define ll long long
#define y1 YONE
typedef unsigned long long ull;
using namespace std;
const int N=3e5+5;
int n,a[N],p[N],par[N];
vector<int> adj[N];
vector<vector<int>> v;
void getp(int x){
    p[x]+=a[x];
    for (auto u:adj[x]){
        p[u]+=p[x];
        getp(u);
    }return;
}
void dfs(int x,int mn,int s){
    if (p[x]>p[s]){
        v.pb({mn-p[s],p[x]-p[s],x});
        return;
    }
    mn=min(mn,p[x]);
    for (auto u:adj[x]) dfs(u,mn,s);
    return;
}
int32_t main(){
    ios_base::sync_with_stdio(false);cin.tie(NULL);cout.tie(NULL);
    cin>>n>>a[0];
    for (int i=1;i<=n;i++){
        cin>>a[i]>>par[i];
        adj[par[i]].pb(i);
    }
    getp(0);
    int ans=0;
    set<vector<int>> pq;
    pq.ep({0,a[0],0});
    while (!pq.empty()){
        auto it=pq.lower_bound({-ans,-1,-1});
        if (it==pq.end()) break;

        //cout<<pq.top()[0]<<' '<<pq.top()[1]<<' '<<pq.top()[2]<<endl;
        ans+=(*it)[1];
        int x=(*it)[2];
        pq.erase(it);
        dfs(x,p[x],x);
        for (auto u:v){
            pq.ep(u);
        }
        v.clear();
    }
    cout<<ans-a[0]<<endl;
    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...
#Verdict Execution timeMemoryGrader output
Fetching results...