#include <bits/stdc++.h>
using namespace std;
#define task "task"
#define bit(x, i) ((x >> i) & 1)
// https://trap.jp/post/1224/
#define FOR1(i, a, b) for(int i = (a), _b = (b); i <= _b; i ++)
#define FOR2(i, a, b, c) for(int i = (a), _b = (b); i <= _b; i += c)
#define FORD1(i, a, b) for(int i = (a), _b = (b); i >= _b; i --)
#define FORD2(i, a, b, c) for(int i = (a), _b = (b); i >= _b; i -= c)
#define overload4(a, b, c, d, name, ...) name
#define FOR(...) overload4(__VA_ARGS__, FOR2, FOR1)(__VA_ARGS__)
#define FORD(...) overload4(__VA_ARGS__, FORD2, FORD1)(__VA_ARGS__)
#define pb emplace_back
#define pf emplace_front
#define ins emplace
#define mp make_pair
#define fi first
#define se second
using ll = long long;
using pii = pair<int, int>;
using pll = pair<ll, ll>;
using vi = vector<int>;
using vl = vector<ll>;
// mt19937 rd(chrono::steady_clock::now().time_since_epoch().count());
// ll rand(ll l, ll r) { assert(l <= r); return uniform_int_distribution<ll>(l, r)(rd); }
const int N = 505;
const ll inf = 1e18;
const int mod = 1e9 + 7;
const int base = 31;
int n, m, a[N], dp[N][N][2];
vector<int> g[N];
void dfs(int u, int p){
    dp[u][1][0] = a[u];
    dp[u][1][1] = a[u];
    for(int v : g[u]) if(v != p){
        dfs(v, u);
        FORD(i, m, 0)
            FORD(j, m, 0){
                if(i + j + 2 <= m){
                    dp[u][i + j + 2][1] = max(dp[u][i + j + 2][1], dp[u][i][1] + dp[v][j][1]);
                    dp[u][i + j + 2][0] = max(dp[u][i + j + 2][0], dp[v][j][1] + dp[u][i][0]);
                }
                if(i + j + 1 <= m)
                    dp[u][i + j + 1][0] = max(dp[u][i + j + 1][0], dp[u][i][1] + dp[v][j][0]);
            }
    }
}
void solve(){
    cin>>n>>m;
    FOR(i, 1, n) cin>>a[i];
    FOR(i, 1, n - 1){
        int u, v; cin>>u>>v;
        g[u].pb(v);
        g[v].pb(u);
    }
    dfs(1, 0);
    cout<<dp[1][m][0];
}
signed main(){
    ios::sync_with_stdio(false);
    cin.tie(NULL);
    if(fopen(task".inp", "r")){
        freopen(task".inp", "r", stdin);
        freopen(task".out", "w", stdout);
    }
    int nTest = 1;
    // cin>>nTest;
    while(nTest --) solve();
    cerr << "\nTime: " << 1.0 * clock() / CLOCKS_PER_SEC << "s\n";
    return 0;
}
컴파일 시 표준 에러 (stderr) 메시지
dostavljac.cpp: In function 'int main()':
dostavljac.cpp:67:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
   67 |         freopen(task".inp", "r", stdin);
      |         ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~
dostavljac.cpp:68:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
   68 |         freopen(task".out", "w", stdout);
      |         ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~| # | 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... |