#include "bits/stdc++.h"
using namespace std;
typedef long long ll;
typedef pair<ll, ll> pii;
#define all(x) x.begin(), x.end()
#define sz(x) ((int) ((x).size()))
#define pb push_back
#define F first
#define S second
#define FIO ios_base::sync_with_stdio(false); cin.tie(0)
const int N = 2e5 + 10;
int n, k;
int a[N];
int par[N];
vector<int> adj[N];
int ans;
int dfs(int node) {
multiset<int> s;
for (int x : adj[node]) {
if (par[node] == x) continue;
par[x] = node;
s.insert(dfs(x));
}
int cur = a[node];
while (sz(s) and cur + *s.begin() <= k) {
cur += *s.begin();
s.erase(s.begin());
}
ans += sz(s);
return cur;
}
int main() {
FIO;
cin >> n >> k;
for (int i = 1; i <= n; i++) {
cin >> a[i];
}
for (int i = 0; i < n - 1; i++) {
int x, y;
cin >> x >> y;
adj[x].pb(y);
adj[y].pb(x);
}
dfs(1);
cout << ans << endl;
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... |