#include #include #define rep(i, n) for(int i = 0; i < (n); ++i) using namespace std; typedef unsigned int uint; typedef vector V; typedef vector VS; typedef vector VU; int n; int a[50000]; V t[50000]; bool u[50000]; uint ans; VU eval(const VS& c, const VU& v, int p){ V cs(1000 + 1, 0); VU vs(1000 + 1, 0); rep(i, 1000){ cs[i + 1] = cs[i] + c[i]; vs[i + 1] = vs[i] + v[i]; } uint ca = cs[p], va = vs[p]; uint cb = cs[1000] - cs[p + 1], vb = vs[1000] - vs[p + 1]; uint vc = vs[p + 1] - vs[p]; return {ca * (ca - 1) / 2 + va, cb * (cb - 1) / 2 + vb, vc}; } uint dfs(int x, int k){ u[x] = true; VS c(1000, 0); VU v(1000, 0); for(int i: t[x]){ if(!u[i]){ c[a[i]] = 1; v[a[i]] = max(dfs(i, a[x]), v[a[i]]); } } auto e1 = eval(c, v, a[x]); ans = max(*max_element(e1.begin(), e1.end()), ans); c[k] = 1; v[k] = 0; auto e2 = eval(c, v, a[x]); return x[a] != k ? e2[x[a] < k] : e2[2]; } int main(){ cin >> n; rep(i, n){ cin >> a[i]; --a[i]; } rep(i, n - 1){ int x, y; cin >> x >> y; --x; --y; t[x].push_back(y); t[y].push_back(x); } dfs(0, 1); cout << ans << endl; return 0; }