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>
// #pragma GCC optimize("Ofast,no-stack-protector,unroll-loops")
// #pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native")
#define int long long
// #define ll long long
#define pii pair<int, int>
#define all(v) v.begin(), v.end()
using namespace std;
const int oo = 1e9 + 9;
const int MAX = 2e5 + 5, LOGMAX = 20, B = 441, MOD = 998244353;
int n, k;
vector<int> g[MAX];
int arr[MAX];
int ans = 0;
int dfs(int node, int p){
int a = arr[node];
vector<int> v;
for(int to : g[node]){
if(to == p) continue;
v.emplace_back(dfs(to, node));
a += v.back();
}
sort(all(v));
while(a > k){
a -= v.back();
v.pop_back();
ans++;
}
return a;
}
void solve(){
cin >> n >> k;
for(int i = 1; i <= n; i++){
cin >> arr[i];
}
for(int i = 1; i < n ; i++){
int u, v; cin >> u >> v;
g[u].push_back(v);
g[v].push_back(u);
}
dfs(1, 1);
cout << ans << '\n';
}
signed main(){
ios::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
int t = 1;
// cin >> t;
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... |