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;
typedef long long ll;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
#define ent "\n"
const int maxn = 3e5 + 100;
const ll INF = (ll)1e18 + 100;
const int inf = 1e9 + 100;
const int MOD = 1e9 + 7;
const int maxl = 350;
const int P = 31;
ll sum, start;
int n;
vector<int> g[maxn];
int p[maxn];
int a[maxn];
ll mn[maxn];
ll f(int v, ll sum = 0, ll mn = 0){
sum += a[v];
mn = min(mn, sum);
ll x = INF;
if(sum > 0) x = -mn;
for(int to: g[v]){
x = min(x, f(to, sum, mn));
}
return x;
}
void test(){
cin >> n >> start; sum = start;
for(int i = 1; i <= n; i++){
cin >> a[i] >> p[i];
g[p[i]].push_back(i);
}
for(int i = 1; i <= n; i++){
mn[i] = f(i);
}
set<pll> s;
for(int to: g[0]){
s.insert({mn[to], to});
}
while(s.size()){
int v = 0;
for(auto it = s.begin(); it != s.end() && it->first <= sum; it++){
int to = it->second;
if(!v || a[v] < a[to]) v = to;
}
if(!v) break;
s.erase({mn[v], v});
sum += a[v];
for(int to: g[v]){
s.insert({mn[to], to});
}
}
cout << sum - start;
}
int main(){
ios_base::sync_with_stdio(0);
cin.tie(0); cout.tie(0);
int t; t = 1;
while(t--) test();
cout << ent;
}
# | 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... |