이 제출은 이전 버전의 oj.uz에서 채점하였습니다. 현재는 제출 당시와는 다른 서버에서 채점을 하기 때문에, 다시 제출하면 결과가 달라질 수도 있습니다.
#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define vll vector<ll>
#define all(x) begin(x),end(x)
#define pb push_back
const int N=3e5+10;
ll pro[N],deg[N],par[N],mx[N],mi[N],sm[N];
vll ma[N];
set<vll> vals;
void dfs(int x,int p=-1)
{
mx[x]=pro[x];
vals.insert({mi[x],sm[x],x});
for(auto y:ma[x])
{
sm[y]=sm[x]+pro[y];
mi[y]=min(mi[x],sm[y]);
dfs(y,x);
mx[x]+=mx[y];
}
if(mx[x]<0)
mx[x]=0;
}
ll node=-1;
ll mxp=-1e18;
ll cursm=0;
void dfs1(int x,int p=-1,ll sm=0,ll mi=0)
{
sm+=pro[x];
mi=min(mi,sm);
if((mi+cursm)>=0)
{
if(sm>mxp)
{
mxp=sm;
node=x;
}
}
for(auto y:ma[x])
{
dfs1(y,x,sm,mi);
}
}
void solve()
{
ll n,s;
cin>>n>>s;
pro[0]=0;
for(int i=1;i<=n;i++)
{
cin>>pro[i]>>par[i];
deg[i]++;
ma[par[i]].pb(i);
}
ll mp=1e18;
dfs(0);
par[0]=-1;
if(s==mp)
{
cout<<mx[0]<<endl;
}
else{
ll og=s;
cursm=s;
for(int i=0;i<n;i++)
{
node=-1;
mxp=-1e18;
dfs1(0);
if(mxp>0)
{
cursm+=mxp;
while ((node!=-1))
{
pro[node]=0;
node=par[node];
}
}
else{
break;
}
}
cout<<cursm-og<<endl;
}
}
int main()
{
cin.tie(0);cout.tie(0);
ios::sync_with_stdio(0);
int t=1;
// cin>>t;
while(t--)
solve();
}
# | 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... |