Problem Statement
You are given a length-N sequence A=(A1,A2,dots,AN) consisting of 0, 1, and 2, and a length-N string S=S1S2dotsSN consisting of M
, E
, and X
.
Find the sum of textmex(Ai,Aj,Ak) over all tuples of integers (i,j,k) such that 1leqi<j<kleqN and SiSjSk= MEX
. Here, textmex(Ai,Aj,Ak) denotes the minimum non-negative integer that equals neither Ai,Aj, nor Ak.
Constraints
- 3leqNleq2times105
- N is an integer.
- Aiinlbrace0,1,2rbrace
- S is a string of length N consisting of
M
, E
, and X
.
The input is given from Standard Input in the following format:
N
A1 A2 dots AN
S
Output
Print the answer as an integer.
4
1 1 0 2
MEEX
Sample Output 1
3
The tuples (i,j,k)(1leqi<j<kleqN) such that SiSjSk = MEX
are the following two: (i,j,k)=(1,2,4),(1,3,4). Since textmex(A1,A2,A4)=textmex(1,1,2)=0 and textmex(A1,A3,A4)=textmex(1,0,2)=3, the answer is 0+3=3.
3
0 0 0
XXX
Sample Output 2
0
15
1 1 2 0 0 2 0 2 0 0 0 0 0 2 2
EXMMXXXEMEXEXMM
Sample Output 3
13