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;
#include<fstream>
#define ll long long
#define vt vector
#define pb push_back
#define fi first
#define se second
#define forr(i, a, b) for(int i = a; i < b; i++)
#define dorr(i, a, b) for(int i = a; i >= b; i--)
typedef unsigned long long ull;
#define pii pair<int, int>
#pragma GCC optimize("O3,unroll-loops")
#pragma GCC target("avx2,bmi,bmi2,lzcnt,popcnt")
const ll mod= 1e9 + 7;
const int mxk = 1e3, sq = 750, mxn = 1e5;
const ll inf = -1e18;
ll n, k;
ll d[mxn + 1], w[mxn + 1], a[mxn + 1];
vt<int>adj[mxn + 1];
void dfs(int s, int pre){
vt<ll>v;
d[s] = 0;
for(auto i: adj[s]){
if(i != pre){
dfs(i, s); d[s] += d[i];
v.pb(w[i]);
}
}
sort(v.begin(), v.end());
w[s] = a[s]; int last = v.size();
for(int i = 0; i < v.size(); i++){
if(w[s] + v[i] <= k){
w[s] += v[i];
}else{
last = i; break;
}
}
d[s] += v.size() - last;
}
signed main(){
ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0);
cin >> n >> k;
for(int i = 1; i <= n; i++)cin >> a[i];
forr(i, 1, n){
int u, v; cin >> u >> v;
adj[u].pb(v); adj[v].pb(u);
}
dfs(1, -1);
cout << d[1];
return(0);
}
Compilation message (stderr)
paprike.cpp: In function 'void dfs(int, int)':
paprike.cpp:34:22: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<long long int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
34 | for(int i = 0; i < v.size(); i++){
| ~~^~~~~~~~~~
# | 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... |