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;
using ll = long long;
using ld = long double;
const int N = 100000;
vector <int> graf[N+5];
int val[N+5];
pair <int, int> dfs(int v, int p, int k){
int res = 0;
vector <int> vec;
for(auto c : graf[v]){
if(c == p) continue;
pair <int, int> x = dfs(c, v, k);
res += x.first;
vec.push_back(x.second);
}
sort(vec.begin(), vec.end());
reverse(vec.begin(), vec.end());
while(vec.size() && val[v] + vec.back() <= k) val[v] += vec.back(), vec.pop_back();
res += vec.size();
return {res, val[v]};
}
int main(){
ios_base::sync_with_stdio(false), cin.tie(0);
cout.precision(10);
cout << fixed;
int n, k;
cin >> n >> k;
for(int i=1; i<=n; i++) cin >> val[i];
for(int i=1; i<n; i++){
int a, b;
cin >> a >> b;
graf[a].push_back(b);
graf[b].push_back(a);
}
cout << dfs(1, 0, k).first << "\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... |