제출 #828218

#제출 시각아이디문제언어결과실행 시간메모리
828218vjudge1Paprike (COI18_paprike)C++17
100 / 100
66 ms31224 KiB
#include<bits/stdc++.h> //#pragma GCC optimize("Ofast,unroll-loops") //#pragma GCC target("avx2,popcnt,lzcnt,abm,bmi,bmi2,fma,tune=native") #define pii pair<int, int> #define fi first #define se second #define pb push_back #define int long long #define ld long double using namespace std; using ll=long long; const int inf = 1e9; const int MN =2e5+5; const int mod=998244353; int n,k; vector<int> adj[MN]; vector<pii> g[MN]; int a[MN]; pii dp[MN]; void dfs(int u, int p) { int sum=a[u], cnt=0; for (int v:adj[u]) { if(v==p) continue; dfs(v,u); g[u].pb(dp[v]); cnt+=dp[v].se; sum+=dp[v].fi; } if(g[u].size()==0) return; sort(g[u].begin(), g[u].end()); while(sum>k) { cnt++; sum-=g[u].back().fi; g[u].pop_back(); } dp[u]={sum,cnt}; // cout<<u<<" "<<dp[u].fi<<" "<<dp[u].se<<"\n"; } void solve() { cin>>n>>k; for (int i=1; i<=n; i++) { cin>>a[i]; dp[i].fi=a[i]; } for (int i=1; i<n; i++) { int x,y; cin>>x>>y; adj[x].pb(y); adj[y].pb(x); } dfs(1,0); cout<<dp[1].se; } signed main() { ios_base::sync_with_stdio(0); cin.tie(0); // freopen(".inp", "r", stdin); // freopen(".out", "w", stdout); int t=1; // cin>>t; while(t--) { solve(); } }
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...