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 ;
const int MAX = 1e5 + 10 ;
int arr[MAX] ;
int n , k ;
vector< vector<int> >adj(MAX) ;
int ans = 0 ;
int dfs(int node , int par)
{
long long sum = arr[node] ;
vector<int>v ;
for(auto &child : adj[node])
{
if(child == par)
continue ;
int x = dfs(child , node) ;
sum += x , v.push_back(x) ;
}
sort(v.begin() , v.end()) ;
while(sum > k)
ans++ , sum -= v.back() , v.pop_back() ;
return sum ;
}
int main()
{
ios_base::sync_with_stdio(0) ;
cin.tie(0) ;
cin>>n>>k ;
for(int i = 1 ; i <= n ; ++i)
cin>>arr[i] ;
for(int i = 0 ; i < n-1 ; ++i)
{
int x , y ;
cin>>x>>y ;
adj[x].push_back(y) ;
adj[y].push_back(x) ;
}
dfs(1 , -1) ;
return cout<<ans<<"\n" , 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... |