func_code_string: #include <bits/stdc++.h>   using namespace std;   const int MaxN = 100010;   struct adjList {     int id, nxt;   } Lst[MaxN * 2];   int LCnt, Head[MaxN];   int N, w[MaxN], root;   void addEdge(int a, int b) {     Lst[LCnt].id = b;     Lst[LCnt].nxt = Head[a];     Head[a] = LCnt++;   }   pair<long long, long long> dfs(int id, int pre) {     vector<pair<long long, long long> > v;     for (int x = Head[id]; x != -1; x = Lst[x].nxt) {       if (Lst[x].id != pre) {         v.push_back(dfs(Lst[x].id, id));       }     }     sort(v.begin(), v.end());     long long sum = 1, rem = 0;     w[id]--;     for (int i = (int)(v.size()) - 1; i >= 0; i--) {       if (w[id] == 0) break;       w[id]--;       sum += v[i].first + 1;       rem += v[i].second;     }     if (w[id]) {       sum += 2 * min((long long)w[id], rem);       w[id] -= min((long long)w[id], rem);     }     return make_pair(sum, w[id]);   }   int main() {     while (cin >> N) {       memset(Head, -1, sizeof(Head));       LCnt = 0;       for (int i = 1; i <= N; i++) {         scanf( %d , w + i);       }       for (int i = 1; i < N; i++) {         int a, b;         scanf( %d %d , &a, &b);         addEdge(a, b);         addEdge(b, a);       }       cin >> root;       w[root]++;       cout << dfs(root, -1).first - 1 << endl;       ;     }     return 0;   }   