이 제출은 이전 버전의 oj.uz에서 채점하였습니다. 현재는 제출 당시와는 다른 서버에서 채점을 하기 때문에, 다시 제출하면 결과가 달라질 수도 있습니다.
#include <bits/stdc++.h>
#define F first
#define S second
#define pb push_back
#define ppb pop_back
#define ep insert
#define endl '\n'
#define elif else if
#define pow pwr
#define sqrt sqrtt
#define int long long
#define ll long long
#define y1 YONE
typedef unsigned long long ull;
using namespace std;
const int N=3e5+5;
int n,a[N],p[N],par[N];
vector<int> adj[N];
vector<vector<int>> v;
void getp(int x){
p[x]+=a[x];
for (auto u:adj[x]){
p[u]+=p[x];
getp(u);
}return;
}
void dfs(int x,int mn,int s){
if (p[x]>p[s]){
v.pb({mn-p[s],p[x]-p[s],x});
return;
}
mn=min(mn,p[x]);
for (auto u:adj[x]) dfs(u,mn,s);
return;
}
int32_t main(){
ios_base::sync_with_stdio(false);cin.tie(NULL);cout.tie(NULL);
cin>>n>>a[0];
for (int i=1;i<=n;i++){
cin>>a[i]>>par[i];
adj[par[i]].pb(i);
}
getp(0);
int ans=0;
priority_queue<vector<int>> pq;
pq.push({0,a[0],0});
while (!pq.empty()){
if (ans+pq.top()[0]<0) break;
//cout<<pq.top()[0]<<' '<<pq.top()[1]<<' '<<pq.top()[2]<<endl;
ans+=pq.top()[1];
int x=pq.top()[2];
pq.pop();
dfs(x,p[x],x);
for (auto u:v){
pq.push(u);
}
v.clear();
}
cout<<ans-a[0]<<endl;
return 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... |