#include <bits/stdc++.h>   using namespace std;   const int MAXN = 100005;   vector<int> graph[MAXN];   int values[MAXN];   long long dp[MAXN];   int leftValue[MAXN];   priority_queue<pair<long long, int>, vector<pair<long long, int> >,                  greater<pair<long long, int> > >       pq;   void dfs(int u, int prev) {     for (int v : graph[u]) {       if (v == prev || values[v] == 0) {         continue;       }       values[v]--;       dfs(v, u);     }     while (!pq.empty()) {       pq.pop();     }     for (int v : graph[u]) {       if (v == prev) {         continue;       }       pq.push({dp[v], v});     }     int currentLeft = values[u];     while (!pq.empty()) {       if (pq.size() <= values[u]) {         dp[u] += pq.top().first + 1;         values[pq.top().second] = leftValue[pq.top().second];         currentLeft--;       }       pq.pop();     }     for (int v : graph[u]) {       if (v == prev || values[v] == 0) {         continue;       }       int pick = min(currentLeft, values[v]);       currentLeft -= pick;       values[v] -= pick;       dp[u] += 2 * pick;     }     dp[u]++;     leftValue[u] = currentLeft;   }   int main() {     cin.sync_with_stdio(false);     cin.tie(0);     int n;     cin >> n;     for (int i = 1; i <= n; i++) {       cin >> values[i];     }     for (int i = 1; i < n; i++) {       int in, out;       cin >> in >> out;       graph[in].push_back(out);       graph[out].push_back(in);     }     int root;     cin >> root;     dfs(root, -1);     cout << dp[root] - 1 << endl;     cin.get();     cin.get();     return 0;   }   