PHƯỚC HƯNG OJ · THỬ THÁCH THUẬT TOÁN

#GD0000037. Phép nối tốt nhất (Best Concatenation)

HỌC THEO LỘ TRÌNH

Bài tập thuộc lộ trình

1 lộ trình

Phép nối tốt nhất (Best Concatenation)

Best Concatenation

Source: AtCoder

Version: Phuoc Hung OJ Extended

Problem Statement

There are nn strings consisting of digits 1 to 9 and X. Concatenate them in some order. The score is the sum, over every digit, of its value multiplied by the number of preceding X characters. Find the maximum score.

Input

The first line contains nn. Each of the next nn lines contains one string.

Output

Print the maximum score.

Constraints

  • 1≤n≤2⋅1051\le n\le 2\cdot10^5.
  • 1≤∣si∣1\le |s_i| và ∑i=1n∣si∣≤2⋅105\sum_{i=1}^n |s_i|\le 2\cdot10^5.
  • Each character of sis_i is X or a digit from 1 through 9..

Subtasks

  • Subtask 1 (20 points): n≤8n\le 8.
  • Subtask 2 (30 points): n≤30n\le 30.
  • Subtask 3 (50 points): Full constraints.

Examples

Input

3
1X3
59
XXX

Output

71

Explanation

The sample follows the ordering rule described in the Vietnamese explanation and produces the shown optimum.