/ SeriousOJ /

Record Detail

Time Exceeded


  
# Status Time Cost Memory Cost
#1 Accepted 1ms 532.0 KiB
#2 Accepted 1ms 536.0 KiB
#3 Accepted 1ms 532.0 KiB
#4 Accepted 1ms 432.0 KiB
#5 Accepted 1ms 532.0 KiB
#6 Accepted 1ms 532.0 KiB
#7 Accepted 1ms 508.0 KiB
#8 Accepted 1ms 324.0 KiB
#9 Accepted 1ms 532.0 KiB
#10 Accepted 1ms 380.0 KiB
#11 Accepted 1ms 532.0 KiB
#12 Accepted 102ms 21.012 MiB
#13 Accepted 223ms 62.414 MiB
#14 Accepted 251ms 68.586 MiB
#15 Accepted 264ms 73.188 MiB
#16 Accepted 208ms 44.117 MiB
#17 Accepted 158ms 36.395 MiB
#18 Accepted 116ms 28.77 MiB
#19 Accepted 226ms 51.684 MiB
#20 Accepted 111ms 25.773 MiB
#21 Accepted 103ms 22.656 MiB
#22 Accepted 135ms 34.773 MiB
#23 Accepted 161ms 36.316 MiB
#24 Accepted 201ms 50.027 MiB
#25 Accepted 231ms 54.66 MiB
#26 Accepted 105ms 13.52 MiB
#27 Accepted 272ms 86.941 MiB
#28 Accepted 85ms 15.02 MiB
#29 Accepted 111ms 24.195 MiB
#30 Accepted 216ms 59.34 MiB
#31 Accepted 255ms 77.672 MiB
#32 Time Exceeded ≥3116ms ≥273.285 MiB
#33 Time Exceeded ≥3110ms ≥196.969 MiB

Code

#include <bits/stdc++.h>
using namespace std;

typedef long long ll;
const ll MINF = -1e18;

int N, K;
vector<ll> A;
vector<vector<int>> g;
vector<vector<ll>> dp;  // dp[i][j] = max weight path in subtree of i with exactly j nodes
ll ans = 0;

void dfs(int u, int p) {
    dp[u][1] = A[u]; // base case

    vector<vector<ll>> child_dp;  // child_dp[c][j] = dp for each child
    vector<int> children;

    for (int v : g[u]) {
        if (v == p) continue;
        dfs(v, u);
        children.push_back(v);

        // Single chain path: extend chain by taking child
        for (int j = 1; j < K; ++j) {
            if (dp[v][j] != MINF)
                dp[u][j + 1] = max(dp[u][j + 1], dp[v][j] + A[u]);
        }
    }

    ans = max(ans, dp[u][K]);

    // For each length j, collect dp for all children
    vector<vector<ll>> all_dp(K + 1);
    for (int v : children) {
        for (int j = 1; j <= K; ++j) {
            if (dp[v][j] != MINF) {
                all_dp[j].push_back(dp[v][j]);
            }
        }
    }

    // For each length j, build prefix and suffix max
    vector<vector<ll>> pre(K + 1), suf(K + 1);
    for (int j = 1; j <= K; ++j) {
        int sz = all_dp[j].size();
        if (sz == 0) continue;

        pre[j].resize(sz);
        suf[j].resize(sz);

        pre[j][0] = all_dp[j][0];
        for (int i = 1; i < sz; ++i)
            pre[j][i] = max(pre[j][i - 1], all_dp[j][i]);

        suf[j][sz - 1] = all_dp[j][sz - 1];
        for (int i = sz - 2; i >= 0; --i)
            suf[j][i] = max(suf[j][i + 1], all_dp[j][i]);
    }

    // For each child, try combining with other children
    for (int idx = 0; idx < (int)children.size(); ++idx) {
        int v = children[idx];
        for (int r = 1; r <= K - 2; ++r) {
            ll left = dp[v][r];
            ll rem = K - r - 1;
            if (left == MINF || rem < 1) continue;
            if (dp[v][rem] == MINF && all_dp[rem].size() == 0) continue;

            int pos = -1;  // find the position of dp[v][rem] in all_dp[rem]
            for (int i = 0; i < (int)all_dp[rem].size(); ++i) {
                if (all_dp[rem][i] == dp[v][rem]) {
                    pos = i;
                    break;
                }
            }

            ll otherMax = MINF;
            int sz = all_dp[rem].size();
            if (sz == 1 && pos != -1) {
                otherMax = MINF;
            } else if (sz > 0) {
                if (pos == -1) {
                    otherMax = suf[rem][0];
                } else {
                    if (pos > 0) otherMax = max(otherMax, pre[rem][pos - 1]);
                    if (pos + 1 < sz) otherMax = max(otherMax, suf[rem][pos + 1]);
                }
            }

            if (otherMax != MINF) {
                ans = max(ans, left + A[u] + otherMax);
            }
        }
    }
}

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

    cin >> N >> K;
    A.resize(N + 1);
    g.resize(N + 1);
    dp.assign(N + 1, vector<ll>(K + 1, MINF));

    for (int i = 1; i <= N; ++i) {
        cin >> A[i];
    }

    for (int i = 1; i < N; ++i) {
        int u, v;
        cin >> u >> v;
        g[u].push_back(v);
        g[v].push_back(u);
    }

    dfs(1, 0);

    cout << ans << '\n';
    return 0;
}

Information

Submit By
Type
Submission
Problem
P1160 F1. Max path sum (Easy Version)
Language
C++17 (G++ 13.2.0)
Submit At
2025-07-15 18:51:45
Judged At
2025-07-15 18:51:45
Judged By
Score
62
Total Time
≥3116ms
Peak Memory
≥273.285 MiB