Submission #1051687

# Submission time Handle Problem Language Result Execution time Memory
1051687 2024-08-10T09:02:49 Z TAhmed33 Chase (CEOI17_chase) C++
30 / 100
599 ms 177124 KB
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const int MAXN = 1e5 + 25;
ll dp[MAXN][101][2], 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, int c) {
	ll &ret = dp[pos][cur][c];
	if (ret != -1) {
		return ret;
	}
	ll v = (c ? p[pos] : 0);
	ret = v;
	int z = (int)adj[pos].size();
	ll pref[z + 1] = {}, suff[z + 2] = {};
	for (int i = 0; i < z; i++) {
		pref[i + 1] = pref[i] + p[adj[pos][i]];
	}
	for (int i = z - 1; i >= 0; i--) {
		suff[i + 1] = suff[i + 2] + p[adj[pos][i]];
	}
	for (int i = 0; i < z; i++) {
		ret = max(ret, v + ans(adj[pos][i], cur, 0));
		if (cur) ret = max(ret, v + pref[i] + suff[i + 2] + ans(adj[pos][i], cur - 1, 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, 0) << '\n';
}		
signed main () {
	ios::sync_with_stdio(0); cin.tie(0);
	int tc = 1; //cin >> tc;
	while (tc--) solve();
}	
# Verdict Execution time Memory Grader output
1 Incorrect 19 ms 161680 KB Output isn't correct
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Incorrect 19 ms 161680 KB Output isn't correct
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 585 ms 177124 KB Output is correct
2 Correct 599 ms 176972 KB Output is correct
3 Correct 48 ms 168908 KB Output is correct
4 Correct 54 ms 166904 KB Output is correct
5 Correct 280 ms 167068 KB Output is correct
6 Correct 259 ms 167004 KB Output is correct
7 Correct 273 ms 166996 KB Output is correct
# Verdict Execution time Memory Grader output
1 Incorrect 19 ms 161680 KB Output isn't correct
2 Halted 0 ms 0 KB -