import std.stdio; import std.conv, std.array, std.algorithm, std.string; import std.math, std.random, std.range, std.datetime; import std.bigint; ulong globalw; class Node{ int id; char name; Node[] adjnodes; this(int id, char c){ this.id = id; this.name = c; } ulong value(){ debug static int x; debug x += 1; debug if(x % 100 == 0) writeln("calcValue ", x); ulong v; if(this.name == 'w'){ foreach(nx; adjnodes){ v += nx.ccount(this) * (globalw - 1 - nx.wcount(this)); } return v; } else return 0; } private ulong[int] _ccount; private ulong[int] _wcount; ulong xc; ulong ccount(Node nd){ if(nd.id in _ccount) return _ccount[nd.id]; else{ debug static int x; debug x += 1; debug if(x % 100 == 0) writeln("ccount ", x); if(this.name == 'c') xc = 1; else xc = 0; foreach(nx; adjnodes){ if(nx.id != nd.id) xc += nx.ccount(this); } _ccount[nd.id] = xc; return xc; } } ulong wcount(Node nd){ if(nd.id in _wcount) return _wcount[nd.id]; else{ debug static int x; debug x += 1; debug if(x % 100 == 0) writeln("wcount ", x); if(this.name == 'w') xc = 1; else xc = 0; foreach(nx; adjnodes){ if(nx.id != nd.id) xc += nx.wcount(this); } _wcount[nd.id] = xc; return xc; } } } class Edge{ this(Node nd1, Node nd2){ nd1.adjnodes ~= nd2; nd2.adjnodes ~= nd1; } } void main(){ int n = readln.chomp.to!int; string s = readln.chomp; Node[] nodes; foreach(int i, c; s){ nodes ~= new Node(i, c); } Edge[] edges; for(int i = 1; i <= n - 1; i ++){ int[] tmp = readln.chomp.split.map!(to!int).map!(x => x - 1).array; edges ~= new Edge(nodes[tmp[0]] , nodes[tmp[1]]); } foreach(nd; nodes) if(nd.name == 'w') globalw += 1; BigInt ans; foreach(nd; nodes) ans += nd.value; ans.writeln; }