#include <bits/stdc++.h>
#define int long long
#define ll long long
#define pb push_back
#define fi first
#define se second
#define lf (id<<1)
#define rg ((id<<1)|1)
#define md ((l+r)>>1)
#define ld long double
using namespace std;
typedef pair<int,int> pii;
typedef pair<char,char> pcc;
typedef pair<int,pii> ipii;
typedef pair<pii,pii> ipiii;
const int MAXN = 2e5+10;
const int MAXA = 1e7+10;
const int INF = 1e9+10;
const int SQRT = 300;
const int LOG = 20;
const ld EPS = 1e-6;
const int MOD = 1e9+7;
int sum(int a, int b){ return (a+b)%MOD; }
void chsum(int &a, int b){ a = (a+b)%MOD; }
void chsub(int &a, int b){ a = (a+MOD-b)%MOD; }
int mul(int a, int b){ return (a*b)%MOD; }
void chmul(int &a, int b){ a = (a*b)%MOD; }
void chmn(int &a, int b){ a = min(a, b); }
void chmx(int &a, int b){ a = max(a, b); }
int expo(int a, int b){
    if(b==0) return 1;
    int te = expo(a, b/2); te = mul(te,te);
    return (b%2 ? mul(te,a) : te);
}
int n, k, a[MAXN], val[MAXN], ans;
vector <int> adj[MAXN];
void dfs(int nw, int par){
    vector <int> vec;
    for(auto nx : adj[nw]){
        if(nx==par) continue;
        dfs(nx, nw);
        if(val[nx] != 0) vec.pb(val[nx]);
    }
    sort(vec.begin(), vec.end());
    if(vec.empty()) val[nw] = a[nw];
    else if(vec.size()==1){
        if(vec[0]+a[nw] > k){ // baw tutup
            ans++; val[nw] = a[nw];
        } else val[nw] = vec[0]+a[nw];
    } else {
        if(vec[0]+vec[1]+a[nw] <= k){
            val[nw] = vec[0]+vec[1]+a[nw];
            ans += vec.size()-2; return;
        }
        if(vec[0]+a[nw] <= k){ // oh bisa
            ans += vec.size()-1; // semua kecuali 1
            val[nw] = vec[0]+a[nw];
        } else {
            val[nw] = a[nw];
            ans += vec.size();
        }
    }
    // cout << nw << ' ' << ans << ' ' << val[nw] << " val\n";
}
signed main(){
    // ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0);
    cin>>n>>k;
    for(int i=1; i<=n; i++) cin>>a[i];
    for(int i=1; i<=n-1; i++){
        int x,y;cin>>x>>y;
        adj[x].pb(y); adj[y].pb(x);
    }
    dfs(1,-1);
    cout << ans << '\n';
}
| # | 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... |