This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#pragma GCC optimize ("O3")
#pragma GCC target ("sse4")
#include <bits/stdc++.h>
#include <ext/pb_ds/tree_policy.hpp>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/rope>
using namespace std;
using namespace __gnu_pbds;
using namespace __gnu_cxx;
random_device(rd);
mt19937 rng(rd());
const long long FIXED_RANDOM = chrono::steady_clock::now().time_since_epoch().count();
struct custom_hash
{
	template<class T>
	unsigned long long operator()(T v) const
	{
		unsigned long long x = v;
		x += FIXED_RANDOM; x += 11400714819323198485ull;
		x = (x ^ (x >> 30)) * 13787848793156543929ull;
		x = (x ^ (x >> 27)) * 10723151780598845931ull;
		return x ^ (x >> 31);
	}
};
template<class T> using ordered_set = tree<T, null_type, less<T>, rb_tree_tag, tree_order_statistics_node_update>;
template<class T, class U> using hash_table = gp_hash_table<T, U, custom_hash>;
template<class T>
void readi(T &x)
{
	x = 0;
	bool negative = false;
	char c = ' ';
	while (c < '-')
	{
		c = getchar();
	}
	if (c == '-')
	{
		negative = true;
		c = getchar();
	}
	while (c >= '0')
	{
		x = x * 10 + (c - '0');
		c = getchar();
	}
	if (negative)
	{
		x = -x;
	}
}
template<class T>
void printi(T output)
{
	if (output == 0)
	{
		putchar('0');
		return;
	}
	if (output < 0)
	{
		putchar('-');
		output = -output;
	}
	int buf[20], n = 0;
	while(output)
	{
		buf[n] = ((output % 10));
		output /= 10;
		n++;
	}
	for (n--; n >= 0; n--)
	{
		putchar(buf[n] + '0');
	}
	return;
}
template<class T>
void ckmin(T &a, T b)
{
	a = min(a, b);
}
template<class T>
void ckmax(T &a, T b)
{
	a = max(a, b);
}
long long expo(long long a, long long e, long long mod)
{
	return ((e == 0) ? 1 : ((expo(a * a % mod, e >> 1, mod)) * ((e & 1) ? a : 1) % mod));
}
template<class T, class U>
void nmod(T &x, U mod)
{
	if (x >= mod) x -= mod;
}
template<class T>
T gcd(T a, T b)
{
	return (b ? gcd(b, a % b) : a);
}
template<class T>
T randomize(T mod)
{
	return (uniform_int_distribution<T>(0, mod - 1))(rng);
}
#define y0 ___y0
#define y1 ___y1
#define MP make_pair
#define PB push_back
#define LB lower_bound
#define UB upper_bound
#define fi first
#define se second
#define DBG(x) cerr << #x << " = " << x << endl
#define SZ(x) ((int) (x.size()))
#define FOR(i, a, b) for (auto i = (a); i < (b); i++)
#define FORD(i, a, b) for (auto i = (a) - 1; i >= (b); i--)
#define ALL(x) x.begin(), x.end()
const long double PI = 4.0 * atan(1.0);
const long double EPS = 1e-9;
#define MAGIC 347
#define SINF 10007
#define CO 1000007
#define INF 1000000007
#define BIG 1000000931
#define LARGE 1696969696967ll
#define GIANT 2564008813937411ll
#define LLINF 2696969696969696969ll
#define MAXN 100013
#define MAXK 113
typedef long long ll;
typedef long double ld;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
typedef pair<ld, ld> pdd;
typedef vector<int> vi;
typedef vector<ll> vl;
typedef vector<ld> vd;
typedef vector<pii> vpi;
typedef vector<pll> vpl;
typedef vector<pdd> vpd;
int N, K;
ll arr[MAXN];
int parent[MAXN];
vi edge[MAXN];
ll sum[MAXN];
ll dup[MAXN][MAXK], ddn[MAXN][MAXK];
ll ans;
vector<int> ord;
void dfs(int u)
{
	for (int v : edge[u])
	{
		if (v == parent[u]) continue;
		parent[v] = u;
		dfs(v);
	}
	ord.PB(u);
}
void solve(int u)
{
	pair<pll, pll> UP[MAXK], DN[MAXK];
	UP[0] = {{0, -1}, {0, -2}};
	FOR(i, 1, K + 1) UP[i] = {{sum[u], -1}, {0, -1}};
	FOR(i, 0, K + 1) DN[i] = {{0, -3}, {0, -4}};
	for (int v : edge[u])
	{
		if (v == parent[u]) continue;
		FOR(i, 1, K + 1)
		{
			//don't take it!
			pll poss = {max(dup[v][i - 1] + (sum[u] - arr[v]), dup[v][i]), v};
			if (poss > UP[i].fi)
			{
				UP[i].se = UP[i].fi;
				UP[i].fi = poss;
			}
			else if (poss > UP[i].se)
			{
				UP[i].se = poss;
			}
		}
		FOR(i, 1, K + 1)
		{
			//don't take it!
			pll poss = {max(ddn[v][i - 1] + (sum[v] - arr[u]), ddn[v][i]), v};
			if (poss > DN[i].fi)
			{
				DN[i].se = DN[i].fi;
				DN[i].fi = poss;
			}
			else if (poss > DN[i].se)
			{
				DN[i].se = poss;
			}
		}
	}
	FOR(i, 1, K + 1)
	{
		vector<pll> guys;
		guys.PB(UP[i].fi); guys.PB(UP[i].se); guys.PB(UP[i - 1].fi); guys.PB(UP[i - 1].se);
		pair<pll, pll> best = {{-INF, -INF}, {-INF, -INF}};
		FOR(i, 0, 4)
		{
			FOR(j, 0, 4)
			{
				if (guys[i].se == guys[j].se) continue;
				pair<pll, pll> cur = {{guys[i].fi, guys[j].fi}, {guys[i].se, guys[j].se}};
				ckmax(best, cur);
			}
		}
		UP[i] = {{best.fi.fi, best.se.fi}, {best.fi.se, best.se.se}};
		guys.clear();
		guys.PB(DN[i].fi); guys.PB(DN[i].se); guys.PB(DN[i - 1].fi); guys.PB(DN[i - 1].se);
		best = {{-INF, -INF}, {-INF, -INF}};
		FOR(i, 0, 4)
		{
			FOR(j, 0, 4)
			{
				if (guys[i].se == guys[j].se) continue;
				pair<pll, pll> cur = {{guys[i].fi, guys[j].fi}, {guys[i].se, guys[j].se}};
				ckmax(best, cur);
			}
		}
		DN[i] = {{best.fi.fi, best.se.fi}, {best.fi.se, best.se.se}};
	}
	FOR(i, 0, K + 1)
	{
		auto p = UP[i], p1 = DN[K - i];
		if (p.fi.se != p1.fi.se)
		{
			ckmax(ans, p.fi.fi + p1.fi.fi);
		}
		if (p.fi.se != p1.se.se)
		{
			ckmax(ans, p.fi.fi + p1.se.fi);
		}
		if (p.se.se != p1.fi.se)
		{
			ckmax(ans, p.se.fi + p1.fi.fi);
		}
	}
	FOR(i, 0, K + 1)
	{
		dup[u][i] = UP[i].fi.fi;
		ddn[u][i] = DN[i].fi.fi;
	}
	//solve it for up!
}
int32_t main()
{
	ios_base::sync_with_stdio(0); cin.tie(0);
	// cout << fixed << setprecision(10);
	// cerr << fixed << setprecision(10);
	// if (fopen("file.in", "r"))
	// {
		// freopen ("file.in", "r", stdin);
		// freopen ("file.out", "w", stdout);
	// }
	cin >> N >> K;
	FOR(i, 0, N)
	{
		cin >> arr[i];
	}
	FOR(i, 0, N - 1)
	{
		int u, v;
		cin >> u >> v; u--; v--;
		edge[u].PB(v);
		edge[v].PB(u);
	}
	FOR(u, 0, N)
	{
		for (int v : edge[u])
		{
			sum[u] += arr[v];
		}
	}
	parent[0] = N;
	dfs(0);
	//S_(v_j) - A(v_(j-1))
	for (int u : ord)
	{
		solve(u);
	}
	cout << ans << '\n';
	// cerr << "time elapsed = " << (clock() / (CLOCKS_PER_SEC / 1000)) << " ms" << endl;
	return 0;
}
/* READ READ READ
* int overflow, maxn too small, special cases (n=1?, two distinct?), cin.tie() interactive
* reread the problem, try small cases
* do smth instead of nothing
*/
| # | 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... |