#include <bits/stdc++.h>
//#include <ext/pb_ds/assoc_container.hpp>
//#include <ext/pb_ds/tree_policy.hpp>
//#pragma GCC optimize("unroll-loops")
//#pragma GCC optimize("-O3")
//#pragma GCC optimize("Ofast")
//#pragma GCC optimize("fast-math")
//#pragma GCC optimize("no-stack-protector")
#define F first
#define S second
#define sz(x) int(x.size())
#define pb push_back
#define N 101001
#define M ll(1e9 + 7)
#define inf 1e9 + 1e9
using namespace std;
//using namespace __gnu_pbds;
typedef long double ld;
typedef long long ll;
typedef short int si;
typedef array <int, 3> a3;
//typedef tree <int, null_type, less_equal<int>, rb_tree_tag, tree_order_statistics_node_update> ordered_set;
int n, m;
ll f[505][505][2], a[505], ans;
vector <int> g[505];
void dfs(int v, int p)
{
f[v][1][0] = a[v];
for (auto it : g[v])
{
if (it == p) continue;
dfs(it, v);
for (int i = m; i >= 0; i--)
for (int j = 0; j <= m; j++)
{
if (i + j + 2 <= m)
{
f[v][i + j + 2][0] = max(f[v][i + j + 2][0], f[v][i][0] + f[it][j][0]);
f[v][i + j + 2][1] = max(f[v][i + j + 2][1], f[v][i][1] + f[it][j][0]);
}
if (i + j + 1 <= m)
f[v][i + j + 1][1] = max(f[v][i + j + 1][1], f[v][i][0] + max(f[it][j][1], f[it][j][0]));
}
}
}
int main()
{
//freopen("input.txt", "r", stdin); //freopen("output4.txt", "w", stdout);
ios_base::sync_with_stdio(0); istream::sync_with_stdio(0); cin.tie(0); cout.tie(0);
cin >> n >> m;
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].pb(y);
g[y].pb(x);
}
dfs(1, -1);
for (int i = 0; i <= m; i++) ans = max(ans, max(f[1][i][0], f[1][i][1]));
cout << ans << endl;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
4 ms |
384 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
5 ms |
512 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
6 ms |
640 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
5 ms |
640 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
15 ms |
896 KB |
Output is correct |
2 |
Correct |
9 ms |
896 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
8 ms |
1024 KB |
Output is correct |
2 |
Correct |
56 ms |
1144 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
114 ms |
1608 KB |
Output is correct |
2 |
Correct |
44 ms |
1656 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
39 ms |
2040 KB |
Output is correct |
2 |
Correct |
220 ms |
1756 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
201 ms |
2768 KB |
Output is correct |
2 |
Correct |
82 ms |
3064 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
384 ms |
3344 KB |
Output is correct |
2 |
Correct |
41 ms |
2424 KB |
Output is correct |