제출 #103662

#제출 시각아이디문제언어결과실행 시간메모리
103662ShtefPaprike (COI18_paprike)C++14
100 / 100
107 ms20484 KiB
#include <iostream>
#include <vector>
#include <algorithm>

using namespace std;

typedef long long ll;

ll n, k, h[100005], kolko[100005];
vector <int> ms[100005];

bool cmp(ll x, ll y){
	return x > y;
}

ll dfs(int x, int p){
	vector <int> v;
	kolko[x] = 1;
	for(vector <int>::iterator i = ms[x].begin() ; i != ms[x].end() ; ++i){
		int o = *i;
		if(o == p)
			continue;
		v.push_back(dfs(o, x));
		kolko[x] += kolko[o];
	}
	sort(v.begin(), v.end(), cmp);
	//cout << x << ":" << kolko[x] << endl;
	if(!v.empty() && v[0] >= h[x]){
		v[0] -= h[x];
		kolko[x]--;
		int i = 1;
		while(i < (int)v.size() && v[0] >= (k - v[i])){
			v[0] -= (k - v[i]);
			i++;
			kolko[x]--;
		}
		//cout << x << "-" << kolko[x] << endl;
		return v[0];
	}
	//cout << x << "-" << kolko[x] << endl;
	return k - h[x];
}

int main(){
ios_base::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
cin >> n >> k;
for(int i = 1 ; i <= n ; ++i){
	cin >> h[i];
}
for(int i = 0 ; i < n - 1 ; ++i){
	int x, y;
	cin >> x >> y;
	ms[x].push_back(y);
	ms[y].push_back(x);
}
dfs(1, -1);
cout << kolko[1] - 1 << endl;

return 0;
}
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...