#include <bits/stdc++.h>
#define fi first
#define se second
#define mp make_pair
#define pb push_back
#define eb emplace_back
using namespace std;
typedef long long ll;
typedef pair<int, int> ii;
const int maxn = 505;
vector<int> adj[maxn];
int f[maxn][maxn][2];
int N, M, a[maxn];
void calc(int u, int p = -1)
{
for(int v : adj[u]) if(v != p){
calc(v, u);
for(int i = M; i >= 1; --i){
for(int j = 2; j <= i; ++j){
f[u][i][0] = max(f[u][i][0], f[u][i - j][0] + f[v][j - 2][1]);
f[u][i][1] = max(f[u][i][1], f[u][i - j][1] + f[v][j - 2][1]);
}
for(int j = 1; j <= i; ++j){
f[u][i][0] = max(f[u][i][0], f[u][i - j][1] + f[v][j - 1][0]);
}
}
}
for(int i = M; i >= 1; --i){
f[u][i][0] = max(f[u][i][0], f[u][i - 1][0] + a[u]);
f[u][i][1] = max(f[u][i][1], f[u][i - 1][1] + a[u]);
}
}
signed main(void)
{
ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0);
#ifdef LOCAL
freopen("A.INP", "r", stdin);
freopen("A.OUT", "w", stdout);
#endif // LOCAL
cin >> N >> M;
for(int i = 1; i <= N; ++i)
cin >> a[i];
for(int i = 1; i < N; ++i){
int u, v; cin >> u >> v;
adj[u].eb(v); adj[v].eb(u);
}
calc(1);
cout << f[1][M][0];
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
5 ms |
384 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
5 ms |
512 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
6 ms |
512 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
5 ms |
640 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
10 ms |
768 KB |
Output is correct |
2 |
Correct |
7 ms |
820 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
6 ms |
896 KB |
Output is correct |
2 |
Correct |
32 ms |
900 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
54 ms |
1156 KB |
Output is correct |
2 |
Correct |
20 ms |
1152 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
24 ms |
1536 KB |
Output is correct |
2 |
Correct |
132 ms |
1548 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
102 ms |
1932 KB |
Output is correct |
2 |
Correct |
44 ms |
1912 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
193 ms |
2296 KB |
Output is correct |
2 |
Correct |
24 ms |
2304 KB |
Output is correct |