Example

Input

2
()
1 2


Output

1
#include <bits/stdc++.h>
#define syosu(x) fixed<<setprecision(x)
using namespace std;
typedef long long ll;
typedef unsigned int uint;
typedef unsigned long long ull;
typedef pair<int,int> P;
typedef pair<double,double> pdd;
typedef pair<ll,ll> pll;
typedef vector<int> vi;
typedef vector<vi> vvi;
typedef vector<double> vd;
typedef vector<vd> vvd;
typedef vector<ll> vl;
typedef vector<vl> vvl;
typedef vector<string> vs;
typedef vector<P> vp;
typedef vector<vp> vvp;
typedef vector<pll> vpll;
typedef pair<int,P> pip;
typedef vector<pip> vip;
const int inf=1<<30;
const ll INF=1ll<<60;
const double pi=acos(-1);
const double eps=1e-8;
const ll mod=1e9+7;
const int dx[4]={-1,0,1,0},dy[4]={0,-1,0,1};

int n;
vi a,vis,s;
vvi g,b,c;
ll res=0;
vi tb,tc;

int Cdfs(int v,int p){
	s[v]=1;
	for(auto u:g[v]) if(!vis[u]&&u!=p) s[v]+=Cdfs(u,v);
	return s[v];
}

P Cdfs1(int v,int p,int S){
	P q={inf,0};
	int M=0,S_=S-1;
	for(auto u:g[v]) if(!vis[u]&&u!=p){
		q=min(q,Cdfs1(u,v,S));
		M=max(M,s[u]);
		S_-=s[u];
	}
	return min(q,{max(M,S_),v});
}

int Cent(int v){
	Cdfs(v,-1);
	return Cdfs1(v,-1,s[v]).second;
}

void dfs(int v,int p,int x=0,int mx=0,int y=0,int my=0){
	x-=a[v];
	mx=min(mx,x);
	if(x==mx) tb.push_back(-x);
	y+=a[v];
	my=min(my,y);
	if(y==my) tc.push_back(-y);
	for(auto u:g[v]) if(u!=p&&!vis[u]){
		dfs(u,v,x,mx,y,my);
	}
}

void Rec(int v){
	v=Cent(v);
	vis[v]++;
	map<int,int> mp;
	b.clear(),c.clear();
	for(auto u:g[v]) if(u!=v&&!vis[u]){
		tb.clear(),tc.clear();
		dfs(u,v);
		b.push_back(tb);
		c.push_back(tc);
		if(a[v]>0){
			for(auto j:tc) if(j==1) res++;
		}
		else{
			for(auto j:tb) if(j==1) res++;
		}
		for(auto j:tc) mp[j]++;
	}
	int N=b.size();
	for(int i=0;i<N;i++) for(auto j:b[i]) res+=mp[j+a[v]];
	for(int i=0;i<N;i++){
		mp.clear();
		for(auto j:c[i]) mp[j]++;
		for(auto j:b[i]) res-=mp[j+a[v]];
	}
	for(auto u:g[v]) if(u!=v&&!vis[u]) Rec(u);
}

int main(){
	cin>>n;
	a=vi(n,1);
	vis=s=vi(n);
	g=vvi(n);
	for(int i=0;i<n;i++){
		char c;
		cin>>c;
		if(c==')') a[i]=-1;
	}
	for(int i=1;i<n;i++){
		int u,v;
		cin>>u>>v;
		u--;v--;
		g[u].push_back(v);
		g[v].push_back(u);
	}
	Rec(0);
	cout<<res<<endl;
}
