Submission #249636

# Submission time Handle Problem Language Result Execution time Memory
249636 2020-07-15T12:23:41 Z kshitij_sodani Dostavljač (COCI18_dostavljac) C++14
140 / 140
10 ms 2304 KB
#include <bits/stdc++.h>
using namespace std;
typedef long long llo;
#define mp make_pair
#define pb push_back
#define a first 
#define b second

int n,m;
int dp[501][501];
int dp2[501][501];
int it[501];
vector<int> adj[501];
int ss[501];
void dfs(int no,int par2=-1){
	ss[no]=1;
	for(auto j:adj[no]){
		if(j==par2){
			continue;
		}
		dfs(j,no);
		ss[no]+=ss[j];
	}
	for(auto j:adj[no]){
		if(j==par2){
			continue;
		}
		for(int k=min(m,3*ss[no]);k>=1;k--){
			for(int i=min(min(k-1,m),3*ss[j]);i>=1;i--){
				dp2[no][k]=max(dp2[no][k],dp[no][k-i-1]+dp2[j][i]);
			}
			for(int i=min(min(k-2,m),3*ss[j]);i>=1;i--){
				dp2[no][k]=max(dp2[no][k],dp2[no][k-i-2]+dp[j][i]);
				dp[no][k]=max(dp[no][k],dp[no][k-i-2]+dp[j][i]);
			}
		}
	}

	for(int i=m;i>=1;i--){
		dp[no][i]=max(dp[no][i],dp[no][i-1]+it[no]);
		dp2[no][i]=max(dp2[no][i],dp2[no][i-1]+it[no]);
	}
}
int main(){
	ios_base::sync_with_stdio(false);
	cin.tie(NULL);
	cin>>n>>m;
	for(int i=0;i<n;i++){
		cin>>it[i];
	}
	for(int i=0;i<n-1;i++){
		int aa,bb;
		cin>>aa>>bb;
		aa--;
		bb--;
		adj[aa].pb(bb);
		adj[bb].pb(aa);
	}
	dfs(0);
	cout<<max(dp[0][m],dp2[0][m])<<endl;
	


	return 0;
}

# Verdict Execution time Memory Grader output
1 Correct 1 ms 384 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 0 ms 512 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 512 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 640 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 768 KB Output is correct
2 Correct 1 ms 768 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 896 KB Output is correct
2 Correct 2 ms 896 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 3 ms 1152 KB Output is correct
2 Correct 4 ms 1152 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 3 ms 1536 KB Output is correct
2 Correct 6 ms 1664 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 7 ms 1920 KB Output is correct
2 Correct 8 ms 1920 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 10 ms 2304 KB Output is correct
2 Correct 5 ms 2304 KB Output is correct