#include <bits/stdc++.h>
using namespace std;
#define int long long
#define max3(a, b, c) max(a, max(b, c))
#define min3(a, b, c) min(a, min(b, c))
#define mp make_pair
#define f first
#define se second
#define pb push_back
#define ppb pop_back
#define ll long long
#define ull unsigned long long
#define cntbit(x) __builtin_popcount(x)
#define uset unordered_set
#define umap unordered_map
#define pii pair<int, int>
#define ld long double
#define pll pair<long long, long long>
const int inf = 1e9;
const int N = 5e2 + 15;
int n, m, a[N], dp[N][N], ans;
vector <int> g[N];
void dfs(int v, int p = -1) {
for(int to : g[v]) {
if(to != p) {
for(int k = m - 1; k >= 0; --k) {
dp[to][k + 1] = max(dp[to][k + 1], dp[v][k]);
if(k + 2 <= m)
dp[to][k + 2] = max(dp[to][k + 2], dp[v][k] + a[to]);
}
dfs(to, v);
for(int k = m - 1; k >= 0; --k)
dp[v][k + 1] = max(dp[v][k + 1], dp[to][k]);
}
}
}
#undef int
int main() {
#define int long long
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;
g[u].pb(v);
g[v].pb(u);
}
for(int i = 0; i < N; ++i)
for(int j = 0; j < N; ++j)
dp[i][j] = -inf;
dp[1][0] = 0, dp[1][1] = a[1];
dfs(1);
for(int i = 1; i <= n; ++i)
ans = max(ans, dp[i][m]);
cout << ans << endl;
return 0;
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
4 ms |
2424 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
4 ms |
2396 KB |
Output isn't correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
4 ms |
2428 KB |
Output isn't correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
4 ms |
2424 KB |
Output isn't correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
5 ms |
2472 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
4 ms |
2424 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
4 ms |
2424 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
5 ms |
2424 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
6 ms |
2424 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
5 ms |
2424 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |