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 ll long long
ll n, k, a, b;
vector<ll> sp, dp;
vector<vector<int>> v;
int cnt = 0;
void dfs(int x, int p){
set<vector<ll>> se;
dp[x] = sp[x];
for (auto el : v[x]){
if (el != p){
dfs(el, x);
se.insert({dp[el], el});
dp[x] += dp[el];
}
}
while (dp[x] > k){
dp[x] -= se.rbegin()->at(0);
se.erase(*se.rbegin());
cnt++;
}
}
int main(){
cin>>n>>k;
sp.assign(n + 2, 0), dp.assign(n + 1, 0);
v.assign(n + 1, vector<int>());
for (int i = 1; i <= n; i++){
cin>>sp[i];
}
for (int i = 0; i < n - 1; i++){
cin>>a>>b;
v[a].push_back(b);
v[b].push_back(a);
}
dfs(1, 0);
cout<<cnt;
}
# | 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... |