이 제출은 이전 버전의 oj.uz에서 채점하였습니다. 현재는 제출 당시와는 다른 서버에서 채점을 하기 때문에, 다시 제출하면 결과가 달라질 수도 있습니다.
#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;
int 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);
ll aux = 0LL;
int ind = 1;
for (auto x: S)
{
aux += -x, ind++;
if (ind > min(n, c)) break;
}
ans = aux;
solve(1, 0);
cout << ans << "\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... |