Submission #650194

# Submission time Handle Problem Language Result Execution time Memory
650194 2022-10-12T17:01:46 Z inksamurai Dostavljač (COCI18_dostavljac) C++17
14 / 140
124 ms 19000 KB
#include <bits/stdc++.h>
#define int ll
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);
	auto dfs=[&](auto self,int v,int _par)->void{
		par[v]=_par;
		for(auto u:adj[v]){
			if(u!=_par){
				self(self,u,v);
			}
		}
	};
	dfs(dfs,0,-1);
	vec(vi) leb(n,vi(n,-1));
	rep(v,n){
		adj[v].pb(v);
		int si=sz(adj[v]);
		rep(j,si){
			leb[v][adj[v][j]]=j;
		}
	}
	const int inf=1e9;
	vec(vec(vi)) dp;
	auto affine=[&](int i,int v,int u,int val)->void{
		if(val<0) return;
		int j=leb[v][u];
		dp[i][v][j]=max(dp[i][v][j],val);
	};
	int ans=0;
	rng(type,1,2){
		if(type){
			m-=1;
		}
		dp=vec(vec(vi))(m+1,vec(vi)(n));
		rep(i,m+1){
			rep(v,n){
				int si=sz(adj[v]);
				rep(j,si){
					dp[i][v].pb(-inf);
				}
			}
		}
		if(!type){
			affine(0,0,0,0);
		}else{
			affine(0,0,0,a[0]);
		}
		rep(i,m){
			rep(v,n){
				int _par=par[v];
				multiset<int> mst;
				mst.insert(dp[i][v][leb[v][v]]);
				if(_par!=-1){
					affine(i+1,v,v,dp[i][v][leb[v][_par]]+a[v]);
					mst.insert(dp[i][v][leb[v][_par]]);
				}
				int si=sz(adj[v]);
				rep(j,si){
					int u=adj[v][j];
					if(u==_par or u==v) continue;
					mst.insert(dp[i][v][j]);
					// affine(i+1,u,v,now);
				}
				rep(j,si){
					int u=adj[v][j];
					if(u==_par or u==v) continue;
					mst.erase(mst.find(dp[i][v][j]));
					if(sz(mst)){
						auto it=prev(mst.end());
						affine(i+1,u,v,*it);
					}
					mst.insert(dp[i][v][j]);
				}
				if(_par!=-1){
					affine(i+1,_par,v,*prev(mst.end()));
				}
			}
		}
		rep(i,m+1){
			rep(v,n){
				int si=sz(adj[v]);
				rep(j,si){
					ans=max(ans,dp[i][v][j]);
				}
			}
		}
	}
	cout<<ans<<"\n";
}
# Verdict Execution time Memory Grader output
1 Correct 1 ms 212 KB Output is correct
# Verdict Execution time Memory Grader output
1 Incorrect 1 ms 340 KB Output isn't correct
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Incorrect 2 ms 596 KB Output isn't correct
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Incorrect 1 ms 468 KB Output isn't correct
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Incorrect 6 ms 1656 KB Output isn't correct
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Incorrect 5 ms 1236 KB Output isn't correct
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Incorrect 32 ms 5976 KB Output isn't correct
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Incorrect 26 ms 4892 KB Output isn't correct
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Incorrect 82 ms 12320 KB Output isn't correct
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Incorrect 124 ms 19000 KB Output isn't correct
2 Halted 0 ms 0 KB -