Submission #1362631

#TimeUsernameProblemLanguageResultExecution timeMemory
1362631cpdreamerJobs (BOI24_jobs)C++20
14 / 100
1 ms620 KiB
#include<bits/stdc++.h>
using namespace std;
void file() {
    freopen("input.txt","r",stdin);
    freopen("output.txt","w",stdout);
}
#define all(v) v.begin(),v.end()
#define pb push_back
#define V vector
#define P pair
#define F first
#define S second
typedef long long ll;
const ll MOD=998244353;
V<int>adj[2001];
bool b[2001];
ll v[2001];
int par[2001];
P<ll,int>f(int n,ll x) {
    x+=v[n];
    if (x>=0) {
        return {x,n};
    }
    if (adj[n].empty()) {
        return {x,-1};
    }
    ll maxd=LLONG_MIN;
    int c=-1;
    for (auto u:adj[n]) {
        P<ll,int>p=f(u,x);
        if (p.S!=-1) {
            if (p.F>maxd) {
                maxd=p.F;
                c=p.S;
            }
        }
    }
    if (c==-1) {
        return {x,-1};
    }
    return {min(maxd,x),c};
}
void solve() {
    int n;
    cin>>n;
    for (int i=1;i<=n;i++) {
        par[i]=-1;
        b[i]=true;
    }
    ll s;
    cin>>s;
    ll in=s;
    for (int i=1;i<=n;i++) {
        cin>>v[i];
        int p;
        cin>>p;
        if (p!=0) {
            adj[p].pb(i);
            par[i]=p;
        }
    }
    set<P<ll,P<int,int>>,greater<>>st;
    for (int i=1;i<=n;i++) {
        if (par[i]==-1) {
            P<ll,int>p=f(i,0);
            if (p.S!=-1) {
                st.insert({p.F,{p.S,i}});
            }
        }
    }
    while (!st.empty()) {
        P<ll,P<int,int>>p=*st.begin();
        st.erase(st.begin());
        if (s+p.F<0) {
            break;
        }
        int nd1=p.S.F;
        int nd=p.S.F;
        int r=p.S.S;
        ll y=0;
        while (nd!=r) {
            y+=v[nd];
            if (par[nd]==r) {
                b[nd]=false;
            }
            nd=par[nd];
        }
        y+=v[r];
        if (s<(ll)1e15) {
            s+=y;
        }
        for (auto u:adj[r]) {
            if (b[u]) {
                P<ll,int>o=f(u,0);
                if (o.S!=-1) {
                    st.insert({o.F,{o.S,u}});
                }
            }
        }
        if (nd1!=r) {
            for (auto u:adj[nd1]) {
                P<ll,int>o=f(u,0);
                if (o.S!=-1) {
                    st.insert({o.F,{o.S,u}});
                }
            }
        }
    }
    cout<<s-in<<endl;
}
int main() {
    //file();
    solve();
}

Compilation message (stderr)

Main.cpp: In function 'void file()':
Main.cpp:4:12: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
    4 |     freopen("input.txt","r",stdin);
      |     ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~
Main.cpp:5:12: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
    5 |     freopen("output.txt","w",stdout);
      |     ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~
#Result Execution timeMemoryGrader output
Fetching results...
#Result Execution timeMemoryGrader output
Fetching results...
#Result Execution timeMemoryGrader output
Fetching results...
#Result Execution timeMemoryGrader output
Fetching results...
#Result Execution timeMemoryGrader output
Fetching results...