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 ll long long
#define N 100010
#define fs first
#define sc second
#define ii pair<int,int>
#define iii pair<ii,int>
#define ld long double
#define pb push_back
#define int ll
int dp[N],n,k,a[N],ans=0;
vector<int>g[N],s[N];
void DFS(int u,int p)
{
int sum=a[u];
for(auto v:g[u])
if(v!=p)
{
DFS(v,u);
s[u].pb(dp[v]);
sum+=dp[v];
}
sort(s[u].begin(),s[u].end());
while(sum>k)
{
sum-=s[u].back();
s[u].pop_back();
ans++;
}
dp[u]=sum;
}
int32_t main()
{
// freopen("A.inp","r",stdin);
// freopen("A.out","w",stdout);
cin.tie(NULL);
cout.tie(NULL);
ios::sync_with_stdio(0);
cin>>n>>k;
for(int i=1;i<=n;i++)
cin>>a[i];
for(int i=1;i<n;i++)
{
int u,v;
cin>>u>>v;
g[u].pb(v);
g[v].pb(u);
}
DFS(1,0);
cout<<ans;
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... |