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 n, k;
vector<int> h;
vector<vector<int> > g;
vector<int> dpa, dpb;
pair<int, int> dfs(int cur, int par){
vector<pair<int, int> > c;
int sum = 0;
for(int u: g[cur]){
if(u == par) continue;
c.push_back(dfs(u, cur));
sum += c.back().second;
}
sort(c.begin(), c.end());
int csum = h[cur];
for(auto u: c){
if(csum + u.first <= k){
csum += u.first;
}else{
sum++;
}
}
return {csum, sum};
}
signed main(){
ios::sync_with_stdio(false); cin.tie(0);
cin>>n>>k;
h.resize(n);
for(int i = 0; i < n; i++){
cin>>h[i];
}
g.resize(n);
for(int i = 0; i < n-1; i++){
int x, y; cin>>x>>y; x--, y--;
g[x].push_back(y);
g[y].push_back(x);
}
cout<<dfs(0, -1).second<<endl;
}
# | 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... |