Advertisement
Guest User

Untitled

a guest
Nov 23rd, 2017
63
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.04 KB | None | 0 0
  1. #include <iostream>
  2. #include <string>
  3. #include <stdio.h>
  4. #include <vector>
  5. #include <algorithm>
  6. #include <string.h>
  7. #include <map>
  8.  
  9. using namespace std;
  10.  
  11. #define ll long long
  12. #define LL long long
  13. #define sl(n) scanf("%lld", &n)
  14. #define sf(n) scanf("%lf", &n)
  15. #define si(n) scanf("%d", &n)
  16. #define ss(n) scanf("%s", n)
  17. #define pii pair <int, int>
  18. #define pll pair <ll, ll>
  19. #define plp pair <int, pll >
  20. #define pb push_back
  21. #define inf (1LL<<60)
  22. #define fr first
  23. #define sc second
  24. #define EPS 0.0000000001
  25.  
  26.  
  27. vector <ll> ed[210];
  28. ll dp[210][210][2], a[210];
  29.  
  30. ll n, k;
  31.  
  32. void dfs(ll u, ll par)
  33. {
  34. for (ll i = 0; i <= k; i++)
  35. dp[u][i][0] = dp[u][i][1] = a[u];
  36.  
  37. for (ll i = 0; i < ed[u].size(); i++)
  38. {
  39. ll v = ed[u][i];
  40.  
  41. if (v != par)
  42. {
  43. dfs(v, u);
  44.  
  45. for (ll j = k; j >= 1; j--)
  46. {
  47. for (ll lj = 1; lj <= j; lj++)
  48. {
  49. if (lj >= 2)
  50. dp[u][j][0] = max(dp[u][j][0], dp[u][j-lj][0] + dp[v][lj-2][0]);
  51.  
  52. dp[u][j][1] = max(dp[u][j][1], dp[u][j-lj][0] + dp[v][lj-1][1]);
  53.  
  54. if (lj >= 2)
  55. dp[u][j][1] = max(dp[u][j][1], dp[u][j-lj][1] + dp[v][lj-2][0]);
  56. }
  57. }
  58. }
  59. }
  60. }
  61.  
  62. int main ()
  63. {
  64. // #ifndef zeron
  65. // freopen("inl.txt", "r", stdin);
  66. // freopen("outu.txt", "w", stdout);
  67. // #endif
  68. // ios_base::sync_with_stdio(0); // no printf/scanf must be present
  69. ll cs, t, i, j, x, y, z, ans, q;
  70.  
  71. for (cs = 1; sl(n) != EOF; cs++)
  72. {
  73. sl(k);
  74. for (i = 0; i <= n; i++)
  75. {
  76. ed[i].clear();
  77. }
  78.  
  79. for (i = 1; i <= n; i++)
  80. sl(a[i]);
  81.  
  82. for (i = 1; i < n; i++)
  83. {
  84. sl(x);
  85. sl(y);
  86.  
  87. ed[x].pb(y);
  88. ed[y].pb(x);
  89. }
  90.  
  91. dfs(1, 0);
  92.  
  93. ans = max(dp[1][k][0], dp[1][k][1]);
  94.  
  95. printf("%lld\n", ans);
  96. }
  97.  
  98. return 0;
  99. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement