답안 #121590

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
121590 2019-06-26T19:56:45 Z pamaj Chase (CEOI17_chase) C++14
0 / 100
930 ms 524288 KB
#include <bits/stdc++.h>
using namespace std;

#define int int64_t

const int maxn = 1e5 + 10;

int v[maxn], p[maxn], n, t;
vector<int> G[maxn];

map<int, int> dp[maxn][110][2];

int solve(int x, int y, int num, int last)
{
	if(dp[x][num][last][y]) return dp[x][num][last][y];
	if(num == 0) return p[x];
	
	int best = 0;

	for(auto u : G[x])
	{
		if(u == y) continue;

		int caso1 = solve(u, x, num, 0);
		int caso2 = v[x] - p[y] + solve(u, x, num - 1, 1);

		if(last)
			caso2 = v[x] - p[u] + solve(u, x, num - 1, 1);
		else
			caso2 = v[x] - p[y] -p[u] + solve(u, x, num - 1, 1);

		best = max({best, caso1, caso2});
	}


	return dp[x][num][last][y] = best;
}

int32_t main()
{
	cin >> n >> t;

	for(int i = 1; i <= n; i++)
	{
		cin >> p[i];
	}

	for(int i = 0; i < n - 1; i++)
	{
		int a, b;
		cin >> a >> b;

		G[a].push_back(b);
		G[b].push_back(a);
	}

	for(int i = 1; i <= n; i++)
	{
		for(auto u : G[i])
		{
			v[i] += p[u];
		}
	}

	int ans = 0;

	for(int i = 1; i <= n; i++)
	{
		ans = max(ans, solve(i, 0, t, 0));
		//cout << i << " " << solve(i, 0, t, 0) << "\n";
	}
	cout << ans << "\n";

	// for(int i = 1; i <= n; i++)
	// {
	// 	cout << i << " " << v[i] << "\n";
	// }
}
# 결과 실행 시간 메모리 Grader output
1 Runtime error 930 ms 524288 KB Execution killed with signal 9 (could be triggered by violating memory limits)
2 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Runtime error 930 ms 524288 KB Execution killed with signal 9 (could be triggered by violating memory limits)
2 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Runtime error 395 ms 524288 KB Execution killed with signal 9 (could be triggered by violating memory limits)
2 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Runtime error 930 ms 524288 KB Execution killed with signal 9 (could be triggered by violating memory limits)
2 Halted 0 ms 0 KB -