#include<bits/stdc++.h> #define LL long long usingnamespace std; constint N = 5e5 + 10; int n; LL m, A[N], DP[N][2]; vector<int> G[N]; voidDFS(int u, int f){ if (G[u].size() - (f > 0) == 0) { if (A[f] >= A[u]) DP[u][0] = A[u], DP[u][1] = -1e15; else DP[u][0] = A[f], DP[u][1] = A[u]; return ; } for (int v : G[u]) if (v != f) DFS(v, u); int d = ((int)G[u].size() - 1) / 2, cnt = 0; vector<LL> tmp; LL res1 = 0, res0 = 0; for (int v : G[u]) if (v != f) { res1 += max(DP[v][0], DP[v][1]); if (DP[v][1] > DP[v][0]) tmp.emplace_back(DP[v][1] - DP[v][0]), ++ cnt; } sort(tmp.begin(), tmp.end()); res0 = res1; for (int i = 0; i < cnt - d; i ++) res0 -= tmp[i]; if (!f) { DP[u][0] = res0; return ; } if (!d) { if (A[f] >= A[u]) DP[u][0] = res0 + A[u], DP[u][1] = -1e15; else DP[u][0] = res0 + A[f], DP[u][1] = res0 + A[u]; return ; } for (int i = 0; i < cnt - d + 1; i ++) res1 -= tmp[i]; if (A[f] >= A[u]) DP[u][0] = max(res1 + A[f], res0 + A[u]), DP[u][1] = res1 + m; else DP[u][0] = max(res0, res1) + A[f], DP[u][1] = max(res0 + A[u], res1 + m); }
intmain(){ ios :: sync_with_stdio(0); cin.tie(0); cout.tie(0); cin >> n >> m; if (!n) { cout << "0\n"; return0; } for (int i = 1; i <= n; i ++) cin >> A[i]; for (int i = 1, u, v; i < n; i ++) { cin >> u >> v; G[u].emplace_back(v); G[v].emplace_back(u); } DFS(1, 0); cout << DP[1][0] << "\n"; return0; }