Vasiliy is fond of solving different tasks. Today he found one he wasn't able to solve himself, so he asks you to help.

Vasiliy is given n strings consisting of lowercase English letters. He wants them to be sorted in lexicographical order (as in the dictionary), but he is not allowed to swap any of them. The only operation he is allowed to do is to reverse any of them (first character becomes last, second becomes one before last and so on).

To reverse the i-th string Vasiliy has to spent ci units of energy. He is interested in the minimum amount of energy he has to spent in order to have strings sorted in lexicographical order.

String A is lexicographically smaller than string B if it is shorter than B (|A| < |B|) and is its prefix, or if none of them is a prefix of the other and at the first position where they differ character in A is smaller than the character in B.

For the purpose of this problem, two equal strings nearby do not break the condition of sequence being sorted lexicographically.

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.

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.

Examples

Input

2
1 2
ba
ac


Output

1


Input

3
1 3 1
aa
ba
ac


Output

1


Input

2
5 5
bbb
aaa


Output

-1


Input

2
3 3
aaa
aa


Output

-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.

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.
# this actually seems like a relatively simple problem
# storing is fine - store 2 values per string 
# one for min(cost) if switch
# one for min(cost) if no switch 

#algorithm is given the assumption that
# suppose we already have a bunch of sorted strings
# the next string in place is ONLY valid if it is valid for the next upper switch
# i.e. 4 cases - automatically valid, valid if prev switches, 
# valid if both prev and I switch, valid if I switch
# If valid for none of these, then the string is not valid

# other problem is simply determining whether a string is valid or not 
# (which in itself, may be a costly step)
# I think python has good strcmp operators

n = int(raw_input())
costs = raw_input().split()
costs = [int(x) for x in costs]
strs = []
rstrs = [] #strings that are reversed
# apparently to rverse, simply attach [::-1]

cnorm = [0]*n #total min costs at norm
crev = [0]*n #total min costs at reversed

for i in range(n):
	a = raw_input()
	strs.append(a)
	b = str(a)
	rstrs.append(b[::-1])

#print strs
#print rstrs
#print costs

# now start the checking
orderFound = 1
for i in range(n):
	if i == 0:
		#first string, just store its 0 and cost values
		cnorm[i] = 0
		crev[i] = costs[i]
	else:

		#if not first string, need to check compatibility
		nn = 0 # prev norm, curr norm
		nr = 0 # prev norm, curr rev
		rr = 0 # both rev
		rn = 0 # prev rev, curr norm
		if strs[i] >= strs[i-1]:
			nn = 1
		if strs[i] >= rstrs[i-1]:
			rn = 1
		if rstrs[i] >= strs[i-1]:
			nr = 1
		if rstrs[i] >= rstrs[i-1]:
			rr = 1

		if nn == 0 and nr == 0 and rr == 0 and rn == 0:
			#sort is impossible
			orderFound = 0
			break
		# check both nn and rn - i.e. current normal
		# check both rr and nr - i.e. current reversed

		if nn == 1 and rn == 1:
			# make sure both of them are possible, i.e. cnom and crev aren't -1 
			if cnorm[i-1] == -1:
				cnorm[i] = crev[i-1]
			elif crev[i-1] == -1:
				cnorm[i] = cnorm[i-1]
			else:
				#both are fine
				cnorm[i] = min(cnorm[i-1], crev[i-1])
		elif nn == 1:
			#norm works, prev rev no work, which is still fine
			cnorm[i] = cnorm[i-1] #these are fine
		elif rn == 1:
			cnorm[i] = crev[i-1] # these are fine
		else:
			cnorm[i] = -1 #-1 means impossible for this branch to exist

		#now for the reversed case
		if nr == 1 and rr == 1:
			if crev[i-1] == -1 and cnorm[i-1] == -1:
				crev[i] = -1
			elif crev[i-1] == -1:
				crev[i] = cnorm[i-1] + costs[i]
			elif cnorm[i-1] == -1:
				crev[i] = crev[i-1] + costs[i]
			else:
				#both are fine
				crev[i] = min(cnorm[i-1], crev[i-1]) + costs[i]
		elif rr == 1:
			# check if reverse is possible
			if crev[i-1] == -1:
				crev[i] = -1
			else:
				crev[i] = crev[i-1] + costs[i]
		elif nr == 1:
			# check if normal is valid
			if cnorm[i-1] == -1:
				crev[i] = -1
			else:
				crev[i] = cnorm[i-1] + costs[i]
		else:
			# neither valid 
			crev[i] = -1

		#finally check that last 4 terms are not -1, otherwise we early exit
		if crev[i] == -1 and cnorm[i] == -1:
			orderFound = 0
			break


#the only problem is the string comparison checking, otherwise algorithm should be fineee

#print crev
#print cnorm

if orderFound == 0:
	print -1
else:
	if crev[n-1] == -1:
		print cnorm[n-1]
	elif cnorm[n-1] == -1:
		print crev[n-1]
	else:
		print min(crev[n-1], cnorm[n-1])










	  		 				 	      		 				  	