제출 #1010719

#제출 시각아이디문제언어결과실행 시간메모리
1010719Beerus13Jobs (BOI24_jobs)C++14
100 / 100
197 ms73340 KiB
#include<bits/stdc++.h>
using namespace std;
#define FOR(i, a, b)  for(int i = (a), _b = (b); i <= _b; ++i)
#define FORD(i, b, a) for(int i = (b), _a = (a); i >= _a; --i)
#define REP(i, a, b)  for(int i = (a), _b = (b); i < _b;  ++i)
#define REPD(i, b, a) for(int i = (b), _a = (a); i > _a;  --i)
#define MASK(i) (1LL << (i))
#define BIT(mask, i) (((mask) >> (i)) & 1)
#define __builtin_popcount __builtin_popcountll
#define all(val) val.begin(), val.end()
#define ll long long
#define ull unsigned long long
#define pii pair<int, int>
#define pll pair<ll, ll>
#define fi first
#define se second

mt19937_64 rng(chrono::high_resolution_clock::now().time_since_epoch().count());
ll Rand(ll l, ll r) {return l + rng() % (r - l + 1);}

template<class X, class Y>
    bool minimize(X &x, const Y &y) {
        if (x > y) {
            x = y;
            return true;
        } else return false;
    }
template<class X, class Y>
    bool maximize(X &x, const Y &y) {
        if (x < y) {
            x = y;
            return true;
        } else return false;
    }
template<class T>
    T Abs(const T &x) {
        return (x < 0 ? -x : x);
    }

const int N = 3e5 + 5;

int n, x[N];
ll s;
vector<int> g[N];
priority_queue<pll> q[N];

void sub1() {
    if(s < 3e14) return;
    function<ll(int)> dfs = [&](int u) {
        ll sum = x[u];
        for(int v : g[u]) {
            sum += dfs(v);
        }
        if(sum < 0) sum = 0;
        return sum;
    };
    cout << dfs(0) << '\n';
    exit(0);
}

void sub5() {
    function<void(int)> dfs = [&](int u) {
        for(int v : g[u]) {
            dfs(v);
            if(q[u].size() < q[v].size()) swap(q[u], q[v]);
            while(q[v].size()) {
                auto top = q[v].top(); q[v].pop();
                q[u].push(top);
            }
        }
        if(u == 0) return;
        if(x[u] > 0) q[u].emplace(0, x[u]);
        else {
            ll mn = x[u], prof = x[u]; // <= 0
            while(q[u].size()) {
                auto top = q[u].top();
                if(prof <= 0) {
                    minimize(mn, prof + top.fi);
                    prof += top.se;
                }
                else {
                    if(top.fi < mn) break;
                    prof += top.se;
                }
                q[u].pop();
            }
            if(prof > 0) q[u].emplace(mn, prof);
            else {
                while(q[u].size()) q[u].pop();
            }
        }
    };
    dfs(0);
    ll sum = s;
    while(q[0].size()) {
        auto top = q[0].top(); q[0].pop();
        if(sum + top.fi >= 0) sum += top.se;
        else break;
    }
    cout << sum - s << '\n';
    exit(0);
}

void process() {
    cin >> n >> s;
    FOR(i, 1, n) {
        int p;
        cin >> x[i] >> p;
        g[p].push_back(i);
    }
    sub1();
    sub5();
}

signed main() {
    if(fopen("test.inp","r")) {
        freopen("test.inp","r",stdin);
        freopen("test.out","w",stdout);
    }
    ios::sync_with_stdio(0);
    cin.tie(0); cout.tie(0);
    int ntests = 1;
    // cin >> ntests;
    while(ntests--) process();
    return 0;
}

컴파일 시 표준 에러 (stderr) 메시지

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