This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
/*
#pragma GCC optimize("Ofast")
#pragma GCC target("avx2")
#pragma GCC optimize("unroll-loops")
*/
#include <bits/stdc++.h>
#define taskname "bai3"
#define int long long
#define ull unsigned long long
#define float long double
#define pb push_back
#define mp make_pair
#define ff first
#define ss second
#define endl '\n'
#define pii pair<int, int>
using namespace std;
const int mxN = 1e5 + 5;
const int mod = 1e9 + 7;
const int oo = 0x3f3f3f3f;
int n, k, val[mxN], sus[mxN];
vector<int> G[mxN];
int ans;
int sum(int u, int par){
int res = val[u];
for(auto tmp : G[u]){
//cout << u << " " << tmp << endl;
if(tmp == par) continue;
res += sum(tmp, u);
}
if(res > k){
vector<int> v;
for(auto tmp : G[u]){
if(tmp == par) continue;
v.pb(sus[tmp]);
}
//cout << v.size() << endl;
sort(v.begin(), v.end());
int sums = val[u];
for(int i = 0; i < (int)v.size(); ++i){
if(sums + v[i] <= k) sums += v[i];
else{
ans += v.size() - i;
sus[u] = sums;
return sus[u];
}
}
}
//cout << u << " " << res << endl;
sus[u] = res;
return res;
}
void solve(){
cin >> n >> k;
for(int i = 1; i <= n; ++i){
cin >> val[i];
}
for(int i = 1; i < n; ++i){
int x, y; cin >> x >> y;
//cout << x << " " << y << endl;
G[x].pb(y);
G[y].pb(x);
}
sum(1, -1);
cout << ans << endl;
}
signed main(){
ios_base::sync_with_stdio(false);
cin.tie(nullptr);
//freopen(taskname".inp", "r", stdin);
//freopen(taskname".out", "w", stdout);
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... |