This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#include<bits/stdc++.h>
using namespace std;
const int N = 505;
int n, m, a[N], dp[N][N][2];
vector < int > g[N];
void dfs(int v, int pr = -1){
dp[v][0][0] = dp[v][0][1] = 0;
dp[v][1][1] = dp[v][1][0] = a[v];
for(int to : g[v]){
if(to != pr){
dfs(to, v);
for(int i = m; i >= 1; i--){
for(int j = 0; j < i; j++){
if(i - j - 1 >= 0){
dp[v][i][0] = max(dp[v][i][0], dp[v][j][1] + dp[to][i - j - 1][0]);
}
if(i - j - 2 >= 0){
dp[v][i][1] = max(dp[v][i][1], dp[v][j][1] + dp[to][i - j - 2][1]);
dp[v][i][0] = max(dp[v][i][0], dp[v][j][0] + dp[to][i - j - 2][1]);
}
}
}
}
}
}
int main(){
ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0);
cin >> n >> m;
memset(dp, -63, sizeof(dp));
for(int i = 1; i <= n; i++){
cin >> a[i];
}
for(int i = 1; i < n; i++){
int x, y;
cin >> x >> y;
g[x].push_back(y);
g[y].push_back(x);
}
dfs(1);
int ans = 0;
for(int i = 1; i <= m; i++){
ans = max({ans, dp[1][i][0], dp[1][i][1]});
}
cout << ans;
}
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |