SHARE
TWEET

Untitled

a guest Apr 23rd, 2019 64 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #include <bits/stdc++.h>
  2. #define ll long long
  3. using namespace std;
  4. struct node{
  5.     ll a;
  6.     vector< node* > vec;
  7. };
  8.  
  9. ll dfs(node* head, ll x){
  10.     ll sum = head->a;
  11.     for(auto i:head->vec){
  12.         sum+=dfs(i,x);
  13.     }
  14.     return max((-1)*x, sum);
  15. }
  16. int main(){
  17.     ll t,n,x,i,u,v,ans;
  18.     cin>>t;
  19.     while(t--){
  20.         cin>>n>>x;
  21.         node* tree[n+1];
  22.         for(i=1;i<=n;i++){
  23.             tree[i]=new node();
  24.             cin>>tree[i]->a;
  25.         }
  26.         for(i=0;i<n-1;i++){
  27.             cin>>u>>v;
  28.             tree[u]->vec.push_back(tree[v]);
  29.         }
  30.         ans = dfs(tree[1], x);
  31.         cout<<ans<<endl;
  32.     }
  33. }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top