/ SeriousOJ /

Record Detail

Accepted


  
# Status Time Cost Memory Cost
#1 Accepted 1ms 532.0 KiB
#2 Accepted 1ms 532.0 KiB
#3 Accepted 1ms 324.0 KiB
#4 Accepted 1ms 532.0 KiB
#5 Accepted 1ms 348.0 KiB
#6 Accepted 1ms 532.0 KiB
#7 Accepted 1ms 480.0 KiB
#8 Accepted 1ms 436.0 KiB
#9 Accepted 1ms 572.0 KiB
#10 Accepted 1ms 532.0 KiB
#11 Accepted 1ms 532.0 KiB
#12 Accepted 170ms 50.168 MiB
#13 Accepted 227ms 79.293 MiB
#14 Accepted 172ms 50.324 MiB
#15 Accepted 178ms 54.77 MiB
#16 Accepted 127ms 28.773 MiB
#17 Accepted 165ms 47.359 MiB
#18 Accepted 157ms 44.27 MiB
#19 Accepted 204ms 68.773 MiB
#20 Accepted 130ms 30.27 MiB
#21 Accepted 228ms 79.285 MiB
#22 Accepted 94ms 15.102 MiB
#23 Accepted 192ms 61.02 MiB
#24 Accepted 155ms 41.02 MiB
#25 Accepted 115ms 24.191 MiB
#26 Accepted 90ms 13.672 MiB
#27 Accepted 113ms 22.859 MiB
#28 Accepted 78ms 11.895 MiB
#29 Accepted 215ms 71.621 MiB
#30 Accepted 152ms 36.562 MiB
#31 Accepted 198ms 62.52 MiB
#32 Accepted 210ms 68.773 MiB
#33 Accepted 204ms 65.66 MiB
#34 Accepted 98ms 16.52 MiB
#35 Accepted 135ms 27.281 MiB
#36 Accepted 103ms 21.184 MiB
#37 Accepted 202ms 65.562 MiB
#38 Accepted 110ms 22.77 MiB
#39 Accepted 173ms 48.656 MiB
#40 Accepted 133ms 28.898 MiB
#41 Accepted 214ms 70.285 MiB
#42 Accepted 73ms 21.199 MiB
#43 Accepted 80ms 24.066 MiB
#44 Accepted 82ms 16.441 MiB
#45 Accepted 80ms 16.27 MiB
#46 Accepted 78ms 16.273 MiB
#47 Accepted 1338ms 567.0 MiB
#48 Accepted 1187ms 326.766 MiB
#49 Accepted 731ms 329.062 MiB
#50 Accepted 121ms 53.957 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<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);
          }
      }
  }

}

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
P1161 F2. Max path sum (Hard Version)
Language
C++17 (G++ 13.2.0)
Submit At
2025-07-15 18:45:32
Judged At
2025-07-15 18:45:32
Judged By
Score
100
Total Time
1338ms
Peak Memory
567.0 MiB