Submission #1098810

#TimeUsernameProblemLanguageResultExecution timeMemory
1098810Zero_OPJobs (BOI24_jobs)C++14
100 / 100
216 ms67020 KiB
#include<bits/stdc++.h>

using namespace std;

#define sz(v) (int)v.size()
#define all(v) begin(v), end(v)
#define compact(v) v.erase(unique(all(v)), end(v))
#define dbg(v) "[" #v " = " << (v) << "]"
#define file(name) if(fopen(name".inp", "r")) {freopen(name".inp", "r", stdin); freopen(name".out", "w", stdout); }
#define rep(i, l, r) for(int i = (l); i < (r); ++i)

using ll = long long;
using vi = vector<int>;
using vl = vector<long long>;
using ld = long double;

template<typename T>
    bool minimize(T& a, const T& b){
        if(a > b){
            return a = b, true;
        }  return false;
    }

template<typename T>
    bool maximize(T& a, const T& b){
        if(a < b){
            return a = b, true;
        } return false;
    }

template<int dimension, typename T>
struct tensor : public vector<tensor<dimension - 1, T>> {
    static_assert(dimension > 0, "Dimension must be positive !\n");
    template<typename... Args>
    tensor(int n = 0, Args... args) : vector<tensor<dimension - 1, T>> (n, tensor<dimension - 1, T>(args...)) {}
};

template<typename T>
struct tensor<1, T> : public vector<T> {
    tensor(int n = 0, T val = T()) : vector<T>(n, val) {}
};

const int MAX = 3e5 + 5;

int n, x[MAX], p[MAX];
ll s;
vector<int> adj[MAX];
priority_queue<pair<ll, ll>, vector<pair<ll, ll>>, greater<pair<ll, ll>>> pq[MAX];

void insert(auto& pq, pair<ll, ll> nw){
    while(!pq.empty()){
        pair<ll, ll> tp = pq.top();
        if(nw.second <= 0){
            pq.pop();
            nw = {max(nw.first, tp.first - nw.second), nw.second + tp.second};
        } else{
            if(nw.first > tp.first){
                pq.pop();
                nw = {nw.first, nw.second + tp.second};
            } else{
                pq.push(nw);
                break;
            }
        }
    }

    if(pq.empty()) pq.push(nw);
    while(!pq.empty() && pq.top().second <= 0) pq.pop();
}

void dfs(int u){
    for(int v : adj[u]){
        dfs(v);

        if(sz(pq[u]) < sz(pq[v])){
            swap(pq[u], pq[v]);
        }

        while(!pq[v].empty()){
            pq[u].push(pq[v].top());
            pq[v].pop();
        }
    }

    insert(pq[u], {max(0ll, -1ll * x[u]), x[u]});
}

int main(){
    ios_base::sync_with_stdio(0);
    cin.tie(0);

    if(fopen("task.inp", "r")){
        freopen("task.inp", "r", stdin);
        freopen("task.out", "w", stdout);
    }

    cin >> n >> s;
    for(int i = 1; i <= n; ++i){
        cin >> x[i] >> p[i];
        adj[p[i]].push_back(i);
    }

    dfs(0);
    ll profit = 0;
    while(!pq[0].empty() && s + profit >= pq[0].top().first){
        profit += pq[0].top().second; pq[0].pop();
    }

    cout << profit << '\n';

    return 0;
}

Compilation message (stderr)

Main.cpp:50:13: warning: use of 'auto' in parameter declaration only available with '-fconcepts-ts'
   50 | void insert(auto& pq, pair<ll, ll> nw){
      |             ^~~~
Main.cpp: In function 'int main()':
Main.cpp:93:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
   93 |         freopen("task.inp", "r", stdin);
      |         ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~
Main.cpp:94:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
   94 |         freopen("task.out", "w", stdout);
      |         ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~
#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...