#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
const int maxn = 500 + 37;
int a[maxn];
vector<int> t[maxn];
short Time = 1, st[maxn], pos[maxn];
short dis[maxn][maxn];
int dp[maxn][maxn], pd[maxn][maxn];
void dfsdis(int v, int src, int par = -1){
for (auto u : t[v]){
if (u == par)
continue;
dis[src][u] = dis[src][v] + 1;
dfsdis(u, src, v);
}
}
void dfs(int v, int par = -1){
pos[Time] = v;
st[v] = Time ++;
for (auto u : t[v])
if (u != par)
dfs(u, v);
}
int main(){
int n, m;
cin >> n >> m;
for (int i = 1; i <= n; i++)
cin >> a[i];
for (int i = 0; i < n - 1; i++){
int v, u;
cin >> v >> u;
t[v].push_back(u);
t[u].push_back(v);
}
for (int i = 1; i <= n; i++)
dfsdis(i, i);
dfs(1);
memset(dp, -63, sizeof dp);
dp[1][0] = 0, dp[1][1] = a[1];
int answer = a[1];
for (int i = 2; i <= n; i++){
int v = pos[i];
for (int x = 0; x <= m; x++){
for (int j = 1; j < i; j++){
int u = pos[j];
if (dis[u][v] + 1 <= x)
dp[i][x] = max(dp[i][x], dp[j][x - dis[u][v] - 1] + a[v]);
}
if (x)
dp[i][x] = max(dp[i][x], dp[i][x - 1]);
}
}
pos[n + 1] = 1;
memset(pd, -63, sizeof pd);
pd[n + 1][0] = 0;
for (int i = n; i >= 1; i--){
int v = pos[i];
for (int x = 0; x <= m; x++){
for (int j = i + 1; j <= n + 1; j++){
int u = pos[j];
if (dis[v][u] + 1 <= x)
pd[i][x] = max(pd[i][x], pd[j][x - dis[u][v] - 1] + a[v]);
}
if (x)
pd[i][x] = max(pd[i][x], pd[i][x - 1]);
}
}
for (int i = 2; i <= n; i++){
int v = pos[i], h = dis[1][i];
for (int j = 0; j <= m; j++){
int p = min(m - j + h + 1, m);
dp[i][j] = max(dp[i][j], 0);
pd[i][p] = max(pd[i][p], 0);
// cout << v << " " << j << " " << p << " : " << dp[i][j] << " + " << pd[i][p] << endl;
answer = max(answer, dp[i][j] + pd[i][p] - a[v]);
}
}
cout << answer << '\n';
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
6 ms |
2552 KB |
Output isn't correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
6 ms |
2552 KB |
Output isn't correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
7 ms |
2680 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
7 ms |
2680 KB |
Output isn't correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
15 ms |
2680 KB |
Output is correct |
2 |
Incorrect |
10 ms |
2680 KB |
Output isn't correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
14 ms |
2808 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
76 ms |
2808 KB |
Output is correct |
2 |
Incorrect |
45 ms |
2808 KB |
Output isn't correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
84 ms |
2864 KB |
Output is correct |
2 |
Correct |
206 ms |
2936 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
303 ms |
3104 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
660 ms |
3292 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |