Advertisement
BaoJIaoPisu

Untitled

Apr 11th, 2022
78
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.19 KB | None | 0 0
  1. #include<bits/stdc++.h>
  2.  
  3. using namespace std;
  4.  
  5. using ll = long long;
  6. using ld = long double;
  7. using ull = unsigned long long;
  8.  
  9. using pii = pair<int, int>;
  10. using pll = pair<ll, ll>;
  11. using pld = pair<ld, ld>;
  12.  
  13. #define fi first
  14. #define se second
  15. #define pb push_back
  16. #define pf push_front
  17. #define mp make_pair
  18. #define ins insert
  19. #define btpc __builtin_popcount
  20. #define btclz __builtin_clz
  21.  
  22. #define sz(x) (int)(x.size());
  23. #define all(x) x.begin(), x.end()
  24. #define debug(...) " [" << #__VA_ARGS__ ": " << (__VA_ARGS__) << "] "
  25.  
  26. mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
  27.  
  28. int d4x[4] = {1, 0, -1, 0}; int d4y[4] = {0, 1, 0, -1};
  29. int d8x[8] = {0, 1, 1, 1, 0, -1, -1, -1};
  30. int d8y[8] = {1, 1, 0, -1, -1, -1, 0, 1};
  31.  
  32. template<class X, class Y>
  33.     bool minimize(X &x, const Y &y) {
  34.         if (x > y)
  35.         {
  36.             x = y;
  37.             return true;
  38.         }
  39.         return false;
  40.     }
  41. template<class X, class Y>
  42.     bool maximize(X &x, const Y &y) {
  43.         if (x < y)
  44.         {
  45.             x = y;
  46.             return true;
  47.         }
  48.         return false;
  49.     }
  50.  
  51. const int MOD = 1e9 + 7; //998244353
  52.  
  53. template<class X, class Y>
  54.     void add(X &x, const Y &y) {
  55.         x = (x + y);
  56.         if(x >= MOD) x -= MOD;
  57.     }
  58.  
  59. template<class X, class Y>
  60.     void sub(X &x, const Y &y) {
  61.         x = (x - y);
  62.         if(x < 0) x += MOD;
  63.     }
  64.  
  65. /* Author : Le Ngoc Bao Anh, 11A5, LQD High School for Gifted Student*/
  66.  
  67. const ll INF = 1e18;
  68. const int N = 1000 + 10;
  69.  
  70. vector<int> g[N];
  71. int sz[N];
  72. int w[N];
  73. ll dp[N][N][3], ndp[N][N][3];
  74.  
  75. void dfs(int u, int par) {
  76.     sz[u] = 1;
  77.     dp[u][0][1] = w[u];
  78.     for(auto v : g[u]) {
  79.         if(v != par) {
  80.             dfs(v, u);
  81.             for(int x = 0; x <= sz[u]; x++) {
  82.                 for(int y = 0; y <= sz[v]; y++) {
  83.                     maximize(ndp[u][x + y][0], dp[u][x][0] + dp[v][y][0]);
  84.                     maximize(ndp[u][x + y][1], dp[u][x][1] + dp[v][y][0]);
  85.                     maximize(ndp[u][x + y][1], dp[u][x][0] + dp[v][y][1] + w[u]);
  86.                     maximize(ndp[u][x + y + 1][2], dp[u][x][1] + dp[v][y][1]);
  87.                     maximize(ndp[u][x + y][2], dp[u][x][2] + dp[v][y][0]);
  88.                 }
  89.             }
  90.             sz[u] += sz[v];
  91.  
  92.             for(int i = 0; i <= sz[u]; i++) {
  93.                 for(int j = 0; j < 3; j++) {
  94.                     dp[u][i][j] = ndp[u][i][j];
  95.                     ndp[u][i][j] = -INF;                   
  96.                 }
  97.             }
  98.         }
  99.     }
  100.  
  101.     for(int i = 0; i <= sz[u]; i++) {
  102.         maximize(dp[u][i][0], dp[u][i][2]);
  103.         maximize(dp[u][i + 1][0], dp[u][i][1]);
  104.     }
  105. }
  106.  
  107. void solve() {
  108.     int n, k; cin >> n >> k;
  109.     for(int i = 1; i <= n; i++) cin >> w[i];
  110.    
  111.     for(int i = 1; i < n; i++) {
  112.         g[u].pb(v);
  113.         int u, v; cin >> u >> v;
  114.         g[v].pb(u);
  115.     }  
  116.  
  117.     for(int i = 1; i <= n; i++) {
  118.         for(int j = 0; j <= n; j++) {
  119.             for(int t = 0; t < 3; t++) dp[i][j][t] = ndp[i][j][t] = -INF;
  120.         }
  121.         dp[i][0][0] = ndp[i][0][0] = 0;
  122.     }
  123.  
  124.     dfs(1, 0);
  125.     ll ans = -INF;
  126.     for(int i = 1; i <= n; i++) maximize(ans, dp[i][k][0]);
  127.  
  128.     cout << ans;
  129. }
  130.  
  131. int main()
  132. {
  133.     ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0);
  134.     #ifndef ONLINE_JUDGE
  135.     freopen("input.txt", "r", stdin);
  136.     freopen("output.txt", "w", stdout);
  137.     #else
  138.     //online
  139.     #endif
  140.  
  141.     int tc = 1, ddd = 0;
  142.     // cin >> tc;
  143.     while(tc--) {
  144.         //ddd++;
  145.         //cout << "Case #" << ddd << ": ";
  146.         solve();
  147.     }
  148. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement