#abc354g. G - Select Strings

G - Select Strings

Score : 625625 points

问题陈述

给定 NN 个字符串 S1,S2,,SNS_1, S_2, \ldots, S_N,它们由小写英文字母组成,以及 NN 个正整数 A1,A2,,ANA_1, A_2, \ldots, A_N

如果集合 TT{1,2,,N}\lbrace 1, 2, \ldots, N \rbrace 的一个子集,并且对于 TT 中的任意两个不同的元素 i,ji, jSiS_i 不是 SjS_j 的子串,则称 TT 为一个好集合

找出一个好集合 TTiTAi\displaystyle \sum_{i \in T} A_i 的最大可能值。

什么是子串?字符串 SS 的一个子串是通过从 SS 的开头删除零个或多个字符,以及从 SS 的结尾删除零个或多个字符得到的字符串。例如,ababc 的一个子串,但 ac 不是 abc 的子串。

以上为大语言模型 kimi 翻译,仅供参考。

Problem Statement

You are given NN strings S1,S2,,SNS_1, S_2, \ldots, S_N consisting of lowercase English letters and NN positive integers A1,A2,,ANA_1, A_2, \ldots, A_N.

A subset TT of {1,2,,N}\lbrace 1, 2, \ldots, N \rbrace is called a good set if there is no pair i,jT(ij)i, j \in T (i \neq j) such that SiS_i is a substring of SjS_j.

Find the maximum possible value of iTAi\displaystyle \sum_{i \in T} A_i for a good set TT.

What is a substring? A substring of a string SS is a string obtained by deleting zero or more characters from the beginning and zero or more characters from the end of SS. For example, ab is a substring of abc, but ac is not a substring of abc.

Constraints

  • 1N1001 \leq N \leq 100
  • SiS_i is a string consisting of lowercase English letters.
  • 1Si1 \leq |S_i|
  • S1+S2++SN5000|S_1| + |S_2| + \ldots + |S_N| \leq 5000
  • 1Ai1091 \leq A_i \leq 10^9

Input

The input is given from Standard Input in the following format:

NN

S1S_1

S2S_2

\vdots

SNS_N

A1A_1 A2A_2 \ldots ANA_N

Output

Print the answer.

Sample Input 1

4
atcoder
at
coder
code
5 2 3 4

Sample Output 1

6

The possible good sets TT and their corresponding iTAi\displaystyle \sum_{i \in T} A_i are as follows:

  • T={1}T = \lbrace 1 \rbrace: iTAi=5\displaystyle \sum_{i \in T} A_i = 5
  • T={2}T = \lbrace 2 \rbrace: iTAi=2\displaystyle \sum_{i \in T} A_i = 2
  • T={3}T = \lbrace 3 \rbrace: iTAi=3\displaystyle \sum_{i \in T} A_i = 3
  • T={4}T = \lbrace 4 \rbrace: iTAi=4\displaystyle \sum_{i \in T} A_i = 4
  • T={2,3}T = \lbrace 2, 3 \rbrace: iTAi=5\displaystyle \sum_{i \in T} A_i = 5
  • T={2,4}T = \lbrace 2, 4 \rbrace: iTAi=6\displaystyle \sum_{i \in T} A_i = 6

The maximum among them is 66, so print 66.

Sample Input 2

10
abcd
abc
ab
a
b
c
d
ab
bc
cd
100 10 50 30 60 90 80 70 40 20

Sample Output 2

260