Submission #1063936

#TimeUsernameProblemLanguageResultExecution timeMemory
1063936Boycl07Jobs (BOI24_jobs)C++17
11 / 100
137 ms42408 KiB
#include <bits/stdc++.h>
using namespace std;

typedef long long ll;

#define rep(i, n) for(int i = 1; (i) <= (n); ++i)
#define forn(i, l, r) for(int i = (l); i <= (r); ++i)
#define ford(i, r, l) for(int i = (r); i >= (l); --i)
#define FOR(i, n) for(int i = 0; i < (n); ++i)
#define FORD(i, n) for(int i = ((n) - 1); i >= 0; --i)
#define fi first
#define se second
#define pii pair<int, int>
#define pll pair<ll, ll>
#define pb push_back
#define endl "\n"
#define task "FFILL"
#define sz(a) int(a.size())
#define C(x, y) make_pair(x, y)
#define all(a) (a).begin(), (a).end()
#define bit(i, mask) (mask >> i & 1)

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


const int N = 3e5 + 3;
const int LIM = (1 << 16) + 3;
const int INF = 1e9 + 1;

int n;
ll k;

int val[N], par[N];

priority_queue<pll, vector<pll>, greater<pll>> pq[N];

void solve()
{
    cin >> n >> k;
    rep(i, n) cin >> val[i] >> par[i];

    ford(i, n, 0)
    {
        ll p = max(0, -val[i]), v = val[i];

        while(!pq[i].empty() && v <= 0)
        {
            pll tmp = pq[i].top(); pq[i].pop();

            v += tmp.se;
            p = max(p, tmp.fi - v);
        }
        while(!pq[i].empty())
        {
            pll tmp = pq[i].top();
            if(tmp.fi > p + v) break;
            pq[i].pop();
            v += tmp.se;
        }



        if(v > 0) pq[i].push({p, v});
        int u = par[i];
        if(i != 0)
        {
            if(sz(pq[u]) < sz(pq[i])) swap(pq[u], pq[i]);
            while(!pq[i].empty())
            {
                pq[u].push(pq[i].top());
                pq[i].pop();
            }
        }
    }
    ll res = k;
    while(!pq[0].empty() && pq[0].top().fi <= res) res += pq[0].top().se, pq[0].pop();
    cout << res - k;
}

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

    int TC = 1;

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


    while(TC--)
    {
        solve();
    }

    return 0;
}

Compilation message (stderr)

Main.cpp: In function 'int main()':
Main.cpp:90:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
   90 |         freopen("note.inp", "r", stdin);
      |         ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~
Main.cpp:91:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
   91 |         freopen("note.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...