#include <bits/stdc++.h>
#define int long long
using namespace std;
#define ll long long
#define ld long double
#define ull unsigned long long
#define pii pair<int,int>
#define pll pair<long long, long long>
#define fi first
#define se second
#define all(a) (a).begin(), (a).end()
#define pb push_back
#define lwb lower_bound
#define upb upper_bound
#define TASKNAME "NAME"
void init()
{
ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0);
///freopen(TASKNAME".INP","r",stdin); freopen(TASKNAME".OUT","w",stdout);
}
const int SZ = 5e2+5;
const ll INF = INT_MAX / 2, MOD = 1e9+7, INFLL = 2e18;
const double epsilon = 1e-3;
int n,m, a[SZ], dp[SZ][SZ][2];
vector<int> adj[SZ];
void dfs(int u, int pre)
{
for(int v : adj[u])
{
if(v == pre) continue;
dfs(v, u);
vector<int> tem[2];
tem[0].resize(m + 1, 0);
tem[1].resize(m + 1, 0);
for(int j = 0; j <= m; j++)
{
for(int k = 0; k < j; k++)
{
if(j - k > 1)
{
tem[0][j] = max(tem[0][j], dp[u][j - k - 2][0] + dp[v][k][0]);
tem[1][j] = max(tem[1][j], dp[u][j - k - 2][1] + dp[v][k][0]);
}
tem[1][j] = max(tem[1][j], dp[u][j - k - 1][0] + dp[v][k][1]);
}
}
for (int j = 0; j <= m; j++)
{
dp[u][j][0] = max(dp[u][j][0], tem[0][j]);
dp[u][j][1] = max(dp[u][j][1], tem[1][j]);
}
}
for (int j = m; j >= 1; j--){
dp[u][j][0] = max(dp[u][j][0], dp[u][j - 1][0] + a[u]);
dp[u][j][1] = max(dp[u][j][1], dp[u][j - 1][1] + a[u]);
}
}
signed main()
{
init();
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;
adj[u].pb(v);
adj[v].pb(u);
}
dfs(1, 0);
int res = 0;
for(int i = 1; i <= n; i++)
{
res = max(res, dp[1][i][1]);
}
cout << res;
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
600 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
1 ms |
604 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
2 ms |
600 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
604 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
10 ms |
1140 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
3 ms |
1116 KB |
Output is correct |
2 |
Incorrect |
46 ms |
1460 KB |
Output isn't correct |
3 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
87 ms |
1964 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
29 ms |
2384 KB |
Output is correct |
2 |
Incorrect |
200 ms |
2668 KB |
Output isn't correct |
3 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
169 ms |
3540 KB |
Output is correct |
2 |
Correct |
64 ms |
3412 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
333 ms |
4296 KB |
Output is correct |
2 |
Correct |
31 ms |
3428 KB |
Output is correct |