This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
// #define _GLIBCXX_DEBUG
#include <bits/stdc++.h>
#define ll long long
#define ull unsigned ll
#define pb push_back
#define mp make_pair
#define pll pair<ll, ll>
#define pii pair<int, int>
#define ld long double
#define pas(v) cout << #v << ' ' << v << endl
#define all(v) v.begin(), v.end()
#define fi first
#define se second
#define vi vector<ll>
#define vvi vector<vi>
using namespace std;
int main() {
cout.precision(30);
ios_base::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
int n, k; cin >> n >> k;
vi sp(n);
for (auto &i : sp) cin >> i;
vvi g(n);
for (int i = 0; i < n - 1; i++) {
int a, b; cin >> a >> b;
a--, b--;
g[a].pb(b);
g[b].pb(a);
}
function<pll(int, int)> dfs = [&](int v, int p) {
vi have;
ll sum = 0;
ll ret = 0;
for (auto u : g[v]) {
if (u == p) continue;
auto x = dfs(u, v);
ll s = x.first;
ll num = x.second;
ret += num;
have.pb(s);
sum += s;
}
sum += sp[v];
if (sum <= k) return pll{sum, ret};
sort(all(have));
while (sum > k) {
sum -= have.back();
ret++;
have.pop_back();
}
return pll{sum, ret};
};
auto res = dfs(0, -1);
cout << res.se;
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... |