/ SeriousOJ /

Record Detail

Accepted


  
# Status Time Cost Memory Cost
#1 Accepted 1ms 532.0 KiB
#2 Accepted 1ms 532.0 KiB
#3 Accepted 1ms 400.0 KiB
#4 Accepted 1ms 532.0 KiB
#5 Accepted 1ms 516.0 KiB
#6 Accepted 1ms 396.0 KiB
#7 Accepted 1ms 436.0 KiB
#8 Accepted 1ms 320.0 KiB
#9 Accepted 1ms 560.0 KiB
#10 Accepted 1ms 380.0 KiB
#11 Accepted 1ms 532.0 KiB
#12 Accepted 85ms 6.621 MiB
#13 Accepted 63ms 6.52 MiB
#14 Accepted 69ms 6.52 MiB
#15 Accepted 65ms 6.52 MiB
#16 Accepted 85ms 6.457 MiB
#17 Accepted 59ms 6.598 MiB
#18 Accepted 50ms 6.562 MiB
#19 Accepted 66ms 6.598 MiB
#20 Accepted 54ms 6.566 MiB
#21 Accepted 49ms 6.48 MiB
#22 Accepted 63ms 6.52 MiB
#23 Accepted 54ms 6.574 MiB
#24 Accepted 87ms 6.562 MiB
#25 Accepted 61ms 6.52 MiB
#26 Accepted 45ms 6.52 MiB
#27 Accepted 70ms 6.574 MiB
#28 Accepted 47ms 6.523 MiB
#29 Accepted 49ms 6.574 MiB
#30 Accepted 63ms 6.52 MiB
#31 Accepted 67ms 6.52 MiB
#32 Accepted 525ms 257.34 MiB
#33 Accepted 386ms 180.77 MiB
#34 Accepted 57ms 6.52 MiB
#35 Accepted 57ms 6.52 MiB
#36 Accepted 50ms 6.52 MiB
#37 Accepted 46ms 6.57 MiB
#38 Accepted 74ms 6.52 MiB
#39 Accepted 52ms 6.52 MiB
#40 Accepted 52ms 6.52 MiB
#41 Accepted 58ms 6.52 MiB
#42 Accepted 83ms 6.523 MiB
#43 Accepted 68ms 6.52 MiB
#44 Accepted 65ms 6.566 MiB
#45 Accepted 72ms 6.5 MiB
#46 Accepted 52ms 6.578 MiB
#47 Accepted 87ms 6.566 MiB
#48 Accepted 70ms 6.562 MiB
#49 Accepted 47ms 6.52 MiB
#50 Accepted 117ms 6.566 MiB

Code

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
 
const ll MINF = -1e18;
 
int N, K;
vector<ll> val;
vector<vector<int>> adj;
 
// For each node u, dp[u][d] = maximum sum of a downward path starting at u with exactly d nodes.
 
// The DFS returns a vector dp (size K+1, where index 1..K are used).
// 'ans' is updated globally with any valid path of exactly K nodes.
vector<ll> dfs(int u, int parent, ll &ans) {
    vector<ll> dp(K+1, MINF);
    dp[1] = val[u];  // The trivial path that is just the node u.
    
    // Process every child v of u.
    for (int v : adj[u]) {
        if(v == parent) continue;
        vector<ll> childDP = dfs(v, u, ans);
        
        // --- Combination step ---
        // Before merging the new child's information into dp[u], try to combine
        // a branch already in dp[u] (coming from a different child or u itself)
        // with a new branch from v.
        // A branch from child v gives (when attached to u):
        //   new_branch_length = L+1, with sum = val[u] + childDP[L]
        // Now, if dp[u] already has a branch of length i (which already includes u),
        // combining the two branches (and subtracting one copy of u) gives:
        //   total length = i + (L+1) - 1 = i + L.
        // To have exactly K nodes we require: i + L = K.
        for (int i = 1; i < K; i++) {
            int L = K - i; // L must be at least 1 and at most K-1.
            if(L >= 1 && L <= K-1) {
                if(dp[i] != MINF && childDP[L] != MINF) {
                    ans = max(ans, dp[i] + childDP[L]);
                }
            }
        }
        
        // --- Merging step ---
        // Now update dp[u] with the new downward branches coming from v.
        // For every possible branch from v of length L (1 <= L <= K-1),
        // we can form a branch from u of length L+1 with sum = val[u] + childDP[L].
        vector<ll> newDP = dp;
        for (int L = 1; L <= K-1; L++) {
            if(childDP[L] != MINF) {
                newDP[L+1] = max(newDP[L+1], val[u] + childDP[L]);
            }
        }
        dp = newDP;
    }
    // Also update the answer with any downward branch from u that has exactly K nodes.
    ans = max(ans, dp[K]);
    return dp;
}
 
int main(){
    ios::sync_with_stdio(false);
    cin.tie(nullptr);
 
    cin >> N >> K;
    val.resize(N+1);
    for (int i = 1; i <= N; i++){
        cin >> val[i];
    }
    adj.resize(N+1);
    for (int i = 1; i <= N-1; i++){
        int u, v;
        cin >> u >> v;
        adj[u].push_back(v);
        adj[v].push_back(u);
    }
    
    ll ans = MINF;
    dfs(1, -1, ans);
    cout << (ans == MINF ? 0 : ans) << "\n";
    return 0;
}

Information

Submit By
Type
Submission
Problem
P1160 Max path sum (Easy Version)
Contest
Brain Booster #8
Language
C++17 (G++ 13.2.0)
Submit At
2025-02-17 16:57:50
Judged At
2025-02-17 16:57:50
Judged By
Score
100
Total Time
525ms
Peak Memory
257.34 MiB