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 long long MAX=1e5+10;
long long n,k;
long long niza[MAX];
bool vis[MAX];
long long sumofsub[MAX];
vector<long long> graph[MAX];
long long res=0;
void dfs(long long node, long long parent)
{
sumofsub[node]=niza[node];
vector<long long> v;
for(auto x:graph[node])
{
if(x==parent)continue;
dfs(x,node);
v.push_back(sumofsub[x]);
sumofsub[node]+=sumofsub[x];
}
if(sumofsub[node]>k)
{
sort(v.begin(),v.end());
long long zbir=niza[node];
for(auto x:v)
{
if(zbir+x>k)
{
sumofsub[node]-=x;
res++;
}
else zbir+=x;
}
}
}
int main()
{
ios_base::sync_with_stdio(false);
cin.tie(NULL);
cout.tie(NULL);
cin>>n>>k;
for(long long i=0; i<n; i++)cin>>niza[i];
for(long long i=0; i<n-1; i++)
{
long long a,b;
cin>>a>>b;
a--;b--;
graph[a].push_back(b);
graph[b].push_back(a);
}
dfs(0,0);
cout<<res<<endl;
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... |