#include <bits/stdc++.h>
#define ll long long
#define vec vector
#define pb push_back
#define pll array<ll, 2>
#define all(X) X.begin(), X.end()
#define allr(X) X.rbegin(), X.rend()
using namespace std;
const ll inf = 1e18;
ll res = 0, k;
vec<vec<ll>>g;
vec<ll>sp;
void dfs(ll u, ll p){
multiset<ll>st;
for(ll &v : g[u]){
if(v != p){
dfs(v, u);
st.insert(sp[v]);
}
}
if(st.empty()) return ;
ll add = st.size();
for(ll s : st){
//cout << sp[v] << ' ' << sp[u] << " " << add << endl;
if(s + sp[u] <= k) sp[u] += s, add--;
else break;
} res += add;
}
void _(){
ll n; cin >> n >> k;
g.assign(n + 1, {}); sp.resize(n + 1, 0);
for(ll i = 1; i <= n; i++) cin >> sp[i];
for(ll i = 1; i < n; i++){
ll a, b; cin >> a >> b;
g[a].pb(b), g[b].pb(a);
}
dfs(1, 0); cout << res << endl;
}
signed main(){
ios_base::sync_with_stdio(0);
cin.tie(0); cout.tie(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... |