Submission #650197

# Submission time Handle Problem Language Result Execution time Memory
650197 2022-10-12T18:09:12 Z inksamurai Dostavljač (COCI18_dostavljac) C++17
140 / 140
12 ms 2004 KB
#include <bits/stdc++.h>
using namespace std;
#define rep(i,n) for(int i=0;i<n;i++)
#define rng(i,c,n) for(int i=c;i<n;i++)
#define per(i,n) for(int i=n-1;i>=0;i--)
#define fi first
#define se second
#define pb push_back
#define sz(a) (int)a.size()
#define vec(...) vector<__VA_ARGS__>
#define _3yqVz8E ios::sync_with_stdio(0),cin.tie(0)
typedef long long ll;
using pii=pair<int,int>;
using vi=vector<int>;
void print(){cout<<'\n';}
template<class h,class...t>
void print(const h&v,const t&...u){cout<<v<<' ',print(u...);}
// e

signed main(){
_3yqVz8E;
	int n,m;
	cin>>n>>m;
	vi a(n);
	rep(i,n){
		cin>>a[i];
	}
	vec(vi) adj(n);
	rep(i,n-1){
		int u,v;
		cin>>u>>v;
		u-=1,v-=1;
		adj[u].pb(v);
		adj[v].pb(u);
	}
	vi par(n,-1);
	vi cnt(n);
	auto dfs=[&](auto self,int v,int _par)->void{
		par[v]=_par;
		cnt[v]=1;
		for(auto u:adj[v]){
			if(u!=_par){
				self(self,u,v);
				cnt[v]+=cnt[u];
			}
		}
	};
	dfs(dfs,0,-1);
	vec(vec(vi)) dp(n);
	auto rfs=[&](auto self,int v,int _par)->void{
		int si=1;
		vec(vi) fdp(si+1,vi(2));
		fdp[1][0]=fdp[1][1]=a[v];
		for(auto u:adj[v]){
			if(u==_par) continue;
			self(self,u,v);
			int ad=sz(dp[u])-1;
			int nsi=si+ad+2;
			vec(vi) nfdp(nsi+1,vi(2));
			for(int i=0;i<=si;i++){
				nfdp[i][0]=max(nfdp[i][0],fdp[i][0]);
				nfdp[i][1]=max(nfdp[i][1],fdp[i][1]);
				for(int j=0;j<=ad;j++){
					nfdp[i+j+2][0]=max(nfdp[i+j+2][0],fdp[i][0]+dp[u][j][0]);
					nfdp[i+j+1][1]=max(nfdp[i+j+1][1],fdp[i][0]+dp[u][j][1]);
					nfdp[i+j+2][1]=max(nfdp[i+j+2][1],fdp[i][1]+dp[u][j][0]);
				}
			}
			fdp.swap(nfdp);
			si=nsi;
		}
		dp[v].swap(fdp);
	};
	rfs(rfs,0,0);
	int ans=0;
	rep(i,min(m+1,sz(dp[0])))ans=max({ans,dp[0][i][0],dp[0][i][1]});
	print(ans);
}
# Verdict Execution time Memory Grader output
1 Correct 0 ms 212 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 340 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 340 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 340 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 340 KB Output is correct
2 Correct 1 ms 468 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 468 KB Output is correct
2 Correct 1 ms 468 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 2 ms 596 KB Output is correct
2 Correct 2 ms 1108 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 3 ms 724 KB Output is correct
2 Correct 3 ms 724 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 5 ms 1108 KB Output is correct
2 Correct 5 ms 2004 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 8 ms 1236 KB Output is correct
2 Correct 12 ms 1056 KB Output is correct