This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#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 process() {
cin >> n >> s;
FOR(i, 1, n) {
int p;
cin >> x[i] >> p;
g[p].push_back(i);
}
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 prof = 0;
while(q[0].size()) {
auto top = q[0].top(); q[0].pop();
if(s + prof + top.fi >= 0) prof += top.se;
else break;
}
cout << prof << '\n';
}
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;
}
// https://oj.uz/problem/view/BOI24_jobs
Compilation message (stderr)
Main.cpp: In function 'int main()':
Main.cpp:97:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
97 | freopen("test.inp","r",stdin);
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~
Main.cpp:98:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
98 | freopen("test.out","w",stdout);
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |