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.
from __future__ import print_function
import sys
from collections import deque,Counter,defaultdict
import math
import functools
import itertools
from bisect import bisect_left,bisect_right
import operator
import heapq
from random import random,randint,randrange,shuffle
true=True
false=False
null=None
reduce=functools.reduce
SLOW=__debug__
try:
 range=xrange
except:
 pass
def fast():
 global SLOW
 SLOW=False
def div(a,b): return float(a)/b
def compute(f,v): return f(v)
def seq(lo,hi,step=1): 
 return range(lo,hi+1,step)
def sround(val,nd):
 return '{0:.{1}f}'.format(val,nd)
def ceil(a,b):
 ans=a//b
 if a%b!=0: ans+=1
 return ans
def e1e(d,e): return d*(10**e)
mod=e1e(1,9)+7
try:
 memoi=functools.lru_cache(None)
except:
 class memoize(dict):
  def __init__(self,f):
   self.f=f
  def __call__(self,*args):
   return self[args]
  def __missing__(self,key):
   ans=self[key]=self.f(*key)
   return ans
 memoi=memoize
class ndarray(list):
 def __init__(self,defval,sizes):
  self.sizes=sizes
  self.dimension=len(sizes)
  self.pm=pm=list(sizes)
  pm.append(1)
  for ii in reversed(range(self.dimension)): pm[ii]*=pm[ii+1]
  list.__init__(self,[defval]*pm[0])
 def ___i1d___(self,ixs):
  if SLOW:
   assert len(ixs)==self.dimension, ('Dimension must be {}.'.format(self.dimension))
  ans=0
  for ii in range(self.dimension):
   ix=ixs[ii]
   if SLOW:
    assert ix<self.sizes[ii], ('Index[{}]={} >= Len[{}]={}.'.format(ii,ix,ii,self.sizes[ii]))
   ans+=ix*self.pm[ii+1]
  return ans
 def __getitem__(self,ixs):
  ixs=self.___i1d___(ixs)
  return list.__getitem__(self,ixs)
 def __setitem__(self,ixs,val):
  ixs=self.___i1d___(ixs)
  list.__setitem__(self,ixs,val)
 def _str_(self,dim0=0,ofs=0):
  lft="  "*dim0
  if dim0+1==self.dimension: return lft+str(list.__getitem__(self,slice(ofs,ofs+self.sizes[dim0])))
  ans=[]
  for ii in range(self.sizes[dim0]):
   ans.append(self._str_(dim0+1,ofs))
   ofs+=self.pm[dim0+1]
  ans="{0}[\n{1}\n{0}]".format(lft,",\n".join(ans))
  return ans
 def __str__(self):
  return self._str_()
def arrays(defval,*sizes):
 if len(sizes)==1: return [defval]*sizes[0]
 return ndarray(defval,sizes)
def perr(*args,**kwargs): 
 if SLOW:
  print(*args,file=sys.stderr,**kwargs)
def line():
 ln=sys.stdin.readline().strip()
 #perr(ln)
 if ln=='': sys.exit()
 return ln
def lines(n): return [line() for i in range(n)]
def split(ln=None): return (ln or line()).split()
def num(str=None):
 str=str or line()
 return float(str) if '.' in str else int(str)
def nums(o=None):
 if o is not None:
  if isinstance(o, int): o=lines(o)
  #elif isinstance(o, str): o=split(o)
 return list(map(num, o or split()))
def loop(f,n=0):
 for tcid in range(n or 99999999): f(tcid+1)
def repeat(n,f):
 for _ in range(n): yield f()
def recursi(root,fchildren,ctop):
  p=deque()
  c=deque()
  q=deque()
  q.append(root)
  while len(q)>0:
   pp=q.popleft()
   for cc in fchildren(pp):
    p.append(pp)
    c.append(cc)
    q.append(cc)
  for _ in range(len(p)):
   ctop(c.pop(),p.pop())   
def summod(*args):
 sums=0
 for vv in args:
  sums=(sums+vv)%mod
 return sums
def multmod(*args):
 ans=0
 for vv in args:
  ans=(ans+vv)%mod
 return ans
def subsmod(*args):
 ans=0
 for vv in args:
  ans=(ans+mod-vv)%mod
 return ans
def matmultmod(a,b):
 ra=len(a)
 ca=len(a[0])
 rb=len(b)
 cb=len(b[0])
 if SLOW:
  assert ca==rb, "matrix incompatible size"
 return [[summod(*map(lambda ii:a[rr][ii]*b[ii][cc],range(ca))) for cc in range(cb)] for rr in range(ra)]
"""
matmultmod()
summod() multmod() subsmod()
recursi(root,fchilds,ctop)
e1e(d,e) mod arrays(defv,*sz) repeat(n,f)
ceil(a,b) sround(val,nd) true false null @memoi
num(?) nums(?) split(?) lines(n) line()
perr(print) seq() loop(f(tcid),0) compute(f,v) fast()
"""
#
def mainloop(tcid): #
 ignored=1 #
 def solve():
  n=num()
  c=nums()
  s=lines(n)
  r=[ss[::-1] for ss in s]
  aa=arrays(null,n)
  bb=arrays(null,n)
  def aaa(dp,ii,*vv):
   if null not in vv:
    if dp[ii] is null:
     dp[ii]=1<<62
    dp[ii]=min(dp[ii],sum(vv))
  aa[0]=0
  bb[0]=c[0]
  for ii in range(1,n):
   if s[ii]>=s[ii-1]:
    aaa(aa,ii,aa[ii-1])
   if s[ii]>=r[ii-1]:
    aaa(aa,ii,bb[ii-1])
   if r[ii]>=s[ii-1]:
    aaa(bb,ii,c[ii],aa[ii-1])
   if r[ii]>=r[ii-1]:
    aaa(bb,ii,c[ii],bb[ii-1])
  ans=[xx for xx in [aa[-1],bb[-1]] if xx is not null]
  return min(ans) if ans else -1
 ans=solve()
 print(ans)
tcmax=0
loop(mainloop,tcmax) #
#