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>
using namespace std;
#ifdef LOCAL
#include "debug.h"
#else
#define debug(...) 42
#endif
const int N = 1e5 + 5;
int n, k;
int a[N], cnt[N];
long long dp[N];
vector<int> g[N];
void dfs(int u, int p) {
priority_queue<long long> pq;
dp[u] = a[u];
for (int v : g[u]) {
if (v ^ p) {
dfs(v, u);
pq.push(dp[v]);
dp[u] += dp[v];
cnt[u] += cnt[v];
}
}
while (dp[u] > k) {
dp[u] -= pq.top(); pq.pop();
++cnt[u];
}
}
int main() {
ios::sync_with_stdio(false); cin.tie(nullptr);
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].push_back(v);
g[v].push_back(u);
}
dfs(1, 1);
cout << cnt[1];
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... |