Submission #1289298

#TimeUsernameProblemLanguageResultExecution timeMemory
1289298selahaddin123Paths (RMI21_paths)C++20
0 / 100
1 ms568 KiB
#include <bits/stdc++.h>
using namespace std;

const int MAXN = 2005; // adjust for subtasks
int N, K;
vector<pair<int,int>> adj[MAXN]; // {neighbor, treats}
long long dp[MAXN][105]; // dp[u][k] = max treats in subtree u picking k vertices

// merge child dp into parent dp
void merge(long long a[], long long b[]) {
    static long long tmp[105];
    for (int i = 0; i <= K; i++) tmp[i] = a[i];
    for (int i = 0; i <= K; i++) {
        for (int j = 0; j + i <= K; j++) {
            tmp[i+j] = max(tmp[i+j], a[i] + b[j]);
        }
    }
    for (int i = 0; i <= K; i++) a[i] = tmp[i];
}

// DFS to fill dp[u]
void dfs(int u, int p) {
    dp[u][0] = 0; // pick 0 vertices in subtree
    dp[u][1] = 0; // pick only u (no extra treats)
    for (auto &e : adj[u]) {
        int v = e.first;
        int c = e.second;
        if (v == p) continue;
        dfs(v, u);
        long long child[105];
        for (int i = 0; i <= K; i++) {
            if (i == 0) child[i] = 0;
            else child[i] = dp[v][i] + c; // edge contributes if pick >= 1
        }
        merge(dp[u], child);
    }
}

int main() {
    ios::sync_with_stdio(false);
    cin.tie(0);

    cin >> N >> K;
    for (int i = 0; i < N-1; i++) {
        int x, y, c;
        cin >> x >> y >> c;
        adj[x].push_back({y, c});
        adj[y].push_back({x, c});
    }

    // Simple version: compute for root 1 only
    dfs(1, 0);
    cout << dp[1][K] << "\n";

    return 0;
}
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...