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;
const int kN = 1e5;
int n, k, h[1 + kN];
vector<int> g[1 + kN];
struct state {
int minCuts;
int64_t minSum;
} dp[1 + kN];
void dfs(int u, int par) {
dp[u].minCuts = 0;
dp[u].minSum = h[u];
priority_queue<int64_t> pq;
for (int v : g[u]) {
if (v != par) {
dfs(v, u);
dp[u].minCuts += dp[v].minCuts;
dp[u].minSum += dp[v].minSum;
pq.emplace(dp[v].minSum);
}
}
while (dp[u].minSum > k) {
dp[u].minCuts += 1;
dp[u].minSum -= pq.top();
pq.pop();
}
}
void testCase() {
cin >> n >> k;
for (int i = 1; i <= n; ++i) {
cin >> h[i];
}
for (int i = 1; i < n; ++i) {
int u, v;
cin >> u >> v;
g[u].emplace_back(v);
g[v].emplace_back(u);
}
dfs(1, 0);
cout << dp[1].minCuts << '\n';
}
int main() {
ios_base::sync_with_stdio(false);
cin.tie(nullptr);
int tests = 1;
for (int tc = 0; tc < tests; ++tc) {
testCase();
}
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... |