Submission #530528

# Submission time Handle Problem Language Result Execution time Memory
530528 2022-02-25T17:48:44 Z cig32 Dostavljač (COCI18_dostavljac) C++17
14 / 140
210 ms 4296 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 sub_dp[k + 1][MAXN][2];
  for(int i=0; i<MAXN; i++) sub_dp[0][i][0] = sub_dp[0][i][1] = 0;
  sub_dp[0][1][0] = sub_dp[0][1][1] = a[node];
  for(int i=1; i<=k; i++) {
    sub_dp[i][0][0] = sub_dp[i][0][1] = 0;
    for(int j=1; j<MAXN; j++) {
      sub_dp[i][j][0] = sub_dp[i-1][j][0];
      sub_dp[i][j][1] = sub_dp[i-1][j][1];
      for(int l=1; l<j; l++) {
        sub_dp[i][j][0] = max(sub_dp[i][j][0], sub_dp[i-1][j-l-1][1] + dp[v[i]][l][0]);
        if(l+1 < j) sub_dp[i][j][1] = max(sub_dp[i][j][1], sub_dp[i-1][j-l-2][1] + dp[v[i]][l][1]); 
      }
    }
  }
  for(int i=1; i<MAXN; i++) {
    for(int j=0; j<2; j++) {
      dp[node][i][j] = sub_dp[k][i][j];
    }
  }
}
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 10 ms 464 KB Output is correct
# Verdict Execution time Memory Grader output
1 Incorrect 13 ms 600 KB Output isn't correct
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Incorrect 23 ms 696 KB Output isn't correct
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Incorrect 29 ms 904 KB Output isn't correct
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Incorrect 47 ms 1120 KB Output isn't correct
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Incorrect 67 ms 1544 KB Output isn't correct
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Incorrect 88 ms 1964 KB Output isn't correct
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Incorrect 126 ms 2728 KB Output isn't correct
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Incorrect 180 ms 3468 KB Output isn't correct
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Incorrect 210 ms 4296 KB Output isn't correct
2 Halted 0 ms 0 KB -