This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#pragma GCC optimize("O3")
#include<bits/stdc++.h>
#define ll long long
#define endl "\n"
#define AI ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0);
using namespace std;
vector<ll>val;
vector<vector<ll>>edge;
vector<ll>check;
ll k,c=0;
ll dfs(ll i)
{
check[i]=1;
priority_queue<ll>pq;
ll res=val[i],x;
for(ll j:edge[i])
{
if(!check[j])
{
x=dfs(j);
pq.push(x);
res+=x;
}
}
// cout<<res<<' ';
while(res>k and pq.size())
{
res-=pq.top();
c++;
pq.pop();
}
return res;
}
int main()
{
AI
//freopen("input.txt", "r", stdin);
//freopen("output.txt", "w", stdout);
ll n,a,b,i;
cin>>n>>k;
val.resize(n+1);
for(i=1;i<=n;i++)
cin>>val[i];
edge.resize(n+1);
check.resize(n+1,0);
for(i=1;i<n;i++)
{
cin>>a>>b;
edge[a].push_back(b);
edge[b].push_back(a);
}
dfs(1);
cout<<c<<endl;
}
# | 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... |