#include <bits/stdc++.h>
using namespace std;
/*
---===ASCII help===---
'0' -> 48 '9' -> 57
'A' -> 65 'Z' -> 90
'a' -> 97 'z' -> 122
*/
const long long mod = 1e9 + 7;
const int MAXN = 100005;
int n, k;
int a[MAXN];
vector<int> adj[MAXN];
int dp[MAXN], f[MAXN];
void calc(int u, int parent) {
f[u] = a[u];
vector<pair<int,int>> children;
for (int v : adj[u]) {
if (v != parent) {
calc(v, u);
children.push_back({f[v], v});
}
}
sort(children.begin(), children.end());
for (auto &child : children) {
int v = child.second;
int sum = child.first;
dp[u] += dp[v];
if (f[u] + sum <= k) f[u] += sum;
else dp[u]++;
}
}
void solve() {
cin >> n >> k;
for (int i = 1; i <= n; i++) cin >> a[i];
for (int i = 1; i <= n - 1; i++) {
int u, v;
cin >> u >> v;
adj[u].push_back(v);
adj[v].push_back(u);
}
calc(1, -1);
cout << dp[1] << "\n";
}
int main() {
int t = 1;
//cin >> t;
while (t--) {
solve();
}
}
| # | 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... |