#ABC268F. [ABC268F] Best Concatenation

[ABC268F] Best Concatenation

Score : 500500 points

Problem Statement

You are given NN strings S1,S2,,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,,PN)P = (P_1, P_2, \ldots, P_N) of (1,2,,N)(1, 2, \ldots, N) to construct a string T=SP1+SP2++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=T1T2TTT = 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 1i<jT1 \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 1i<jT1 \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 1i<jT1 \leq i \lt j \leq |T|, Ti=T_i = X, and Tj=T_j = 3.
  • \cdots
  • Add 99 points to the score as many times as the number of integer pairs (i,j)(i, j) such that 1i<jT1 \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

  • 2N2×1052 \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,,SNS_1, S_2, \ldots, S_N is at most 2×1052 \times 10^5.

Input

Input is given from Standard Input in the following format:

NN

S1S_1

S2S_2

\vdots

SNS_N

Output

Print the answer.

3
1X3
59
XXX
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 1i<jT1 \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 1i<jT1 \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 1i<jT1 \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 1i<jT1 \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.

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