이 제출은 이전 버전의 oj.uz에서 채점하였습니다. 현재는 제출 당시와는 다른 서버에서 채점을 하기 때문에, 다시 제출하면 결과가 달라질 수도 있습니다.
#include <bits/stdc++.h>
using namespace std;
#define int long long
const int MAXN = 1e5 + 10;
const int MAXV = 110;
array<vector<int>, MAXN> dp;
array<vector<int>, MAXN> grafo;
array<int, MAXN> pom, pomviz;
int N, V;
void dfs(int v, int p)
{
dp[v].clear(); dp[v].resize(V+1);
pomviz[v] = 0;
for (auto viz : grafo[v])
{
if (viz == p) continue;
pomviz[v] += pom[viz];
}
for (auto viz : grafo[v])
{
if (viz == p) continue;
dfs(viz, v);
dp[v][0] = max(dp[viz][0], dp[v][0]);
for (int k = 1; k <= V; k++)
{
dp[v][k] = max({dp[v][k], dp[viz][k], dp[viz][k-1] + pomviz[v]});
}
}
}
int32_t main()
{
ios_base::sync_with_stdio(false);
cin.tie(NULL);
cin >> N >> V;
for (int i = 1; i <= N; i++) cin >> pom[i];
for (int i = 1; i < N; i++)
{
int X, Y;
cin >> X >> Y;
grafo[X].push_back(Y);
grafo[Y].push_back(X);
}
dfs(1, 1);
int resp = dp[1][V];
if (N <= 1000)
{
for (int i = 2; i <= N; i++)
{
dfs(i, i);
resp = max(resp, dp[i][V]);
}
}
cout << resp << '\n';
}
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |