This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#include <bits/stdc++.h>
using namespace std;
#define ll long long
const int N = 100'005;
vector<array<int, 2>> adj[N];
int n, k;
ll vals[N];
pair<ll, int> dfs(int i, int par, int parc) {
pair<ll, int> mx = {-1e18, 0};
for (auto [nxt, w] : adj[i]) {
if (nxt == par) continue;
mx = max(mx, dfs(nxt, i, w));
}
if (adj[i].size() == 1) {
vals[i] = parc;
return {parc, i};
}
vals[mx.second] += parc;
mx.first += parc;
return mx;
}
int main() {
ios_base::sync_with_stdio(0), cin.tie(0);
cin >> n >> k;
for (int i = 1, u, v, w; i < n; i++) {
cin >> u >> v >> w;
adj[u].push_back({v, w});
adj[v].push_back({u, w});
}
for (int i = 1; i <= n; i++) {
for (int j = 1; j <= n; j++) {
vals[j] = 0;
}
dfs(i, 0, 0);
sort(vals + 1, vals + n + 1);
reverse(vals + 1, vals + n + 1);
ll sum = 0;
for (int j = 1; j <= k; j++) {
sum += vals[j];
}
cout << sum << '\n';
}
return 0;
}
# | 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... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |