Submission #941730

# Submission time Handle Problem Language Result Execution time Memory
941730 2024-03-09T11:05:55 Z vjudge1 Chase (CEOI17_chase) C++17
70 / 100
227 ms 97572 KB
#include <bits/stdc++.h>
using namespace std;

using ll = long long;
#define int ll

#define endl '\n'
#define pb push_back
using pi = pair<int, int>;
template<class T> bool ckmin(T& a, const T& b) { return b < a ? a = b, 1 : 0; }
template<class T> bool ckmax(T& a, const T& b) { return a < b ? a = b, 1 : 0; }

/*
dp[i][j] - subarborele lui i daca mai am j de aruncat
*/

const int INF = 1e16;

int32_t main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  
  int n, k;
  cin >> n >> k;
  
  vector<int> a(n + 1);
  for (int i = 1; i <= n; ++i) cin >> a[i];
  
  vector<vector<int>> adj(n + 1);
  for (int i = 1; i < n; ++i) {
    int u, v;
    cin >> u >> v;
    adj[u].pb(v);
    adj[v].pb(u);
  }
  
  vector<vector<int>> dp(n + 1, vector<int>(k + 1));
  function<void(int, int)> dfs = [&](int u, int p) {
    int S = 0;
    for (int v : adj[u]) {
      if (v == p) continue;
      
      dfs(v, u);
      S += a[v];
    }
    
    for (int v : adj[u]) {
      if (v == p) continue;
      
      for (int i = 0; i <= k; ++i) {
        // colectez
        if (i > 0) dp[u][i] = max(dp[u][i], S + max(dp[v][i - 1], 0LL));
        
        // nu colectez
        dp[u][i] = max(dp[u][i], dp[v][i]);
      }
    }
  };
  
  int ans = 0;
  if (n <= 1000) {
    for (int r = 1; r <= n; ++r) {
      for (int i = 1; i <= n; ++i) {
        for (int j = 0; j <= k; ++j) dp[i][j] = -INF;
      }
      dfs(r, 0);
      ans = max(ans, dp[r][k]);
    }
  } else {
    dfs(1, 0);
    ans = dp[1][k];
  }
  
  cout << ans;
}
# Verdict Execution time Memory Grader output
1 Correct 0 ms 348 KB Output is correct
2 Correct 0 ms 348 KB Output is correct
3 Correct 0 ms 348 KB Output is correct
4 Correct 0 ms 348 KB Output is correct
5 Correct 0 ms 348 KB Output is correct
6 Correct 0 ms 348 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 0 ms 348 KB Output is correct
2 Correct 0 ms 348 KB Output is correct
3 Correct 0 ms 348 KB Output is correct
4 Correct 0 ms 348 KB Output is correct
5 Correct 0 ms 348 KB Output is correct
6 Correct 0 ms 348 KB Output is correct
7 Correct 220 ms 1376 KB Output is correct
8 Correct 28 ms 604 KB Output is correct
9 Correct 21 ms 600 KB Output is correct
10 Correct 227 ms 1112 KB Output is correct
11 Correct 80 ms 780 KB Output is correct
12 Correct 37 ms 600 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 111 ms 95556 KB Output is correct
2 Correct 114 ms 97572 KB Output is correct
3 Correct 75 ms 91592 KB Output is correct
4 Correct 47 ms 14672 KB Output is correct
5 Correct 120 ms 91472 KB Output is correct
6 Correct 121 ms 91472 KB Output is correct
7 Correct 122 ms 91728 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 0 ms 348 KB Output is correct
2 Correct 0 ms 348 KB Output is correct
3 Correct 0 ms 348 KB Output is correct
4 Correct 0 ms 348 KB Output is correct
5 Correct 0 ms 348 KB Output is correct
6 Correct 0 ms 348 KB Output is correct
7 Correct 220 ms 1376 KB Output is correct
8 Correct 28 ms 604 KB Output is correct
9 Correct 21 ms 600 KB Output is correct
10 Correct 227 ms 1112 KB Output is correct
11 Correct 80 ms 780 KB Output is correct
12 Correct 37 ms 600 KB Output is correct
13 Correct 111 ms 95556 KB Output is correct
14 Correct 114 ms 97572 KB Output is correct
15 Correct 75 ms 91592 KB Output is correct
16 Correct 47 ms 14672 KB Output is correct
17 Correct 120 ms 91472 KB Output is correct
18 Correct 121 ms 91472 KB Output is correct
19 Correct 122 ms 91728 KB Output is correct
20 Incorrect 123 ms 91484 KB Output isn't correct
21 Halted 0 ms 0 KB -