Submission #1117673

#TimeUsernameProblemLanguageResultExecution timeMemory
1117673raul2008487Paprike (COI18_paprike)C++17
100 / 100
44 ms20816 KiB
#include<bits/stdc++.h>

#define ll long long
#define pb push_back
#define in insert
#define fi first
#define se second
#define vl vector<ll>
#define all(v) v.begin(), v.end()
#define endl "\n"

using namespace std;
const int sz = 1e5 + 123; /// mind this
const int MAX = 2e6 + 123;
const int BS = 61;
const ll inf = 1e17;
const int mod = 998244353;
ll c[sz], s[sz], par[sz], ans, k;
vl e[sz];
void init(ll v, ll p){
    par[v] = p;
    for(auto to: e[v]){
        if(to != p){
            init(to, v);
            s[v] += s[to];
        }
    }
    s[v] += c[v];
}
ll dfs(ll v, ll p){
    if(s[v] <= k){return s[v];}
    vl children;
    for(auto to: e[v]){
        if(to != p){
            s[v] -= s[to];
            children.pb(dfs(to, v));
            s[v] += s[to];
        }
    }
    if(s[v] > k){
        sort(all(children));
        while(s[v] > k){
            s[v] -= children.back();
            children.pop_back();
            ans ++;
        }
    }
    assert(s[v] <= k);
    return s[v];
}
void solve(){
    ll n, i, j, u, v;
    cin >> n >> k;
    for(i = 1; i <= n; i++){
        cin >> c[i];
    }
    for(i = 1; i < n; i++){
        cin >> u >> v;
        e[u].pb(v);
        e[v].pb(u);
    }
    init(1, 0);
    dfs(1, 0);
    cout << ans << endl;


}
int main(){
    ios_base::sync_with_stdio(false);
    cin.tie(0);
    cout.tie(0);
    ll t = 1;
    // cin >> t;
    while(t--){
        solve();
    }
}

/*
10 10
10 10 10 10 10 10 10 10 10 10
1 2
2 4
5 2
6 3
3 1
6 7
9 7
8 6
8 10

*/

Compilation message (stderr)

paprike.cpp: In function 'void solve()':
paprike.cpp:52:14: warning: unused variable 'j' [-Wunused-variable]
   52 |     ll n, i, j, u, v;
      |              ^
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...