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 ll long long
#define ld long double
#define sp ' '
#define en '\n'
#define smin(a, b) a = min(a, b)
#define smax(a, b) a = max(a, b)
using namespace std;
const int N = 1e5 + 2;
const int inf = 1e9;
vector<int> g[N];
ll sum[N], k;
int Dfs(int s, int e) {
int ans = 0;
vector<ll> v;
for (int u : g[s]) {
if (u == e) continue;
ans += Dfs(u, s);
v.push_back(sum[u]);
}
sort(v.rbegin(), v.rend());
while (!v.empty()) {
if (sum[s] + v.back() > k) break;
sum[s] += v.back();
v.pop_back();
}
return ans + v.size();
}
int main() {
ios::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
int n;
cin >> n >> k;
for (int i = 1; i <= n; i++) cin >> sum[i];
for (int i = 0; i < n - 1; i++) {
int u, v;
cin >> u >> v;
g[u].push_back(v);
g[v].push_back(u);
}
cout << Dfs(1, 0) << en;
return 0;
}
# | 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... |