#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;
vector<int> grafo[maxn];
multiset<ll> S;
void dfs(int u, int p)
{
S.insert(-(soma[u]-(ll)num[p]));
for (auto v: grafo[u])
if (v != p) dfs(v, u);
}
void solve(int u, int p)
{
for (auto v: grafo[u])
{
if (v == p) continue;
auto it = S.find(-soma[u]); S.erase(it);
it = S.find(-(soma[v]-(ll)num[u])); S.erase(it);
S.insert(-soma[v]); S.insert(-(soma[u]-(ll)num[v]));
ll aux = 0LL, ind = 1;
for (auto x: S)
{
aux += -x, ind++;
if (ind > min(n, c)) break;
}
ans = max(ans, aux);
solve(v, u);
it = S.find(-soma[v]); S.erase(it);
it = S.find(-(soma[u]-(ll)num[v])); S.erase(it);
S.insert(-soma[u]); S.insert(-(soma[v]-(ll)num[u]));
}
}
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); solve(1, 0);
cout << ans << "\n";
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
4 ms |
2688 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
4 ms |
2688 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
603 ms |
17528 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
4 ms |
2688 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |