voiddfs(int x, int fa){ f[x].add(a[x]); for(int i = 0, ed = g[x].size(); i < ed; ++i) { int v = g[x][i]; if(v == fa) continue; dfs(v, x); f[x].add(f[v].max() + a[x]); ans[x].add(ans[v].max()); } ans[x].add(f[x].nax() ? f[x].max() + f[x].nax() - a[x] : f[x].max()); }
ll res = 0; voiddp(int x, int fa){ for(int i = 0, ed = g[x].size(); i < ed; ++i) { int v = g[x][i]; if(v == fa) continue;
/* Remove v from x */ ans[x].del(f[x].nax() ? f[x].max() + f[x].nax() - a[x] : f[x].max()); f[x].del(f[v].max() + a[x]); ans[x].del(ans[v].max()); ans[x].add(f[x].nax() ? f[x].max() + f[x].nax() - a[x] : f[x].max());
/* Calculate answer between x and v */ res = std::max(res, ans[x].max() + ans[v].max());
/* Add x to v */ ans[v].del(f[v].nax() ? f[v].max() + f[v].nax() - a[v] : f[v].max()); f[v].add(f[x].max() + a[v]); ans[v].add(ans[x].max()); ans[v].add(f[v].nax() ? f[v].max() + f[v].nax() - a[v] : f[v].max());
dp(v, x);
/* Remove x from v */ ans[v].del(f[v].nax() ? f[v].max() + f[v].nax() - a[v] : f[v].max()); ans[v].del(ans[x].max()); f[v].del(f[x].max() + a[v]); ans[v].add(f[v].nax() ? f[v].max() + f[v].nax() - a[v] : f[v].max());
/* Add v to x */ ans[x].del(f[x].nax() ? f[x].max() + f[x].nax() - a[x] : f[x].max()); ans[x].add(ans[v].max()); f[x].add(f[v].max() + a[x]); ans[x].add(f[x].nax() ? f[x].max() + f[x].nax() - a[x] : f[x].max()); } }
intmain(){ int n = read(); for(int i = 1; i <= n; ++i) a[i] = read(); for(int i = 1; i < n; ++i) { int u = read(), v = read(); g[u].emplace_back(v); g[v].emplace_back(u); } dfs(1, 0); dp(1, 0); printf("%lld\n", res); return0; }
#include<cstdio> #include<vector> #include<set> #define N 200005
char buf[1 << 16], *fs = buf, *ft = buf; inlinechargc(){ if(fs == ft) { ft = (fs = buf) + fread(buf, 1, 1 << 16, stdin); if(fs == ft) return0; } return *fs++; } inlineintread(){ int num = 0, f = 0; char c = gc(); while(c < '0' || '9' < c) { if(c == '-') f = 1; c = gc(); } while('0' <= c && c <= '9') { num = (num << 3) + (num << 1) + c - '0'; c = gc(); } return f ? -num : num; }
structdata { std::multiset<int> ms; inlinevoidadd(int x){ ms.insert(x); } inlinevoiddel(int x){ ms.erase(ms.find(x)); } inlineintmax(){ if(ms.size() < 1) return0; auto it = ms.end(); --it; return *it; } inlineintnax(){ if(ms.size() < 2) return0; auto it = ms.end(); --it; --it; return *it; } inlineintfour(int ax){ if(ms.size() < 4) return0; int res = 0; auto it = ms.end(); int cx = 4; while(cx--) { --it; res += (*it) - ax; } return res; } } f[N], ans[N];
int a[N]; std::vector<int> g[N]; voiddfs(int x, int fa){ f[x].add(a[x]); for(int i = 0, ed = g[x].size(); i < ed; ++i) { int v = g[x][i]; if(v == fa) continue; dfs(v, x); f[x].add(f[v].max() + a[x]); ans[x].add(ans[v].max()); } ans[x].add(f[x].nax() ? f[x].max() + f[x].nax() - a[x] : f[x].max()); }
int res = 0; voiddp(int x, int fa){ res = std::max(res, f[x].four(a[x])); for(int i = 0, ed = g[x].size(); i < ed; ++i) { int v = g[x][i]; if(v == fa) continue;
/* Remove v from x */ ans[x].del(f[x].nax() ? f[x].max() + f[x].nax() - a[x] : f[x].max()); f[x].del(f[v].max() + a[x]); ans[x].del(ans[v].max()); ans[x].add(f[x].nax() ? f[x].max() + f[x].nax() - a[x] : f[x].max());
/* Calculate answer between x and v */ res = std::max(res, ans[x].max() + ans[v].max());
/* Add x to v */ ans[v].del(f[v].nax() ? f[v].max() + f[v].nax() - a[v] : f[v].max()); f[v].add(f[x].max() + a[v]); ans[v].add(ans[x].max()); ans[v].add(f[v].nax() ? f[v].max() + f[v].nax() - a[v] : f[v].max());
dp(v, x);
/* Remove x from v */ ans[v].del(f[v].nax() ? f[v].max() + f[v].nax() - a[v] : f[v].max()); ans[v].del(ans[x].max()); f[v].del(f[x].max() + a[v]); ans[v].add(f[v].nax() ? f[v].max() + f[v].nax() - a[v] : f[v].max());
/* Add v to x */ ans[x].del(f[x].nax() ? f[x].max() + f[x].nax() - a[x] : f[x].max()); ans[x].add(ans[v].max()); f[x].add(f[v].max() + a[x]); ans[x].add(f[x].nax() ? f[x].max() + f[x].nax() - a[x] : f[x].max()); } }
intmain(){ int n = read(); for(int i = 1; i <= n; ++i) a[i] = read(); for(int i = 1; i < n; ++i) { int u = read(), v = read(); g[u].emplace_back(v); g[v].emplace_back(u); } if(n == 1) puts("0"); else { dfs(1, 0); dp(1, 0); printf("%d\n", res); } return0; }