func_code_string: #include <bits/stdc++.h>   using namespace std;   vector<int> g[100100];   int aa[100100];   long long dfs(int u, int fa) {     vector<long long> tmp;     for (int i = 0; i < g[u].size(); i++) {       int v = g[u][i];       if (v != fa && aa[v] - 1 >= 0) {         aa[v] -= 1;         tmp.push_back(dfs(v, u));       }     }     sort(tmp.begin(), tmp.end());     int cc = tmp.size();     long long ans = 0;     for (int i = aa[u]; i >= 1; i--) {       if (cc > 0) {         ans += tmp[--cc] + 2;         aa[u]--;       } else {         break;       }     }     for (int i = 0; i < g[u].size(); i++) {       int v = g[u][i];       if (v != fa) {         int tmp = min(aa[u], aa[v]);         ans += tmp * 2;         aa[u] -= tmp;         aa[v] -= tmp;       }     }     return ans;   }   int main() {     int n;     scanf( %d , &n);     for (int i = 1; i <= n; i++) {       scanf( %d , &aa[i]);     }     for (int i = 0; i < n - 1; i++) {       int u, v;       scanf( %d%d , &u, &v);       g[u].push_back(v);       g[v].push_back(u);     }     int s;     scanf( %d , &s);     printf( %I64d n , dfs(s, -1));     return 0;   }   