#include <bits/stdc++.h>   using namespace std;   const long long mod = 1e9 + 7;   long double PI = acosl(-1);   const long long inf = 3e18;   const int nmax = 1e5 + 5;   const int MAXLG = log2(nmax) + 1;   vector<int> g[nmax];   long long val[nmax], chein[nmax], path[nmax];   long long dfs(int u, int p = 0) {     chein[u] = path[u] = val[u];     vector<long long> mxcheins;     for (int v : g[u])       if (v != p) {         mxcheins.push_back(dfs(v, u));         chein[u] = max(chein[u], chein[v] + val[u]);         path[u] = max(path[u], path[v]);       }     path[u] = max(path[u], chein[u]);     sort(mxcheins.rbegin(), mxcheins.rend());     if (mxcheins.size() > 1)       path[u] = max(path[u], val[u] + mxcheins[0] + mxcheins[1]);     else if (mxcheins.size() > 0)       path[u] = max(path[u], val[u] + mxcheins[0]);     return chein[u];   }   long long dfs2(int u, long long mxpath, long long mxchein, int p = 0) {     multiset<long long> alpath;     multiset<long long> alchein;     for (int v : g[u])       if (v != p) alpath.insert(path[v]), alchein.insert(chein[v]);     alchein.insert(mxchein), alpath.insert(mxpath);     long long ret = mxpath + path[u];     for (int v : g[u])       if (v != p) {         alpath.erase(alpath.find(path[v]));         alchein.erase(alchein.find(chein[v]));         long long mxp2 = *alpath.rbegin(), mxc2 = *alchein.rbegin() + val[u];         if (alchein.size() > 1) {           long long tmp = *alchein.rbegin();           alchein.erase(alchein.find(tmp));           long long tmp2 = *alchein.rbegin();           mxp2 = max(mxp2, val[u] + tmp + tmp2);           alchein.insert(tmp);         } else if (alchein.size() > 0) {           mxp2 = max(mxp2, val[u] + *alchein.rbegin());         }         mxp2 = max(mxp2, mxc2);         ret = max(ret, dfs2(v, mxp2, mxc2, u));         alpath.insert(path[v]), alchein.insert(chein[v]);       }     return ret;   }   int main() {     ios::sync_with_stdio(false);     cin.tie(0);     int n;     cin >> n;     for (int i = 1; i <= n; i++) cin >> val[i];     for (int i = 1; i < n; i++) {       int u, v;       cin >> u >> v;       g[u].push_back(v);       g[v].push_back(u);     }     dfs(1);     cout << dfs2(1, 0, 0);   }   