Submission #1331014

#TimeUsernameProblemLanguageResultExecution timeMemory
1331014foxsergJobs (BOI24_jobs)C++20
100 / 100
197 ms64716 KiB
#include <bits/stdc++.h>
// #include <bits/extc++.h>
 
// #pragma GCC optimize("Ofast,unroll-loops,O3")
// #pragma GCC target("avx,avx2,fma")
 
using namespace std;
// using namespace __gnu_pbds;
// using namespace __gnu_cxx;
 
// template <typename T>
// using ordered_set = tree <T, null_type, less <T>, rb_tree_tag, tree_order_statistics_node_update>;
// template <typename T>
// using ordered_multiset = tree <T, null_type, less_equal <T>, rb_tree_tag, tree_order_statistics_node_update>;
 
using ll = long long;
using ld = long double;
using ull = unsigned long long;
using uint = unsigned int;
using i128 = __int128_t;
 
istream& operator>>(istream& is, i128& x) {
    long long a;
    is >> a;
    x = (i128) a;
    return is;
}
ostream& operator<<(ostream& os, i128& x) {
    long long a = (long long) x;
    os << a;
    return os;
}
 
template <typename T>
ostream& operator<<(ostream& is, vector <T>& a) {
    for (uint i = 0; i < a.size(); ++i) is << a[i] << " ";
    return is;
}

#ifdef LOCAL
    # define DEBUG(x) cerr << "(" << __LINE__ << ") " << #x << ":  " << x << endl;
#else
    # define DEBUG(x)
#endif
 
const ll inf = 1e18 + 1e16;
const int inf_t = 1e9 + 7;
const ll mod = 1e9 + 7;
 
#define int long long

const int MAXN = 300001;
vector <int> g[MAXN];
int n;
vector <int> x;

vector <priority_queue <pair <int, int>, vector <pair <int, int>>, greater <pair <int, int>>>> dp;

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

    for (int u : g[v]) {
        if (dp[u].size() > dp[v].size()) swap(dp[v], dp[u]);
    }
    for (int u : g[v]) {
        while (!dp[u].empty()) {
            dp[v].push(dp[u].top());
            dp[u].pop();
        }
    }

    int req = -x[v];
    int s = x[v];

    while (!dp[v].empty() && (s <= 0 || req > dp[v].top().first)) {
        req = max(req, dp[v].top().first - s);
        s += dp[v].top().second;

        dp[v].pop();
    }
    if (s > 0) dp[v].push(make_pair(req, s));
}

signed main() {
    #ifdef LOCAL
freopen("input.txt", "r", stdin);
freopen("output.txt", "w", stdout);
    #endif
    ios_base::sync_with_stdio(false);
    cin.tie(nullptr);
    cout.tie(nullptr);

    cin >> n;
    int s;
    cin >> s;
    x.resize(n + 1);

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

    dp.resize(n + 1);
    dfs(0);

    int ans = 0;
    while (!dp[0].empty()) {
        if (s >= dp[0].top().first) {
            ans += dp[0].top().second;
            s += dp[0].top().second;

            dp[0].pop();
        } else {
            break;
        }
    }

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