#include using namespace std; typedef long long ll; typedef vector vi; typedef vector vl; typedef complex P; typedef pair pii; #define REP(i,n) for(ll i=0;i dp[52525][2]; // その点から下の者達 ll cnt[52525][2]; // ??? ll dfs(int pos, int par, int type){ int opp = 1-type; ll ans = 0; REP(i,g[pos].size()){ int to = g[pos][i]; if(to==par)continue; ll tmp = dfs(to,pos,opp); ans = max(ans,tmp); // じぐざぐ if(a[pos]==a[to] || (a[pos](dp[pos][type][a[to]], cnt[to][opp] + dp[to][opp].size()); } map::iterator iter = dp[pos][type].begin(); while(iter != dp[pos][type].end()){ cnt[pos][type] += iter->second; iter++; } // to - pos - to ラインの門松列の加算 ll sz = dp[pos][type].size(); cnt[pos][type] += sz*(sz-1ll)/2ll; ans = max(ans,cnt[pos][type]); return ans; } int main(){ scanf("%d",&n); REP(i,n)scanf("%d",a+i); REP(i,n-1){ int x,y; scanf("%d%d",&x,&y); --x;--y; g[x].push_back(y); g[y].push_back(x); } ll ans = 0; ans = max(ans, dfs(0,830252521,0)); ans = max(ans, dfs(0,830253150,1)); printf("%lld\n",ans); return 0; }