#include <bits/stdc++.h>
using namespace std;
#define MAXN 505
int a[MAXN];
int dp[MAXN][MAXN][2];
vector <int> g[MAXN];
int n,m;
void dfs(int v,int p)
{
for(auto x:g[v])
{
if(x==p)continue;
dfs(x,v);
for(int i = m; i >= 0; i--)
{
for(int j = 0; j <= i; j++)
{
if(i-j-1>=0)dp[v][i][0]=max(dp[v][i][0],max(dp[v][j][1]+max(dp[x][i-j-1][0],dp[x][i-j-1][1]),dp[v][j][0]+dp[x][i-j-1][1]));
if(i-j-2>=0)dp[v][i][1]=max(dp[v][i][1],dp[v][j][1]+dp[x][i-j-2][1]);
}
}
}
for(int i = m; i >= 1; i--)
{
dp[v][i][0]=max(dp[v][i][0],max(dp[v][i-1][0],dp[v][i-1][1])+a[v]);
dp[v][i][1]=max(dp[v][i][1],dp[v][i-1][1]+a[v]);
}
}
int main()
{
cin >> n >> m;
for(int i = 1; i <= n; i++)
{
cin >> a[i];
}
for(int i = 0; i < n-1; i++)
{
int u,v;
cin >> u >> v;
g[u].push_back(v);
g[v].push_back(u);
}
dfs(1,0);
cout << max(dp[1][m][0],dp[1][m][1]);
return 0;
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
2 ms |
376 KB |
Output isn't correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
2 ms |
616 KB |
Output isn't correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
3 ms |
616 KB |
Output isn't correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
3 ms |
704 KB |
Output isn't correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
10 ms |
828 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
5 ms |
1008 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
78 ms |
1248 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
31 ms |
1696 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
150 ms |
2208 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
293 ms |
2688 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |