#include <bits/stdc++.h>
using namespace std;
const int maxn = 1e5+10;
const int maxv = 110;
typedef long long ll;
int n, c;
int num[maxn];
ll soma[maxn], ans;
ll d[2][maxn][maxv];
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 = 1; i <= c; i++)
{
ll t1 = max(d[0][v][i], d[1][v][i]);
ll t2 = max(d[0][v][i-1], d[1][v][i-1]);
ll D = max(t1, t2+soma[u]-(ll)num[p]);
if (D > d[0][u][i]) d[1][u][i] = d[0][u][i], d[0][u][i] = D;
else if (D > d[1][u][i]) d[1][u][i] = D;
}
}
}
/*
void rotate(int u, int p)
{
for (int i = 1; i <= c; i++) ans = max(ans, d[0][u][i]);
for (auto v: grafo[u])
{
if (v == p) continue;
for (int i = 1; i <= c; i++)
{
ll t1 = max(d[0][v][i], d[1][v][i]);
ll t2 = max(d[0][v][i-1], d[1][v][i-1]);
ll D = max(t1, t2+soma[u]);
if (d[0][v][i] == D) d[0][u][i] = d[1][u][i];
t1 = max(d[0][u][i], d[1][u][i]);
t2 = max(d[0][u][i-1], d[1][u][i-1]);
D = max(t1, t2+soma[])
}
}
}*/
int main(void)
{
ios::sync_with_stdio(false); cin.tie(0);
cin >> n >> c;
for (int i = 1; i <= n; i++)
cin >> num[i];
for (int i = 1; i < n; i++)
{
int u, v;
cin >> u >> v;
grafo[u].push_back(v); grafo[v].push_back(u);
}
for (int u = 1; u <= n; u++)
for (auto v: grafo[u])
soma[u] += (ll)num[v];
dfs(1, 0);
for (int i = 0; i <= c; i++)
ans = max(ans, d[0][1][i]);
cout << ans << "\n";
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
5 ms |
2688 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
5 ms |
2688 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
313 ms |
97236 KB |
Output is correct |
2 |
Correct |
285 ms |
97388 KB |
Output is correct |
3 |
Correct |
123 ms |
8044 KB |
Output is correct |
4 |
Correct |
325 ms |
148708 KB |
Output is correct |
5 |
Correct |
415 ms |
153568 KB |
Output is correct |
6 |
Correct |
422 ms |
153432 KB |
Output is correct |
7 |
Correct |
395 ms |
153208 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
5 ms |
2688 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |