#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';
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
6492 KB |
Output is correct |
2 |
Correct |
2 ms |
6492 KB |
Output is correct |
3 |
Correct |
2 ms |
6492 KB |
Output is correct |
4 |
Correct |
2 ms |
6492 KB |
Output is correct |
5 |
Correct |
2 ms |
6508 KB |
Output is correct |
6 |
Correct |
2 ms |
6492 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
6492 KB |
Output is correct |
2 |
Correct |
2 ms |
6492 KB |
Output is correct |
3 |
Correct |
2 ms |
6492 KB |
Output is correct |
4 |
Correct |
2 ms |
6492 KB |
Output is correct |
5 |
Correct |
2 ms |
6508 KB |
Output is correct |
6 |
Correct |
2 ms |
6492 KB |
Output is correct |
7 |
Correct |
161 ms |
7256 KB |
Output is correct |
8 |
Correct |
30 ms |
6492 KB |
Output is correct |
9 |
Correct |
21 ms |
6492 KB |
Output is correct |
10 |
Correct |
167 ms |
7384 KB |
Output is correct |
11 |
Correct |
61 ms |
6744 KB |
Output is correct |
12 |
Correct |
38 ms |
6492 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
118 ms |
96072 KB |
Output is correct |
2 |
Correct |
118 ms |
96340 KB |
Output is correct |
3 |
Correct |
72 ms |
92516 KB |
Output is correct |
4 |
Correct |
60 ms |
15452 KB |
Output is correct |
5 |
Correct |
113 ms |
92128 KB |
Output is correct |
6 |
Correct |
110 ms |
92240 KB |
Output is correct |
7 |
Correct |
111 ms |
92380 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
6492 KB |
Output is correct |
2 |
Correct |
2 ms |
6492 KB |
Output is correct |
3 |
Correct |
2 ms |
6492 KB |
Output is correct |
4 |
Correct |
2 ms |
6492 KB |
Output is correct |
5 |
Correct |
2 ms |
6508 KB |
Output is correct |
6 |
Correct |
2 ms |
6492 KB |
Output is correct |
7 |
Correct |
161 ms |
7256 KB |
Output is correct |
8 |
Correct |
30 ms |
6492 KB |
Output is correct |
9 |
Correct |
21 ms |
6492 KB |
Output is correct |
10 |
Correct |
167 ms |
7384 KB |
Output is correct |
11 |
Correct |
61 ms |
6744 KB |
Output is correct |
12 |
Correct |
38 ms |
6492 KB |
Output is correct |
13 |
Correct |
118 ms |
96072 KB |
Output is correct |
14 |
Correct |
118 ms |
96340 KB |
Output is correct |
15 |
Correct |
72 ms |
92516 KB |
Output is correct |
16 |
Correct |
60 ms |
15452 KB |
Output is correct |
17 |
Correct |
113 ms |
92128 KB |
Output is correct |
18 |
Correct |
110 ms |
92240 KB |
Output is correct |
19 |
Correct |
111 ms |
92380 KB |
Output is correct |
20 |
Incorrect |
114 ms |
92244 KB |
Output isn't correct |
21 |
Halted |
0 ms |
0 KB |
- |