Submission #1051694

#TimeUsernameProblemLanguageResultExecution timeMemory
1051694TAhmed33Chase (CEOI17_chase)C++98
30 / 100
197 ms90456 KiB
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const int MAXN = 1e5 + 25;
ll dp[MAXN][101], p[MAXN];
vector <int> adj[MAXN];
int n, v; 
void fix (int pos, int par) {
	if (par) {
		adj[pos].erase(find(adj[pos].begin(), adj[pos].end(), par));
	}
	for (auto j : adj[pos]) {
		fix(j, pos);
	}
}
ll ans (int pos, int cur) {
	ll &ret = dp[pos][cur];
	if (ret != -1) {
		return ret;
	}
	ret = 0;
	ll sum = 0;
	for (auto j : adj[pos]) {
		sum += p[j];
	}
	for (auto i : adj[pos]) {
		ret = max(ret, ans(i, cur));
		if (cur) ret = max(ret, sum + ans(i, cur - 1));
	}
	return ret;
}
void solve () {
	cin >> n >> v;
	for (int i = 1; i <= n; i++) {
		cin >> p[i];
	}
	for (int i = 1; i < n; i++) {
		int a, b; cin >> a >> b;
		adj[a].push_back(b);
		adj[b].push_back(a);
	}
	memset(dp, -1, sizeof(dp));
	fix(1, 0);
	cout << ans(1, v) << '\n';
}		
signed main () {
	ios::sync_with_stdio(0); cin.tie(0);
	int tc = 1; //cin >> tc;
	while (tc--) solve();
}	
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...