#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] = sub_dp[i-1][j][0];
sub_dp[i][j][1] = sub_dp[i-1][j][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] + dp[v[i]][l][0]);
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);
int ans = 0;
for(int i=1; i<=m; i++) {
for(int j=0; j<2; j++) {
ans = max(ans, dp[1][i][j]);
}
}
cout << ans << "\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 |
4 ms |
468 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
6 ms |
596 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
9 ms |
716 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
13 ms |
892 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
20 ms |
1100 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
32 ms |
1564 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
42 ms |
1860 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
74 ms |
2780 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
78 ms |
3564 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
107 ms |
4256 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |