Submission #1276809

#TimeUsernameProblemLanguageResultExecution timeMemory
1276809HiepVu217Jobs (BOI24_jobs)C++20
100 / 100
185 ms68156 KiB
//Proud of You//
#include <bits/stdc++.h>
#pragma GCC optimization ("O3")
#pragma GCC optimization ("unroll-loops")
using namespace std;
const int N = 3e5 + 17;
int n, a[N];
long long s, ans;
vector <int> adj[N];
struct block
{
    long long req, sum;
    bool operator < (const block &o) const
    {
        if (req == o.req)
        {
            return sum > o.sum;
        }
        return req > o.req;
    }
};
priority_queue <block> pq[N];
inline void combine (int u, block x)
{
    while (!pq[u].empty())
    {
        block y = pq[u].top();
        if (x.sum < 0)
        {
            x = {max (x.req, y.req - x.sum), x.sum + y.sum};
            pq[u].pop();
            continue;
        }
        if (x.req >= y.req)
        {
            x = {x.req, x.sum + y.sum};
            pq[u].pop();
            continue;
        }
        break;
    }
    if (x.sum >= 0)
    {
        pq[u].push(x);
    }
}
inline void dfs (int u)
{
    for (int v: adj[u])
    {
        dfs(v);
        if (pq[v].size() > pq[u].size())
        {
            swap (pq[u], pq[v]);
        }
        while (!pq[v].empty())
        {
            pq[u].push (pq[v].top());
            pq[v].pop();
        }
    }
    combine (u, {max (0, -a[u]), a[u]});
}
int main()
{
    ios_base::sync_with_stdio(0);
    cin.tie(0);
    cout.tie(0);
    cin >> n >> s;
    for (int i = 1, j; i <= n; ++i)
    {
        cin >> a[i] >> j;
        adj[j].push_back(i);
    }
    dfs(0);
    while (!pq[0].empty())
    {
        auto [req, sum] = pq[0].top();
        pq[0].pop();
        if (s + ans >= req)
        {
            ans += sum;
        }
    }
    cout << ans;
}

#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...