比赛 |
20241128 |
评测结果 |
AAAAAAAAAA |
题目名称 |
外卖 |
最终得分 |
100 |
用户昵称 |
┭┮﹏┭┮ |
运行时间 |
0.428 s |
代码语言 |
C++ |
内存使用 |
5.04 MiB |
提交时间 |
2024-11-28 10:40:05 |
显示代码纯文本
#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define pii pair<int,ll>
#define fi first
#define in inline
#define se second
#define mp make_pair
#define pb push_back
const int N = 510;
ll read(){
ll x = 0,f = 1;char c = getchar();
for(;c < '0' || c > '9';c = getchar())if(c == '-')f = -1;
for(;c >= '0' && c <= '9';c = getchar())x = (x<<1) + (x<<3) + c-'0';
return x * f;
}
int n,m;
int a[N];
vector<int>e[N];
ll f[N][N],g[N][N];
void dfs(int x,int fa){
for(int y : e[x]){
if(y == fa)continue;
dfs(y,x);
}
for(int i = 1;i <= m;i++)f[x][i] = g[x][i] = a[x];
for(int y : e[x]){
if(y == fa)continue;
for(int i = m;i >= 0;i--){
for(int j = 0;j <= i;j++){
if(i - j - 1 >= 0)f[x][i] = max(f[x][i],g[x][i - j - 1] + max(f[y][j],g[y][j]));
if(i - j - 2 >= 0)
f[x][i] = max(f[x][i],f[x][i - j - 2] + g[y][j]),
g[x][i] = max(g[x][i],g[x][i - j - 2] + g[y][j]);
}
}
}
}
int main(){
freopen("food.in","r",stdin);
freopen("food.out","w",stdout);
n = read(),m = read();
for(int i = 1;i <= n;i++)a[i] = read();
for(int i = 1;i < n;i++){
int x = read(),y = read();
e[x].pb(y),e[y].pb(x);
}
dfs(1,0);
ll ans = 0;
for(int i = 0;i <= m;i++)ans = max({ans,f[1][i],g[1][i]});
printf("%lld\n",ans);
return 0;
}