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>
#define ll long long
#define pb push_back
#define in insert
#define fi first
#define se second
#define vl vector<ll>
#define all(v) v.begin(), v.end()
#define endl "\n"
using namespace std;
const int sz = 1e5 + 123; /// mind this
const int MAX = 2e6 + 123;
const int BS = 61;
const ll inf = 1e17;
const int mod = 998244353;
ll c[sz], s[sz], par[sz], ans, k;
vl e[sz];
void init(ll v, ll p){
par[v] = p;
for(auto to: e[v]){
if(to != p){
init(to, v);
s[v] += s[to];
}
}
s[v] += c[v];
}
ll dfs(ll v, ll p){
if(s[v] <= k){return s[v];}
vl children;
for(auto to: e[v]){
if(to != p){
s[v] -= s[to];
children.pb(dfs(to, v));
s[v] += s[to];
}
}
if(s[v] > k){
sort(all(children));
while(s[v] > k){
s[v] -= children.back();
ans ++;
}
}
assert(s[v] <= k);
return s[v];
}
void solve(){
ll n, i, j, u, v;
cin >> n >> k;
for(i = 1; i <= n; i++){
cin >> c[i];
}
for(i = 1; i < n; i++){
cin >> u >> v;
e[u].pb(v);
e[v].pb(u);
}
init(1, 0);
dfs(1, 0);
cout << ans << endl;
}
int main(){
ios_base::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
ll t = 1;
// cin >> t;
while(t--){
solve();
}
}
/*
1
2
9 0
*/
Compilation message (stderr)
paprike.cpp: In function 'void solve()':
paprike.cpp:51:14: warning: unused variable 'j' [-Wunused-variable]
51 | ll n, i, j, u, v;
| ^
# | 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... |