// ~~ icebear ~~
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef pair<int, int> ii;
typedef pair<int, ii> iii;
template<class T>
bool minimize(T &a, const T &b) {
if (a > b) return a = b, true;
return false;
}
template<class T>
bool maximize(T &a, const T &b) {
if (a < b) return a = b, true;
return false;
}
#define FOR(i,a,b) for(int i=(a); i<=(b); ++i)
#define FORR(i,a,b) for(int i=(a); i>=(b); --i)
#define REP(i, n) for(int i=0; i<(n); ++i)
#define RED(i, n) for(int i=(n)-1; i>=0; --i)
#define MASK(i) (1LL << (i))
#define BIT(S, i) (((S) >> (i)) & 1)
#define mp make_pair
#define pb push_back
#define fi first
#define se second
#define all(x) x.begin(), x.end()
#define task "icebear"
/*END OF TEMPLATE. ICEBEAR AND THE CAT WILL WIN VOI26 */
const int MOD = 1e9 + 7;
const int inf = 1e9 + 27092008;
const ll INF = 1e18 + 27092008;
const int N = 2e5 + 5;
int numNode, profit[N];
ll baseMoney;
vector<int> G[N];
struct Info {
ll base, gain;
bool operator < (const Info &other) const {
if (base == other.base) return gain < other.gain;
return base > other.base;
}
};
priority_queue<Info> info[N];
void combine(int u, int v) {
if (info[u].size() > info[v].size()) swap(info[u], info[v]);
while(!info[v].empty()) {
info[u].push(info[v].top());
info[v].pop();
}
}
void create_profit(priority_queue<Info> &info, Info cur) {
while(!info.empty()) {
ll base = info.top().base, gain = info.top().gain;
if (cur.gain <= 0) {
minimize(cur.base, base + cur.gain);
cur.gain += gain;
info.pop();
} else {
if (cur.base >= base) {
cur.gain += gain;
info.pop();
} else {
info.push(cur);
break;
}
}
}
if (info.empty()) info.push(cur);
if (!info.empty() && info.top().gain < 0) info.pop();
}
void dfs(int u) {
for(int v : G[u]) {
dfs(v);
combine(u, v);
}
create_profit(info[u], {max(0,-profit[u]), profit[u]});
}
void init(void) {
cin >> numNode >> baseMoney;
FOR(i, 1, numNode) {
int par; cin >> profit[i] >> par;
G[par].pb(i);
}
}
void process(void) {
dfs(0);
ll ans = 0;
while(!info[0].empty()) {
ll base = info[0].top().base, gain = info[0].top().gain;
info[0].pop();
if (baseMoney + ans >= base) ans += gain;
}
cout << ans;
}
int main() {
ios_base::sync_with_stdio(0);
cin.tie(0); cout.tie(0);
if (fopen(task".inp", "r")) {
freopen(task".inp", "r", stdin);
freopen(task".out", "w", stdout);
}
int tc = 1;
// cin >> tc;
while(tc--) {
init();
process();
}
return 0;
}
Compilation message (stderr)
Main.cpp: In function 'int main()':
Main.cpp:111:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
111 | freopen(task".inp", "r", stdin);
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~
Main.cpp:112:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
112 | freopen(task".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... |