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