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 <iostream>
#include <vector>
#include <algorithm>
using namespace std;
#define FOR(i, a, b) for (int i = (a); i < (b); i++)
#define FORd(i,a,b) for (int i = (b)-1; i >= (a); i--)
#define pb(x) push_back(x)
int n;
long long k, s[100015], d[100015], res[100015];
vector<int> a[100015];
void dfs(int u, int p)
{
int child = 0;
for(int v : a[u])
if(v != p) dfs(v, u), ++child;
if (child<1)
{
d[u] = s[u];
return;
}
res[u] = 0;
d[u] = s[u];
vector<long long> b;
for(int v : a[u])
if(v != p) b.pb(d[v]), res[u] += res[v];
sort(b.begin(), b.end());
reverse(b.begin(), b.end());
while (!b.empty() && d[u]+b.back()<=k)
d[u] += b.back(), b.pop_back();
res[u] += (int)b.size();
}
int main()
{
cin >> n >> k;
FOR(i,1,n+1) cin >> s[i];
FOR(i,0,n-1)
{
int u,v; cin >> u >> v;
a[u].pb(v);
a[v].pb(u);
}
dfs(1, -1);
if (d[1]) ++res[1];
cout << res[1]-1;
}
# | 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... |