# | Time | Username | Problem | Language | Result | Execution time | Memory |
---|---|---|---|---|---|---|---|
1117678 | vjudge1 | Paprike (COI18_paprike) | C++17 | 0 ms | 0 KiB |
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;
#define SPEED ios_base::sync_with_stdio(0); cin.tie(NULL); cout.tie(NULL);
#define ALL(x) x.begin(), x.end()
#define intt long long
#define pb push_back
#define endl "\n"
const int sz = 1e5 + 5;
intt n, k, ans = 0;
vector<vector<intt>>graph;
vector<intt> val(sz), subsize(sz, 0);
s
void dfs(intt node, intt parent, intt k) {
vector<intt>subtree;
subsize[node] = val[node];
for(auto u : graph[node]) {
if(parent != u) {
dfs(u, node, k);
subsize[node] += subsize[u];
subtree.pb(subsize[u]);
}
}
sort(ALL(subtree));
reverse(ALL(subtree));
for(int i = 0; i < (int)subtree.size(); i++) {
if(subsize[node] > k) {
subsize[node] -= subtree[i];
ans++;
} else{
break;
}
}
}
void solve() {
cin >> n >> k;
graph.resize(n+1);
val.resize(n + 1);
for(int i = 1; i <= n; i++) {
cin >> val[i];
}
for(int i = 1; i <= n - 1; i++) {
intt a, b;
cin >> a >> b;
graph[a].pb(b);
graph[b].pb(a);
}
dfs(1, -1, k);
cout << ans << endl;
}
int main() {
SPEED;
intt tst = 1;
// cin >> tst;
while (tst--) {
solve();
}
}