Submission #673733

# Submission time Handle Problem Language Result Execution time Memory
673733 2022-12-21T20:43:58 Z MilosMilutinovic Dostavljač (COCI18_dostavljac) C++14
140 / 140
896 ms 14092 KB
/**
 *    author:  wxhtzdy
 *    created: 21.12.2022 21:21:48
**/
#include <bits/stdc++.h>

using namespace std;

int main() {
  ios::sync_with_stdio(false);
  cin.tie(0);  
  int n, m;
  cin >> n >> m;
  vector<int> a(n);
  for (int i = 0; i < n; i++) {
    cin >> a[i];
  }
  vector<vector<int>> g(n);
  for (int i = 0; i < n - 1; i++) {
    int u, v;
    cin >> u >> v;
    --u; --v;
    g[u].push_back(v);          
    g[v].push_back(u);
  }
  vector<vector<vector<int>>> dp(n, vector<vector<int>>(m + 1, vector<int>(2)));
  function<void(int, int)> Solve = [&](int v, int pv) {
    dp[v][1][0] = a[v];
    dp[v][1][1] = a[v];
    for (int u : g[v]) {
      if (u == pv) {
        continue;
      }
      Solve(u, v);
      for (int i = m - 1; i >= 0; i--) {
        for (int j = 0; j <= m; j++) {
          int new_i = j + i + 2;
          if (new_i <= m) {
            dp[v][new_i][1] = max(dp[v][new_i][1], dp[v][i][1] + dp[u][j][0]);
            dp[v][new_i][0] = max(dp[v][new_i][0], dp[v][i][0] + dp[u][j][0]);
          }
          if (new_i - 1 <= m) {
            dp[v][new_i - 1][1] = max(dp[v][new_i - 1][1], dp[v][i][0] + dp[u][j][1]);
          }
        }
      }
    }
    /* for (int i = m - 1; i >= 0; i--) {
      dp[v][i + 1][0] = max(dp[v][i + 1][0], dp[v][i][0] + a[v]);
      dp[v][i + 1][1] = max(dp[v][i + 1][1], dp[v][i][1] + a[v]);
    } */             
  };
  Solve(0, 0);
  int ans = 0;
  for (int i = 0; i <= m; i++) {
    ans = max(ans, dp[0][i][0]);
    ans = max(ans, dp[0][i][1]);
  }
  cout << ans << '\n';
  return 0;
}
# Verdict Execution time Memory Grader output
1 Correct 1 ms 212 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 320 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 5 ms 608 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 468 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 25 ms 1348 KB Output is correct
2 Correct 8 ms 852 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 8 ms 980 KB Output is correct
2 Correct 133 ms 3156 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 237 ms 4780 KB Output is correct
2 Correct 71 ms 2776 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 80 ms 3480 KB Output is correct
2 Correct 534 ms 8572 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 460 ms 9164 KB Output is correct
2 Correct 184 ms 5844 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 896 ms 14092 KB Output is correct
2 Correct 85 ms 4436 KB Output is correct