이 제출은 이전 버전의 oj.uz에서 채점하였습니다. 현재는 제출 당시와는 다른 서버에서 채점을 하기 때문에, 다시 제출하면 결과가 달라질 수도 있습니다.
#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 = 2e5 + 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... |