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 all(v) v.begin(), v.end()
#define int long long
using namespace std;
const int sz = 1e5 + 5;
int n, k, a[sz];
array<int, 2> dp[sz];
vector<int> g[sz];
void dfs(int v, int p)
{
dp[v] = {a[v], 0};
vector<int> vals;
for(int i : g[v])
{
if(i == p) continue;
dfs(i, v);
vals.push_back(dp[i][0]);
dp[v][1] += dp[i][1] + 1;
}
sort(all(vals));
reverse(all(vals));
while(!vals.empty() && dp[v][0] + vals.back() <= k)
{
dp[v][0] += vals.back();
dp[v][1]--;
vals.pop_back();
}
}
signed main()
{
ios_base::sync_with_stdio(0);
cin.tie(0);
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[v].push_back(u);
g[u].push_back(v);
}
dfs(1, 0);
cout << dp[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... |