#P15960. [ICPC 2018 Jakarta R] Future Generation

    ID: 29411 远端评测题 1000ms 512MiB 尝试: 0 已通过: 0 难度: 7 上传者: 标签>2018排序ICPC双指针 two-pointer状压 DP

[ICPC 2018 Jakarta R] Future Generation

题目描述

Andi is getting married! He and his partner plan to have NN children. To avoid any hassle in the future, Andi wants to decide all their children’s name in advance. Specifically, he wants each child to have a name which is lexicographically larger than any of his/her older siblings. Of course, his partner also agrees with this idea. String AA is lexicographically larger than string BB if and only if BB is a prefix of AA or there exists an index ii where Ai>BiA_i > B_i and Aj=BjA_j = B_j for all j<ij < i. Note that a proper name can be as short as one character, but it cannot be an empty string.

Life is good for Andi, until one day he told his soon-to-be-grandmother-in-law (i.e. his partner’s grandma) about this marriage plan. After learning that Andi plans to have NN children with her granddaughter, she gave him NN names to be used. Moreover, the ithi^{th} name can only be used for the ithi^{th} child.

After going through a long debate with her grandma, Andi came into an agreement: The ithi^{th} child’s name should be a subsequence of the ithi^{th} name her grandma gave. A string AA is a subsequence of string BB if AA can be obtained by deleting zero or more characters from BB without changing the remaining characters’ order, e.g., ABC is a subsequence of DAEBCCB, but EFG is not a subsequence of FABEGC.

Even though Andi dislikes the given list of names, he still wants to impress his partner by showing that he can satisfy both her grandma’s wish and his own desire (i.e. each child’s name is lexicographically larger than any of his/her older siblings). However, Andi wonders, what is the maximum possible total length of their children names?

For example, let N=3N = 3, and the names given by his partner’s grandma are (KARIM, PARBUDI, CHANDRA). Here are several example set of names which satisfies Andi’s desire:

  • [AR, BI, CRA] with a total length of 2+2+3=72 + 2 + 3 = 7.
  • [ARI, BUDI, CHANDRA] with a total length of 3+4+7=143 + 4 + 7 = 14.
  • [ARIM, ARUDI, CHANDRA] with a total length of 4+5+7=164 + 5 + 7 = 16.
  • [AIM, ARBUDI, CHANDRA] with a total length of 3+6+7=163 + 6 + 7 = 16.
  • \dots

Among all sets of names which satisfy Andi’s desire in this example, the maximum total length is 1616. Note that there might be cases where valid set of names cannot be obtained. In such case, you should output -1. For example, let N=2N = 2 and the names given by his partner’s grandma are (ZORO, ANDI). In this example, all subsequences of the 2nd2^{nd} name are lexicographically smaller than all subsequences of the 1st1^{st} name, thus, no possible valid set of names can be obtained.

输入格式

Input begins with a line containing an integer NN (1N151 \leq N \leq 15) representing the number of children. The next NN lines, each contains a string SiS_i (1Si151 \leq |S_i| \leq 15) representing the ithi^{th} name given by Andi’s soon-to-be-grandmother-in-law. SiS_i contains only uppercase alphabets (Sij{AZ}S_{ij} \in \{A - Z\}).

输出格式

Output contains an integer in a line representing the maximum possible total length of their children names, or 1-1 if no possible valid set of names can be obtained.

3
KARIM
PARBUDI
CHANDRA
16
2
ZORO
ANDI
-1
7
HAVE
FUN
IN
ICPC
JAKARTA
TWENTY
EIGHTEEN
25

提示

Explanation for the sample input/output #3

One possible solution is [AVE, FUN, IN, IPC, JAKARTA, NTY, TEEN] with a total length of 3+3+2+3+7+3+4=253 + 3 + 2 + 3 + 7 + 3 + 4 = 25.