Submission #120024

# Submission time Handle Problem Language Result Execution time Memory
120024 2019-06-23T04:49:04 Z luciocf Dostavljač (COCI18_dostavljac) C++14
14 / 140
389 ms 2240 KB
#include <bits/stdc++.h>

using namespace std;

const int maxn = 510;

int n, m;
int A[maxn];

int dp[2][maxn][maxn];

vector<int> grafo[maxn];

void dfs(int u, int p)
{
	for (auto v: grafo[u])
	{
		if (v == p) continue;

		dfs(v, u);

		for (int i = m; i > 0; i--)
		{
			for (int j = 0; j <= i; j++)
			{
				dp[1][u][i] = max(dp[1][u][i], dp[1][u][i-1]);
				if (i >= j+1)
					dp[0][u][i] = max(dp[0][u][i], max(dp[0][v][j], dp[1][v][j]) + dp[1][u][i-j-1]);
				if (i >= j+2)
				{
					dp[1][u][i] = max(dp[1][u][i], dp[1][v][j] + dp[1][u][i-j-2]);
					dp[0][u][i] = max(dp[0][u][i], A[v] + max(dp[0][v][j], dp[1][v][j]) + dp[1][u][i-j-2]);
				}
				if (i >= j+3)
					dp[1][u][i] = max(dp[1][u][i], A[v] + dp[1][v][j] + dp[1][u][i-j-3]);
			}
		}
	}
}

int main(void)
{
	scanf("%d %d", &n, &m);

	for (int i = 1; i <= n; i++)
		scanf("%d", &A[i]);

	for (int i = 1; i < n; i++)
	{
		int u, v;
		scanf("%d %d", &u, &v);

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

	dfs(1, 0);

	int a = max(dp[1][1][m], dp[0][1][m]);
	int b = max(dp[1][1][m-1], dp[0][1][m-1]);

	printf("%d\n", max(a, A[1] + b));
}	

Compilation message

dostavljac.cpp: In function 'int main()':
dostavljac.cpp:43:7: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
  scanf("%d %d", &n, &m);
  ~~~~~^~~~~~~~~~~~~~~~~
dostavljac.cpp:46:8: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
   scanf("%d", &A[i]);
   ~~~~~^~~~~~~~~~~~~
dostavljac.cpp:51:8: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
   scanf("%d %d", &u, &v);
   ~~~~~^~~~~~~~~~~~~~~~~
# Verdict Execution time Memory Grader output
1 Correct 2 ms 384 KB Output is correct
# Verdict Execution time Memory Grader output
1 Incorrect 2 ms 512 KB Output isn't correct
# Verdict Execution time Memory Grader output
1 Incorrect 3 ms 512 KB Output isn't correct
# Verdict Execution time Memory Grader output
1 Incorrect 3 ms 512 KB Output isn't correct
# Verdict Execution time Memory Grader output
1 Incorrect 13 ms 640 KB Output isn't correct
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Incorrect 6 ms 768 KB Output isn't correct
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Incorrect 102 ms 1104 KB Output isn't correct
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Incorrect 37 ms 1272 KB Output isn't correct
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Incorrect 201 ms 1856 KB Output isn't correct
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Incorrect 389 ms 2240 KB Output isn't correct
2 Halted 0 ms 0 KB -