#include "bits/stdc++.h"
using namespace std;
mt19937_64 rng((int)std::chrono::steady_clock::now().time_since_epoch().count());
const int MAXN = 505;
const int MOD = 1e9 + 7;
#define int long long
int rnd(int x, int y) {
int u = uniform_int_distribution<int>(x, y)(rng); return u;
}
int a[MAXN];
vector<int> adj[MAXN];
int dp[MAXN][MAXN][2]; // k = 1: returned to the root
void dfs(int node, int prv) {
vector<int> v;
v.push_back(-1);
for(int x: adj[node]) {
if(x != prv) {
dfs(x, node);
v.push_back(x);
}
}
int k = v.size() - 1;
int sub_dp[k + 1][MAXN][2];
for(int i=0; i<MAXN; i++) sub_dp[0][i][0] = sub_dp[0][i][1] = 0;
sub_dp[0][1][0] = sub_dp[0][1][1] = a[node];
for(int i=1; i<=k; i++) {
sub_dp[i][0][0] = sub_dp[i][0][1] = 0;
for(int j=1; j<MAXN; j++) {
sub_dp[i][j][0] = max(sub_dp[i-1][j][0], sub_dp[i][j-1][0]);
sub_dp[i][j][1] = max(sub_dp[i-1][j][1], sub_dp[i][j-1][1]);
for(int l=1; l<j; l++) {
sub_dp[i][j][0] = max(sub_dp[i][j][0], sub_dp[i-1][j-l-1][1] + max(dp[v[i]][l][0], dp[v[i]][l][1]));
if(l+1 < j) sub_dp[i][j][1] = max(sub_dp[i][j][1], sub_dp[i-1][j-l-2][1] + dp[v[i]][l][1]);
}
sub_dp[i][j][0] = max(sub_dp[i][j][0], sub_dp[i][j][1]);
}
}
for(int i=1; i<MAXN; i++) {
for(int j=0; j<2; j++) {
dp[node][i][j] = sub_dp[k][i][j];
}
}
}
void solve(int tc) {
int n, m;
cin >> n >> m;
for(int i=1; i<=n; i++) cin >> a[i];
for(int i=1; i<n; i++) {
int a, b;
cin >> a >> b;
adj[a].push_back(b);
adj[b].push_back(a);
}
dfs(1, -1);
cout << max(dp[1][m][0], dp[1][m][1]) << "\n";
}
int32_t main(){
ios::sync_with_stdio(0); cin.tie(0);
int t = 1; //cin >> t;
for(int i=1; i<=t; i++) solve(i);
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
5 ms |
460 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
7 ms |
588 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
12 ms |
776 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
19 ms |
940 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
34 ms |
1144 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
36 ms |
1484 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
47 ms |
1968 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
68 ms |
2688 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
111 ms |
3648 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
118 ms |
4348 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |