func_code_string: #include <bits/stdc++.h>   using namespace std;   template <class T>   string i2s(T x) {     ostringstream o;     o << x;     return o.str();   }   int n, wt[100001], root;   bool visited[100001];   pair<long long, long long> mem[100001];   vector<int> adj[100001];   pair<long long, long long> go(int u) {     if (visited[u]) return mem[u];     visited[u] = true;     pair<long long, long long>& ret = mem[u];     wt[u]--;     vector<pair<long long, long long> > tmp;     for (int i = 0; i < adj[u].size(); ++i) {       int v = adj[u][i];       if (!visited[v]) {         tmp.push_back(go(v));       }     }     sort(tmp.begin(), tmp.end(), greater<pair<long long, long long> >());     long long l = 0, r = wt[u];     for (int i = 0; i < tmp.size() && i < wt[u]; ++i) {       l += tmp[i].first + 2;       r--;     }     if (r > 0) {       for (int i = 0; i < tmp.size() && r != 0; ++i) {         if (r >= tmp[i].second) {           l += tmp[i].second * 2;           r -= tmp[i].second;         } else {           l += r * 2;           r = 0;         }       }     }     ret = make_pair(l, r);     return ret;   }   int main() {     scanf( %d , &n);     for (int i = 0; i < n; ++i) {       visited[i] = false;       scanf( %d , wt + i);     }     for (int i = 0; i < n - 1; ++i) {       int a, b;       scanf( %d %d , &a, &b);       a--, b--;       adj[a].push_back(b);       adj[b].push_back(a);     }     scanf( %d , &root);     wt[--root]++;     printf( %I64d n , go(root).first);     return 0;   }   