This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
// In the name of God
#include<bits/stdc++.h>
using namespace std;
#define pb push_back
#define fi first
#define se second
#define mp make_pair
typedef long long ll;
const int N = 2e5 + 5;
int n, k, h[N];
int ans, dp[N];
vector<int> adj[N];
void dfs(int v, int p) {
dp[v] = h[v];
vector<int> vec;
for (auto u : adj[v]) {
if (u != p) {
dfs(u, v);
vec.pb(dp[u]);
}
}
sort(vec.begin(), vec.end());
ans += (int)vec.size();
for (int i = 0; i < (int)vec.size() && vec[i] + dp[v] <= k; dp[v] += vec[i], i++, ans--);
}
void solve() {
cin >> n >> k;
for (int i = 1; i <= n; i++)
cin >> h[i];
for (int i = 0; i < n - 1; i++) {
int v, u; cin >> v >> u;
adj[v].pb(u), adj[u].pb(v);
}
dfs(1, 0);
cout << ans << endl;
}
int32_t main() {
ios::sync_with_stdio(0), cin.tie(0), cout.tie(0);
int tc = 1; // cin >> tc;
while (tc--) {
solve();
}
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... |