Submission #48461

# Submission time Handle Problem Language Result Execution time Memory
48461 2018-05-13T17:28:39 Z aleksami Dostavljač (COCI18_dostavljac) C++14
14 / 140
219 ms 2740 KB
#include <bits/stdc++.h>

using namespace std;
#define MAXN 505
int n,m;
int dp[MAXN][MAXN][2];
int a[MAXN];
vector <int> g[MAXN];

void dfs(int v,int p)
{
	for(auto x:g[v])
	{
		if(x==p)continue;
		dfs(x,v);
		for(int i = m; i >= 0; i--)
		{
			for(int j = i; j >= 0; j--)
			{
				if(i-j-1>=0)dp[v][i][0]=max(dp[v][i][0],dp[v][j][1]+dp[x][i-j-1][0]);
				if(i-j-2>=0)dp[v][i][1]=max(dp[v][i][1],dp[v][j][1]+dp[x][i-j-2][1]);
			}
		}
	}
	for(int i = m; i >= 1; i--)
	{
		dp[v][i][0]=max(dp[v][i][0],dp[v][i-1][0]+a[v]);
		dp[v][i][1]=max(dp[v][i][1],dp[v][i-1][1]+a[v]);
		dp[v][i][0]=max(dp[v][i][0],dp[v][i][1]);
	}
}

int main()
{
	ios_base::sync_with_stdio(false);
	cin.tie(NULL);
	cout.tie(NULL);
	cin >> n >> m;
	for(int i = 1; i <= n; i++)
	{
		cin >> a[i];
	}
	for(int i = 0; i < n-1; i++)
	{
		int u,v;
		cin >> u >> v;
		g[u].push_back(v);
		g[v].push_back(u);
	}
	dfs(1,0);
	cout << max(dp[1][m][0],dp[1][m][1]);
	return 0;
}
# Verdict Execution time Memory Grader output
1 Correct 2 ms 376 KB Output is correct
# Verdict Execution time Memory Grader output
1 Incorrect 2 ms 560 KB Output isn't correct
# Verdict Execution time Memory Grader output
1 Incorrect 3 ms 676 KB Output isn't correct
# Verdict Execution time Memory Grader output
1 Incorrect 3 ms 904 KB Output isn't correct
# Verdict Execution time Memory Grader output
1 Incorrect 10 ms 916 KB Output isn't correct
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Incorrect 4 ms 1208 KB Output isn't correct
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Incorrect 60 ms 1512 KB Output isn't correct
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Incorrect 22 ms 1772 KB Output isn't correct
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Incorrect 112 ms 2216 KB Output isn't correct
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Incorrect 219 ms 2740 KB Output isn't correct
2 Halted 0 ms 0 KB -