void main(){ import std.stdio, std.string, std.conv, std.algorithm; import std.typecons; int n; rd(n); auto s=readln.chomp.to!(char[]); auto g=new int[][](n, 0); foreach(_; 0..(n-1)){ int a, b; rd(a, b); g[a-1]~=(b-1); g[b-1]~=(a-1); } alias T=Tuple!(long, "c", long, "w"); auto _c=new long[](n), _w=new long[](n); T dfs(int i, int pre=-1){ long c=0, w=0; foreach(j; g[i])if(j!=pre){ auto res=dfs(j, i); c+=res.c; w+=res.w; } if(s[i]=='c') c++; if(s[i]=='w') w++; _c[i]=c; _w[i]=w; return T(c, w); } long sum=0; void dfs2(int i, int pre=-1, long c=0, long w=0){ if(s[i]=='w'){ sum+=_c[i]*w+c*(_w[i]-1); foreach(j; g[i])if(j!=pre){ sum+=_w[j]*(_c[i]-_c[j]); } } foreach(j; g[i])if(j!=pre){ dfs2(j, i, c+(_c[i]-_c[j]), w+(_w[i]-_w[j])); } } auto res=dfs(0); dfs2(0); writeln(sum); } void rd(T...)(ref T x){ import std.stdio, std.string, std.conv; auto l=readln.split; assert(l.length==x.length); foreach(i, ref e; x){ e=l[i].to!(typeof(e)); } }