fork(6) download
  1. #include <bits/stdc++.h>
  2.  
  3. using namespace std;
  4.  
  5. #define all(x) begin(x), end(x)
  6. #define int int64_t
  7.  
  8. const int maxn = 1e5 + 42;
  9. vector<int> g[maxn];
  10. int x;
  11. int A[maxn];
  12.  
  13. void init() {
  14. for(int i = 0; i < maxn; i++) {
  15. g[i].clear();
  16. }
  17. }
  18.  
  19. int dfs(int v = 1, int p = 1) {
  20. int res = 0;
  21. for(auto u: g[v]) {
  22. if(u != p) {
  23. res += dfs(u, v);
  24. }
  25. }
  26. return max(A[v] + res, -x);
  27. }
  28.  
  29. void solve() {
  30. init();
  31. int n;
  32. cin >> n >> x;
  33. for(int i = 1; i <= n; i++) {
  34. cin >> A[i];
  35. }
  36. for(int i = 1; i < n; i++) {
  37. int x, y;
  38. cin >> x >> y;
  39. g[x].push_back(y);
  40. g[y].push_back(x);
  41. }
  42. cout << dfs() << endl;
  43. }
  44.  
  45. signed main() {
  46. //freopen("input.txt", "r", stdin);
  47. //freopen("output.txt", "w", stdout);
  48. ios::sync_with_stdio(0);
  49. cin.tie(0);
  50. int t;
  51. cin >> t;
  52. while(t--) {
  53. solve();
  54. }
  55. return 0;
  56. }
Success #stdin #stdout 0s 18360KB
stdin
Standard input is empty
stdout
Standard output is empty