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 x first
#define y second
#define pb push_back
#define mp make_pair
#define in insert
#define up_b upper_bound
#define low_b lower_bound
#define sz(x) (int)x.size()
#define all(v) v.begin(),v.end()
using namespace std;
typedef long long ll;
typedef long double ld;
typedef pair<int,int> pii;
typedef pair<int,ll> pil;
typedef pair<ll,int> pli;
typedef pair<ll,ll> pll;
const ll INF = 1e18;
const int inf = INT_MAX;
const ll mod = 1e9 + 7;
const int dx[4] = {0, 0, 1, -1};
const int dy[4] = {1, -1, 0, 0};
const int N = 1e5 + 5;
const int A = 26;
ll n, k, ans, a[N], d[N];
vector < int > g[N];
void dfs(int v, int p){
	vector < int > val;
	d[v] = a[v];
	for(int i = 0; i < sz(g[v]); i++){
		int to = g[v][i];
		if(to != p){
			dfs(to, v);
			d[v] += d[to];
			val.pb(d[to]);
		}
	}
	sort(all(val));
	reverse(all(val));
	for(int i = 0; i < sz(val); i++){
		if(d[v] > k){
			d[v] -= val[i];
			ans++;
		}
		else break;
	}
}
int main(){
	
	cin >> n >> k;
	for(int i = 1; i <= n; i++){
		cin >> a[i];
	}
	for(int i = 1; i < n; i++){
		int u, v;
		cin >> u >> v;
		g[u].pb(v);
		g[v].pb(u);
	} 
	dfs(1, 0);
	cout << ans;
}
| # | 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... |