이 제출은 이전 버전의 oj.uz에서 채점하였습니다. 현재는 제출 당시와는 다른 서버에서 채점을 하기 때문에, 다시 제출하면 결과가 달라질 수도 있습니다.
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
const int MAXN = 1e5 + 5;
int n, k;
ll mx[MAXN];
pair <ll, int> dp[MAXN];
vector <pair <int, ll> > Adj[MAXN];
void DFS(int node, int p = -1)
{
    for(auto x : Adj[node])
    {
        if(x.first == p)
        {
            continue;
        }
        DFS(x.first, node);
        dp[x.first].first += x.second;
        dp[node] = max(dp[node], dp[x.first]);
    }
    if(dp[node].second == 0)
    {
        dp[node] = make_pair(0, node);
    }
}
int main()
{
    ios_base::sync_with_stdio(false);
    cin.tie(0);
    cout.tie(0);
    cin >> n >> k;
    if(n > MAXN)
    {
        return 0;
    }
    for(int i = 1; i < n; i++)
    {
        int u, v, c;
        cin >> u >> v >> c;
        Adj[u].emplace_back(v, c);
        Adj[v].emplace_back(u, c);
    }
    for(int i = 1; i <= n; i++)
    {
        for(int j = 1; j <= n; j++)
        {
            mx[j] = 0;
            dp[j] = make_pair(0, 0);
        }
        DFS(i);
        for(int j = 1; j <= n; j++)
        {
            mx[dp[j].second] = max(mx[dp[j].second], dp[j].first);
        }
        sort(mx + 1, mx + n + 1, greater <ll> ());
        ll ans = 0;
        for(int j = 1; j <= k; j++)
        {
            ans += mx[j];
        }
        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... | 
| # | Verdict  | Execution time | Memory | Grader output | 
|---|
| Fetching results... | 
| # | Verdict  | Execution time | Memory | Grader output | 
|---|
| Fetching results... |