이 제출은 이전 버전의 oj.uz에서 채점하였습니다. 현재는 제출 당시와는 다른 서버에서 채점을 하기 때문에, 다시 제출하면 결과가 달라질 수도 있습니다.
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
const int N=3e5+5;
int n;
ll s;
ll a[N];
vector<int> adj[N];
struct DP{
map<ll,ll> mp;
ll lz;
DP():lz(0){}
void insert(ll x,ll d){
x-=lz;
mp[x]+=d;
auto it=mp.find(x);
if(it!=mp.begin()){
auto x=prev(it),y=it;
if(x->first+x->second>=y->first){
it--;
x->second+=y->second;
mp.erase(y);
}
}
for(auto x=it,y=next(it);y!=mp.end()&&x->first+x->second>=y->first;y=mp.erase(y)){
x->second+=y->second;
}
}
int size(){
return mp.size();
}
void pop(ll x){
lz+=x;
auto it=mp.begin();
ll cur=x;
for(;it!=mp.end();it=mp.erase(it)){
it->second-=cur;
if(it->second>0)break;
cur=-it->second;
}
insert(0,0);
}
}dp[N];
void dfs(int u){
for(auto v:adj[u]){
dfs(v);
if(dp[v].size()>dp[u].size())swap(dp[u],dp[v]);
for(auto [x,y]:dp[v].mp)dp[u].insert(x+dp[v].lz,y);
}
if(a[u]>=0)dp[u].insert(0,a[u]);
else dp[u].pop(-a[u]);
}
int main(){
cin.tie(nullptr)->sync_with_stdio(false);
cin >> n >> s;
for(int i=1;i<=n;i++){
int p;
cin >> a[i] >> p;
adj[p].emplace_back(i);
}
dfs(0);
ll ans=0;
for(auto [x,y]:dp[0].mp)if(x+dp[0].lz<=s)ans+=y;
cout << ans;
}
# | 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... |