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
const int ssz = 2e5 + 5;
vector<int> adj[ssz], a(ssz);
array<int,2> dp[ssz];
int n, k;
void dfs(int node,int par = -1){
dp[node] = {a[node], 0};
vector<int>val;
for(auto i : adj[node]){
if(i == par){
continue;
}
dfs(i, node);
val.push_back(dp[i][0]);
dp[node][1] += dp[i][1] + 1;
}
sort(val.begin(), val.end());
reverse(val.begin(), val.end());
while(!val.empty() && dp[node][0] + val.back() <= k){
dp[node][0] += val.back();
dp[node][1]--;
val.pop_back();
}
}
void solve(){
cin >> n >> k;
for(int i = 1; i <= n; ++i){
cin >> a[i];
}
for(int i = 0; i < n - 1; ++i){
int u, v;
cin >> u >> v;
adj[u].push_back(v);
adj[v].push_back(u);
}
dfs(1, 0);
cout << dp[1][1] << endl;
}
signed main(){
int T = 1;
while(T--){
solve();
}
}
# | 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... |