/ SeriousOJ /

Record Detail

Memory Exceeded


  
# Status Time Cost Memory Cost
#1 Accepted 1ms 532.0 KiB
#2 Accepted 1ms 488.0 KiB
#3 Accepted 1ms 536.0 KiB
#4 Accepted 1ms 532.0 KiB
#5 Accepted 1ms 484.0 KiB
#6 Accepted 1ms 708.0 KiB
#7 Accepted 1ms 344.0 KiB
#8 Accepted 1ms 440.0 KiB
#9 Accepted 1ms 536.0 KiB
#10 Accepted 1ms 532.0 KiB
#11 Accepted 1ms 532.0 KiB
#12 Accepted 77ms 21.02 MiB
#13 Accepted 165ms 62.574 MiB
#14 Accepted 171ms 68.527 MiB
#15 Accepted 176ms 73.191 MiB
#16 Accepted 123ms 44.039 MiB
#17 Accepted 110ms 36.312 MiB
#18 Accepted 95ms 28.719 MiB
#19 Accepted 140ms 51.691 MiB
#20 Accepted 89ms 25.77 MiB
#21 Accepted 82ms 22.594 MiB
#22 Accepted 107ms 35.0 MiB
#23 Accepted 114ms 36.414 MiB
#24 Accepted 138ms 50.27 MiB
#25 Accepted 146ms 54.918 MiB
#26 Accepted 66ms 13.316 MiB
#27 Accepted 212ms 87.027 MiB
#28 Accepted 70ms 14.891 MiB
#29 Accepted 84ms 24.27 MiB
#30 Accepted 154ms 59.328 MiB
#31 Accepted 185ms 77.707 MiB
#32 Memory Exceeded ≥3181ms ≥1.0 GiB
#33 Memory Exceeded ≥3203ms ≥1.0 GiB

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<multiset<ll>> val(K + 1);
    
    for (int v : g[u]) {
        if (v == p) continue;
        dfs(v, u);

        // For merging later
        for (int j = 1; j <= K; ++j) {
            if (dp[v][j] != MINF)
                val[j].insert(dp[v][j]);
        }

        // 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 (int v : g[u]) {
      if (v == p) continue;
  
      for (int r = 1; r <= K - 2; ++r) {
          ll left = dp[v][r];
          ll rem = K - r - 1;
          if (left == MINF) continue;
          if (rem < 1) continue;
  
          ll rightPart = dp[v][rem];
          if (rightPart != MINF) {
              val[rem].erase(val[rem].find(rightPart));
          }
  
          if (!val[rem].empty()) {
              ll right = *val[rem].rbegin();
              ans = max(ans, left + A[u] + right);
          }
  
          if (rightPart != MINF) {
              val[rem].insert(rightPart);
          }
      }
  }
  for(int v : g[u]){
    if (v == p) continue;
    dp[v].clear();
  }

}

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:57:49
Judged At
2025-07-15 18:57:49
Judged By
Score
62
Total Time
≥3203ms
Peak Memory
≥1.0 GiB