This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#include <algorithm>
#include <iostream>
#include <vector>
using namespace std;
const int N = 1e5 + 5;
int n, k, ans;
int Arr[N];
vector <int> G[N];
int dfs (int v, int par = 0) {
vector <int> vec;
int sum = 0;
for (int u: G[v])
if (u != par) {
vec.push_back(dfs(u, v));
sum += vec.back();
}
sum += Arr[v];
sort(vec.begin(), vec.end());
while (k < sum) {
sum -= vec.back();
ans++;
vec.pop_back();
}
return sum;
}
int main() {
ios::sync_with_stdio(false), cin.tie(0);
cin >> n >> k;
for (int i = 0; i < n; i++)
cin >> Arr[i];
for (int i = 0; i < n - 1; i++) {
int v, u;
cin >> v >> u;
v--, u--;
G[v].push_back(u);
G[u].push_back(v);
}
dfs(0);
cout << ans << "\n";
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... |