#abc268f. [abc268_f]Best Concatenation

[abc268_f]Best Concatenation

Problem Statement

You are given NN strings S1,S2,ldots,SNS_1, S_2, \\ldots, S_N consisting of digits from 1 through 9 and the character X.

We will choose a permutation P=(P1,P2,ldots,PN)P = (P_1, P_2, \\ldots, P_N) of (1,2,ldots,N)(1, 2, \\ldots, N) to construct a string T=SP1+SP2+cdots+SPNT = S_{P_1} + S_{P_2} + \\cdots + S_{P_N}, where ++ denotes a concatenation of strings.

Then, we will calculate the "score" of the string T=T1T2ldotsTTT = T_1T_2\\ldots T_{|T|} (where T|T| denotes the length of TT).
The score is calculated by the following 99 steps, starting from the initial score 00:

  • Add 11 point to the score as many times as the number of integer pairs (i,j)(i, j) such that 1leqiltjleqT1 \\leq i \\lt j \\leq |T|, Ti=T_i = X, and Tj=T_j = 1.
  • Add 22 points to the score as many times as the number of integer pairs (i,j)(i, j) such that 1leqiltjleqT1 \\leq i \\lt j \\leq |T|, Ti=T_i = X, and Tj=T_j = 2.
  • Add 33 points to the score as many times as the number of integer pairs (i,j)(i, j) such that 1leqiltjleqT1 \\leq i \\lt j \\leq |T|, Ti=T_i = X, and Tj=T_j = 3.
  • cdots\\cdots
  • Add 99 points to the score as many times as the number of integer pairs (i,j)(i, j) such that 1leqiltjleqT1 \\leq i \\lt j \\leq |T|, Ti=T_i = X, and Tj=T_j = 9.

Find the maximum possible score of TT when PP can be chosen arbitrarily.

Constraints

  • 2leqNleq2times1052 \\leq N \\leq 2 \\times 10^5
  • NN is an integer.
  • SiS_i is a string of length at least 11 consisting of digits from 1 through 9 and the character X.
  • The sum of lengths of S1,S2,ldots,SNS_1, S_2, \\ldots, S_N is at most 2times1052 \\times 10^5.

Input

Input is given from Standard Input in the following format:

NN S1S_1 S2S_2 vdots\\vdots SNS_N

Output

Print the answer.


Sample Input 1

3
1X3
59
XXX

Sample Output 1

71

When P=(3,1,2)P = (3, 1, 2), we have T=S3+S1+S2=T = S_3 + S_1 + S_2 = XXX1X359. Then, the score of TT is calculated as follows:

  • there are 33 integer pairs (i,j)(i, j) such that 1leqiltjleqT1 \\leq i \\lt j \\leq |T|, Ti=T_i = X, and Tj=T_j = 1;
  • there are 44 integer pairs (i,j)(i, j) such that 1leqiltjleqT1 \\leq i \\lt j \\leq |T|, Ti=T_i = X, and Tj=T_j = 3;
  • there are 44 integer pairs (i,j)(i, j) such that 1leqiltjleqT1 \\leq i \\lt j \\leq |T|, Ti=T_i = X, and Tj=T_j = 5;
  • there are 44 integer pairs (i,j)(i, j) such that 1leqiltjleqT1 \\leq i \\lt j \\leq |T|, Ti=T_i = X, and Tj=T_j = 9.

Therefore, the score of TT is $1 \\times 3 + 3 \\times 4 + 5 \\times 4 + 9 \\times 4 = 71$, which is the maximum possible value.


Sample Input 2

10
X63X395XX
X2XX3X22X
13
3716XXX6
45X
X6XX
9238
281X92
1XX4X4XX6
54X9X711X1

Sample Output 2

3010