This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#include<bits/stdc++.h>
using namespace std;
#define pb push_back
int n,m,a[100005],u_i,v_i,sum[100005],cnt,total,ss,ans=0,w;
vector<int> v[100005];
priority_queue<int> pq;
void dfs(int u,int p)
{
for(auto node:v[u])
{
if(node!=p)dfs(node,u);
}
if(v[u].size()==1&&u!=1)
{
sum[u]=a[u];
}else
{
if(u==1)
{
total=v[1].size();
}else
{
total=v[u].size()-1;
}
cnt=a[u];
for(auto node:v[u])
{
if(node!=p)
{
pq.push(-sum[node]);
}
}
ss=0;
while(!pq.empty())
{
w=-pq.top();
cnt+=w;
pq.pop();
if(cnt>m)
{
cnt-=w;
ss=pq.size()+1;
break;
}
}
while(!pq.empty())pq.pop();
sum[u]=cnt;
ans+=ss;
}
}
int main()
{
ios_base::sync_with_stdio(0),cin.tie(0);
cin >> n >> m;
for(int i=1;i<=n;i++)cin >> a[i];
for(int i=1;i<n;i++)
{
cin >> u_i >> v_i;
v[u_i].pb(v_i);
v[v_i].pb(u_i);
}
dfs(1,1);
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... |