Mroning_X的字典

TimeLimit:1000MS  MemoryLimit:256MB
64-bit integer IO format:%I64d
未提交 | 登录后收藏
Problem Description

图片.png


大家都知道Mroning_X最近打编一本acm常用单词查询词典,但是在编这本书的时候,输入的单词是没有按照字典序的,Mroning_X看着一堆混乱的单词突然他就想玩游戏了,于是他就想出了一个游戏规则:不能交换两两单词的数量,但是可以对第i个单词进行翻转操作,这操作要花费value[i],求出最少花费多少才可以让全部单词满足字典序(str[i]<=str[i+1]),如果不行则输出-1。Mroning_X心满意足的将这题用来提问路过的ACMer,不能在1s内答出来的人会就会被Mroning_X抓去吊在505门口打。

现在轮到你了!!!!!hiahiahiahia!!!!!


Input

The first line of the input contains a single integer n (2 ≤ n ≤ 100 000) — the number of strings.

The second line contains n integers ci (0 ≤ ci ≤ 109), the i-th of them is equal to the amount of energy Vasiliy has to spent in order to reverse the i-th string.

Then follow n lines, each containing a string consisting of lowercase English letters. The total length of these strings doesn't exceed 100 000.


第一行是一个整数N,表示有多少个单词(2<=N<=100000);

第二行有N个数,表示翻转第i个的value;(0<=value[i]<=1e9)

接下来n行,每行一个单词(单词的长度并没有告诉你, 但全部的单词长度加起来一定不会超过100000)


Output

If it is impossible to reverse some of the strings such that they will be located in lexicographical order, print  - 1. Otherwise, print the minimum total amount of energy Vasiliy has to spent.


输出将所有单词字典序从小到大排列好的方案的最小花费,如果不存在将所有单词字典序从小到大排列好的方案则输出-1;


SampleInput 1
2
1 2
ba
ac
SampleOutput 1
1
SampleInput 2
3
1 3 1
aa
ba
ac
SampleOutput 2
1
SampleInput 3
2
5 5
bbb
aaa
SampleOutput 3
-1
SampleInput 4
2
3 3
aaa
aa
SampleOutput 4
-1
Note

In the second sample one has to reverse string 2 or string 3. To amount of energy required to reverse the string 3 is smaller.第一个样例,只交换哪一个都可以,但是交换1的费用更小;

In the third sample, both strings do not change after reverse and they go in the wrong order, so the answer is  - 1。第三个样例,怎么交换都没用,而且字典序是错的.

In the fourth sample, both strings consists of characters 'a' only, but in the sorted order string "aa" should go before string "aaa", thus the answer is  - 1.第四个样例,怎么交换都没用,且aaa>aa

Submit
题目统计信息详细
总AC数13
通过人数9
尝试人数9
总提交量20
AC率45.00%
AC该题后可以添加标签
贴完标签可以获得20ACB。
并且可以获得本题所有提交代码查看权限。
点击标题可以显示标签。
如果你还没认真思考过这题,请不要查看标签
如果您已经通过了该题,请务为该题贴上标签

T^T Online Judge

[BUG反馈] [FAQ] [闽ICP备17026590号-1]
当前版本:3.24 系统时间: