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 N = 1e5 + 10;
int n, k, a[N], sz[N], ans, tmp;
vector<int> g[N];
void dfs(int v, int p = -1){
vector<int> vec;
sz[v] = a[v];
for (int u : g[v]){
if (u == p) continue;
dfs(u, v);
sz[v] += sz[u];
vec.push_back(sz[u]);
}
sort(vec.begin(), vec.end());
while (sz[v] > k){
sz[v] -= vec.back();
vec.pop_back();
tmp++;
}
}
int main(){
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);
}
ans = 1e9;
for (int v = 1; v <= n; v ++){
tmp = 0;
dfs(v);
ans = min(ans, tmp);
}
cout << ans << endl;
}
# | 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... |