#include <bits/stdc++.h>
#define task "D"
#define all(v) (v).begin(), (v).end()
#define rep(i, l, r) for (int i = (l); i <= (r); ++i)
#define Rep(i, r, l) for (int i = (r); i >= (l); --i)
#define DB(X) { cerr << #X << " = " << (X) << '\n'; }
#define DB1(A, _) { cerr << #A << "[" << _ << "] = " << (A[_]) << '\n'; }
#define DB2(A, _, __) { cerr << #A << "[" << _ << "][" << __ << "] = " << (A[_][__]) << '\n'; }
#define DB3(A, _, __, ___) { cerr << #A << "[" << _ << "][" << __ << "][" << ___ << "] = " << (A[_][__][___]) << '\n'; }
#define PR(A, l, r) { cerr << '\n'; rep(_, l, r) DB1(A, _); cerr << '\n';}
#define SZ(x) ((int)(x).size())
#define pb push_back
#define eb emplace_back
#define pf push_front
#define F first
#define S second
#define by(x) [](const auto& a, const auto& b) { return a.x < b.x; } // sort(arr, arr + N, by(a));
#define next ___next
#define prev ___prev
#define y1 ___y1
#define left ___left
#define right ___right
#define y0 ___y0
#define div ___div
#define j0 ___j0
#define jn ___jn
using ll = long long;
using ld = long double;
using ull = unsigned long long;
using namespace std;
typedef pair<int, int> ii;
typedef pair<ii, int> iii;
typedef vector<int> vi;
typedef vector<ii> vii;
typedef vector<ll> vl;
const int N = 502;
int n, M, a[N], dp[N][N][2], f[2][N][2], sz[N];
vi g[N];
void upd(int &x, int y)
{
x = max(x, y);
}
void dfs(int u, int p)
{
sz[u] = 1;
for (int v : g[u]) if (v != p)
{
dfs(v, u);
sz[u] += sz[v];
}
int now = 0, lst = 1, cursz = 0;
rep(w, 1, M) f[0][w][0] = f[0][w][1] = -1e9;
f[0][0][0] = f[0][0][1] = 0;
for (int v : g[u]) if (v != p)
{
cursz += sz[v];
now ^= 1, lst ^= 1;
rep(w, 0, M) f[now][w][0] = f[lst][w][0], f[now][w][1] = f[lst][w][1];
rep(W, 0, min(M, 3 * cursz)) rep(w, 0, min(W - 1, 3 * sz[v]))
{
upd(f[now][W][1], f[lst][W - w - 1][0] + dp[v][w][0]);
if (W - w - 2 >= 0)
{
upd(f[now][W][0], f[lst][W - w - 2][0] + dp[v][w][1]);
upd(f[now][W][1], f[lst][W - w - 2][1] + dp[v][w][1]);
}
}
}
rep(w, 0, M)
{
dp[u][w][0] = f[now][w][1];
if (w > 0) upd(dp[u][w][0], f[now][w - 1][1] + a[u]);
dp[u][w][1] = f[now][w][0];
if (w > 0) upd(dp[u][w][1], f[now][w - 1][0] + a[u]);
}
}
int main()
{
#ifdef HynDuf
freopen(task".in", "r", stdin);
//freopen(task".out", "w", stdout);
#else
ios_base::sync_with_stdio(false); cin.tie(nullptr);
#endif
cin >> n >> M;
rep(i, 1, n) cin >> a[i];
rep(i, 2, n)
{
int u, v;
cin >> u >> v;
g[u].eb(v);
g[v].eb(u);
}
dfs(1, 0);
int ans = 0;
rep(w, 0, M) ans = max(ans, dp[1][w][0]);
cout << ans;
return 0;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
384 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
640 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
512 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
768 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
768 KB |
Output is correct |
2 |
Correct |
1 ms |
768 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
896 KB |
Output is correct |
2 |
Correct |
2 ms |
896 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
4 ms |
1152 KB |
Output is correct |
2 |
Correct |
5 ms |
1152 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
1536 KB |
Output is correct |
2 |
Correct |
7 ms |
1664 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
8 ms |
1920 KB |
Output is correct |
2 |
Correct |
12 ms |
1920 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
9 ms |
2304 KB |
Output is correct |
2 |
Correct |
4 ms |
2304 KB |
Output is correct |