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<bits/stdc++.h>
using namespace std;
#define int long long
int ans, k;
int f[100005] = {};
int dfs(vector<vector<int>> &m, vector<int> &w, int p){
int sum = w[p];
vector<int> down;
for (int i : m[p]){
if (i != f[p]){
f[i] = p;
down.emplace_back(dfs(m, w, i));
sum += down.back();
}
}
sort(down.begin(), down.end());
for (int i = down.size() - 1; sum > k; --i){
sum -= down[i];
++ans;
}
// cout << p << ' ' << sum << '\n';
return sum;
}
main(){
cin.tie(0);
ios::sync_with_stdio(false);
int n;
cin >> n >> k;
vector<int> w(n + 1);
for (int i = 1; i <= n; ++i) cin >> w[i];
vector<vector<int>> m(n + 1);
for (int i = 0; i < n - 1; ++i){
int a, b;
cin >> a >> b;
m[a].emplace_back(b);
m[b].emplace_back(a);
}
ans = 0;
dfs(m, w, 1);
cout << ans << '\n';
}
Compilation message (stderr)
paprike.cpp:28:6: warning: ISO C++ forbids declaration of 'main' with no type [-Wreturn-type]
28 | main(){
| ^
# | 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... |