Submission #122219

# Submission time Handle Problem Language Result Execution time Memory
122219 2019-06-27T21:03:03 Z sams Chase (CEOI17_chase) C++14
0 / 100
849 ms 228220 KB
#include <bits/stdc++.h>

using namespace std;

typedef long long ll;
const int maxn = 1e5+10, maxk = 110;

int n, k;
ll dp[maxn][maxk], dp2[maxn][maxk], ant[maxn][maxk], ans, vis[maxn], vet[maxn];

vector <int> graph[maxn];

ll solve(int u, int f = 0, int c = k)
{
	if(c == 0) return dp[u][c] = 0;
	if(dp[u][c] != -1) return dp[u][c];

	for(int v: graph[u])
	{	
		if(v == f) continue;

		ll val = max(solve(v, u, c-1) + vis[v] - vet[u], solve(v, u, c));
		
		if(val > dp[u][c])
		{
			dp2[u][c] = dp[u][c];
			dp[u][c] = val;
		}
		else if(val > dp2[u][c]) dp2[u][c] = val;
	}
	
	return dp[u][c];
}

void rotate(int u, int f = 0)
{
	for(int i = 1 ; i <= k ; ++i)
	{
		ans = max({ans, dp[u][i], dp[u][i-1] + vis[u]});
	}

	for(int v: graph[u])
	{
		if(v == f) continue;

		for(int i = 1 ; i <= k ; ++i)
		{
			ant[u][i] = dp[u][i];
			
			ll val = max(dp[v][i], dp[v][i-1] + vis[v] - vet[u]);

			if(val == dp[u][i])
			{
				dp[u][i] = dp2[u][i];
			}
		}

		for(int i = 1 ; i <= k ; ++i)
		{
			ll val = max(dp[u][i], dp[u][i-1] + vis[u] - vet[v]);

			if(val > dp[v][i]) dp2[v][i] = dp[v][i], dp[v][i] = val;
			else if(val > dp2[v][i]) dp2[v][i] = val;
		}

		rotate(v, u);

		for(int i = 1 ; i <= k ; ++i) dp[u][i] = ant[u][i];
	}
}

int main()
{
	cin >> n >> k;

	for(int i = 1 ; i <= n ; ++i) cin >> vet[i];

	for(int i = 1 ; i < n ; ++i)
	{
		int u, v;
		cin >> u >> v;

		graph[u].push_back(v);
		graph[v].push_back(u);
	}

	for(int i = 1 ; i <= n ; ++i)
	{
		for(int v: graph[i])
		{
			vis[i] += vet[v];
		}
	}
	memset(dp, -1, sizeof dp);
	solve(1);

	rotate(1);

	cout << ans << '\n';
}
# Verdict Execution time Memory Grader output
1 Correct 67 ms 88792 KB Output is correct
2 Correct 68 ms 88824 KB Output is correct
3 Correct 67 ms 88824 KB Output is correct
4 Incorrect 68 ms 88824 KB Output isn't correct
5 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 67 ms 88792 KB Output is correct
2 Correct 68 ms 88824 KB Output is correct
3 Correct 67 ms 88824 KB Output is correct
4 Incorrect 68 ms 88824 KB Output isn't correct
5 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Incorrect 849 ms 228220 KB Output isn't correct
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 67 ms 88792 KB Output is correct
2 Correct 68 ms 88824 KB Output is correct
3 Correct 67 ms 88824 KB Output is correct
4 Incorrect 68 ms 88824 KB Output isn't correct
5 Halted 0 ms 0 KB -