이 제출은 이전 버전의 oj.uz에서 채점하였습니다. 현재는 제출 당시와는 다른 서버에서 채점을 하기 때문에, 다시 제출하면 결과가 달라질 수도 있습니다.
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef priority_queue<pair<ll, ll>> pqii;
const int MAXN = 300005;
vector<int> adj[MAXN];
ll val[MAXN];
pqii dfs(int x){
pqii pq;
for (int nn : adj[x]){
auto join = dfs(nn);
if (pq.size() < join.size()) swap(pq, join);
while (!join.empty()){
pq.push(join.top()); join.pop();
}
}
if (val[x] > 0) pq.push({0, val[x]});
else{
ll mp = val[x], sum = val[x];
while (!pq.empty()){
ll jmp, js; tie(jmp, js) = pq.top(); pq.pop();
mp = min(mp, sum + jmp); sum += js;
if (sum > 0) break;
}
if (sum > 0) pq.push({mp, sum});
}
return pq;
}
int main(){
ios_base::sync_with_stdio(false);
cin.tie(0); cout.tie(0);
int nums; cin >> nums >> val[0];
for (int i = 1; i <= nums; i++){
int par; cin >> val[i] >> par;
adj[par].push_back(i);
}
auto pq = dfs(0);
ll ans = 0;
while (!pq.empty()){
ll mp, sum; tie(mp, sum) = pq.top(); pq.pop();
if (ans + mp >= 0) ans += sum;
else break;
}
cout << ans - val[0];
}
# | 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... |