Submission #43989

#TimeUsernameProblemLanguageResultExecution timeMemory
43989wasylPaprike (COI18_paprike)C++11
100 / 100
124 ms44132 KiB
#include <bits/stdc++.h>
#ifndef dbg
#define dbg(...)
#endif
#define all(x) begin(x), end(x)
#define rsz(...) resize(__VA_ARGS__)
#define psh(...) push_back(__VA_ARGS__)
#define emp(...) emplace_back(__VA_ARGS__)
#define prt(...) print(cout, __VA_ARGS__)
#define dmp(...) print(cerr, #__VA_ARGS__, '=', __VA_ARGS__)
#define dprt(...) dbg(print(cerr,__VA_ARGS__))
#define ddmp(...) dbg(dmp(__VA_ARGS__))
using namespace std;using ll=long long;
template<typename t>using V=vector<t>;
template<typename t>void print(ostream& os, const t& a){os<<a<<'\n';}
template<typename t, typename... A>void print
(ostream& os, const t& a, A&&... b){os<<a<<' ';print(os, b...);}

int n, k, r;
V< ll > val;
V< V< int > > gr;

inline void dfs (int v, int o)
{
    for (int s : gr[v])
        if (s != o)
            dfs(s, v);
    
    V< int > ilo;
    for (int s : gr[v])
        if (s != o)
        {
            val[v] += val[s];
            ilo.psh(val[s]);
        }
    
    sort(all(ilo));
    reverse(all(ilo));
    
    int pt = 0;    
    
    while (val[v] > k)
    {
        val[v] -= ilo[pt++];
        ++r;        
    }
}

int main()
{
    ios::sync_with_stdio(false);
    cin.tie(nullptr);
    
    cin >> n >> k;
    
    gr.rsz(n + 1);
    val.rsz(n + 1);
    
    for (int i = 1; i <= n; ++i)
        cin >> val[i];
        
    for (int i = 0; i < n - 1; ++i)
    {
        int p, q; cin >> p >> q;
        gr[p].psh(q);
        gr[q].psh(p);    
    }
    
    dfs(1, 1);
    
    prt(r);
}
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...