Submission #530773

# Submission time Handle Problem Language Result Execution time Memory
530773 2022-02-26T18:06:55 Z cig32 Dostavljač (COCI18_dostavljac) C++17
140 / 140
693 ms 4444 KB
#include "bits/stdc++.h"
using namespace std;
mt19937_64 rng((int)std::chrono::steady_clock::now().time_since_epoch().count());
const int MAXN = 505;
const int MOD = 1e9 + 7;
#define int long long
int rnd(int x, int y) {
  int u = uniform_int_distribution<int>(x, y)(rng); return u;
}
int a[MAXN];
vector<int> adj[MAXN];
int dp[MAXN][MAXN][2]; // k = 1: returned to the root
void dfs(int node, int prv) {
  vector<int> v;
  v.push_back(-1);
  for(int x: adj[node]) {
    if(x != prv) {
      dfs(x, node);
      v.push_back(x);
    }
  }
  int k = v.size() - 1;
  int pre_dp[k + 1][MAXN];
  for(int i=0; i<MAXN; i++) pre_dp[0][i] = 0;
  pre_dp[0][1] = a[node];
  for(int i=1; i<=k; i++) {
    pre_dp[i][0] = 0;
    for(int j=1; j<MAXN; j++) {
      pre_dp[i][j] = pre_dp[i-1][j];
      for(int l=1; l+1<j; l++) {
        pre_dp[i][j] = max(pre_dp[i][j], pre_dp[i-1][j-l-2] + dp[v[i]][l][1]); 
      }
    }
  }
  int suf_dp[k + 2][MAXN];
  for(int i=0; i<MAXN; i++) suf_dp[k + 1][i] = 0;
  for(int i=k; i>=1; i--) {
    suf_dp[i][0] = 0;
    for(int j=1; j<MAXN; j++) {
      suf_dp[i][j] = suf_dp[i+1][j];
      for(int l=1; l+1<j; l++) {
        suf_dp[i][j] = max(suf_dp[i][j], suf_dp[i+1][j-l-2] + dp[v[i]][l][1]); 
      }
    }
  }
  int et[k + 2][MAXN];
  for(int i=0; i<MAXN; i++) et[k + 1][i] = 0;
  for(int i=0; i<=k; i++) {
    for(int j=0; j<MAXN; j++) {
      et[i][j] = 0;
      for(int l=1; l<j; l++) {
        et[i][j] = max(et[i][j], suf_dp[i+1][j-l-1] + dp[v[i]][l][0]);
      }
    }
  }
  for(int i=0; i<MAXN; i++) {
    for(int j=0; j<=k; j++) {
      for(int l=0; l<=i; l++) {
        dp[node][i][1] = max(dp[node][i][1], pre_dp[j][l]);
        dp[node][i][0] = max(dp[node][i][0], pre_dp[j][l] + max(suf_dp[j + 1][i - l], et[j + 1][i - l]));
      }
    }
  }
}
void solve(int tc) {
  int n, m;
  cin >> n >> m;
  for(int i=1; i<=n; i++) cin >> a[i];
  for(int i=1; i<n; i++) {
    int a, b;
    cin >> a >> b;
    adj[a].push_back(b);
    adj[b].push_back(a);
  }
  dfs(1, -1);
  int ans = 0;
  for(int i=1; i<=m; i++) {
    for(int j=0; j<2; j++) {
      ans = max(ans, dp[1][i][j]);
    }
  }
  cout << ans << "\n";
}
 
int32_t main(){
  ios::sync_with_stdio(0); cin.tie(0);
  int t = 1; //cin >> t;
  for(int i=1; i<=t; i++) solve(i);
}  
# Verdict Execution time Memory Grader output
1 Correct 27 ms 456 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 41 ms 588 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 76 ms 808 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 101 ms 964 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 149 ms 1212 KB Output is correct
2 Correct 140 ms 1092 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 207 ms 1592 KB Output is correct
2 Correct 213 ms 1652 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 272 ms 1932 KB Output is correct
2 Correct 273 ms 1860 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 413 ms 2752 KB Output is correct
2 Correct 412 ms 2884 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 561 ms 3548 KB Output is correct
2 Correct 565 ms 3496 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 686 ms 4424 KB Output is correct
2 Correct 693 ms 4444 KB Output is correct