#include <iostream>
#include <algorithm>
using namespace std;
int v[110],n,M;
int dp[110][110]={0};
bool is[110]={false},e[110][110]={false};
void postorder(int root)
{
if(is[root]) return ;
is[root]=true;
for(int i=1;i<=n;i++) if(e[root][i])
postorder(i);
dp[root][1]=v[root];
for(int t_child=1;t_child<=n;t_child++) if(e[root][t_child]&&!is[t_child]){
for(int m=M;m>=2;m--)
for(int m_child=1;m_child<m;m_child++)
dp[root][m]=max(dp[root][m],dp[root][m-m_child]+dp[t_child][m_child]);
}
is[root]=false;
}
int main()
{
cin>>n>>M;
for(int i=1;i<=n;i++) cin>>v[i];
for(int i=1;i<n;i++){
int a,b;
cin>>a>>b;
e[a][b]=e[b][a]=true;
}
postorder(1);
cout<<dp[1][M]<<endl;
return 0;
}
曾经看到过一个讲树形DP的文章说要将K-树转化为二叉树,其实是没必要的,我们只需要将其递推关系转化为背包问题或者其组合就可以解决了,采取后序遍历从树叶推根