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>
#define fi first
#define se second
using namespace std;
const int N=1e5;
int tab[N+10];
vector<int> e[N+10];
long long s[N+10];
int dfs(int x,int p,int k)
{
int ans=0;
s[x]=tab[x];
vector<long long> sons;
for(auto v:e[x])
{
if(v!=p)
{
ans+=dfs(v,x,k);
sons.push_back(s[v]);
s[x]+=s[v];
}
}
sort(sons.begin(),sons.end());
while(s[x]>k)
{
ans++;
s[x]-=sons.back();
sons.pop_back();
}
return ans;
}
int main()
{
ios_base::sync_with_stdio(false);
cin.tie(NULL);
cout.tie(NULL);
int n,k;
cin>>n>>k;
for(int i=1;i<=n;i++)
cin>>tab[i];
for(int i=1;i<n;i++)
{
int a,b;
cin>>a>>b;
e[a].push_back(b);
e[b].push_back(a);
}
cout<<dfs(1,0,k)<<"\n";
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... |