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.
import java.util.*;
import java.math.*;
import java.io.*; 
 public class B{
static int[] dx={-1,1,0,0};
static int[] dy={0,0,1,-1};
    static FastReader scan=new FastReader();
   public static PrintWriter out = new PrintWriter (new BufferedOutputStream(System.out));
   static boolean prime[];
   static void sieve(int n) 
    { 
        
        prime = new boolean[n+1]; 
        for(int i=0;i<n;i++) 
            prime[i] = true; 
          
        for(int p = 2; p*p <=n; p++) 
        { 
           
            if(prime[p] == true) 
            { 
                
                for(int i = p*p; i <= n; i += p) 
                    prime[i] = false; 
            } 
        } 
          
        
    }  
   static LinkedList<Pair>edges[];
   
 public static boolean areSame(int []arr)
    {
        // Put all array elements in a HashSet
        Set<Integer>s=new HashSet<Integer>();
        for(int i=0;i<arr.length;i++)
            s.add(arr[i]);

 
        // If all elements are same, size of
        // HashSet should be 1. As HashSet contains only distinct values.
        return (s.size() == 1);
    }
   

    
     public static int lowerBound(long[] array, int length, long value) {
        int low = 0;
        int high = length;
        while (low < high) {
            final int mid = (low + high) / 2;
            //checks if the value is less than middle element of the array
            if (value <= array[mid]) {
                high = mid;
            } else {
                low = mid + 1;
            }
        }
        return low;
    }
    public static int upperBound(long[] array, int length, long value) {
        int low = 0;
        int high = length;
        while (low < high) {
            final int mid = low+(high-low) / 2;
            if ( array[mid]>value) {
                high = mid ;
            } else {
                low = mid+1;
            }
        }
        return low;
    }
static long mod(long x,long y)
    {
        if(x<0)
            x=x+(-x/y+1)*y;
        return x%y;
    }
    
    
    static boolean isPowerOfTwo(int n)
{
    if(n==0)
    return false;
 
return (int)(Math.ceil((Math.log(n) / Math.log(2)))) == 
       (int)(Math.floor(((Math.log(n) / Math.log(2)))));
}
static boolean isprime(long x)
           {
            for(long i=2;i*i<=x;i++)
                if(x%i==0)
                    return false;
                return true;
           }
           static long arr[];
        static int n;
        static int k;

 static boolean can(long x)
 {
    int bl=0;
    int i=0;
    for(int kas=0;i<n&&kas<k;kas++)
    {
        long hasta=arr[i];
        hasta+=x*2;
        while(i<n-1&&arr[i+1]<=hasta)i++;
        i++;
        
    }
return (i>=n);
 }
 static long mod=Long.MAX_VALUE;
static int p[];
static int v[];
static int dp[][];
static int count=0;
static int max(int i,int price)
{
    if(i==n)
        return 0;
    int choice1=0,choice2=0;
    if(dp[i][price]!=-1){
    //out.println("FUCK");
        return dp[i][price];
    }
    //out.println(count++);

      if(price<p[i])
       return dp[i][price]=max(i+1,price);
    else
return dp[i][price]=Math.max(max(i+1,price-p[i])+v[i],max(i+1,price));
    

  
}

    public static void main(String[] args)  throws IOException 
{
//java.util.Scanner scan=new java.util.Scanner(new File("mootube.in"));
 //PrintWriter out = new PrintWriter (new FileWriter("mootube.out"));
//java.util.Scanner scan=new java.util.Scanner(new File("angry.in"));
//PrintWriter out = new PrintWriter (new FileWriter("angry.out"));
n=scan.nextInt();
special arr[]=new special[n];
long energy[]=new long[n];
for(int i=0;i<n;i++)
    energy[i]=scan.nextLong();
for(int i=0;i<n;i++)
    arr[i]=new special(scan.next(),energy[i]);
long dp[][]=new long[n+1][2];
for(int i=0;i<n;i++)
{
    if(i==0)
    {
        dp[i][0]=0;
        dp[i][1]=arr[0].l;
    }
    else 
    {
        long dp01=0,dp10=0,dp11=0,dp101=0;
        if(arr[i].s.compareTo(arr[i-1].s)>=0&&dp[i-1][0]!=mod)
                    dp01=dp[i-1][0];
            else 
                dp01=mod;
         if(arr[i].s.compareTo(arr[i-1].reversed)>=0&&dp[i-1][1]!=mod)
            dp10=dp[i-1][1];
        else 
            dp10=mod;
        if(arr[i].reversed.compareTo(arr[i-1].s)>=0&&dp[i-1][0]!=mod)
            dp11=dp[i-1][0]+arr[i].l;
        else dp11=mod;
        if(arr[i].reversed.compareTo(arr[i-1].reversed)>=0&&dp[i-1][1]!=mod)
            dp101=dp[i-1][1]+arr[i].l;
        else 
            dp101=mod;
        dp[i][0]=Math.min(dp01,dp10);
        dp[i][1]=Math.min(dp11,dp101);        
    }
}
long ans=Math.min(dp[n-1][1],dp[n-1][0]);
out.println(ans==mod?-1:ans);

out.close();





    

}
static class special{
    String s;
    String reversed;
    long l;
    special(String s,long l)
    {
        this.s=s;
        this.l=l;
        StringBuilder sb=new StringBuilder(s);
        reversed=sb.reverse().toString();
    }
}
 public static  long pow(long b, long e) {
            long r = 1;
            while (e > 0) {
                if (e % 2 == 1) r = r * b ;
                b = b * b;
                e >>= 1;
            }
            return r;
        }

private static void sort(long[] arr) {
        List<Long> list = new ArrayList<>();
        for (long object : arr) list.add(object);
        Collections.sort(list);
        for (int i = 0; i < list.size(); ++i) arr[i] = list.get(i);
    }
 public static class FastReader {
        BufferedReader br;
        StringTokenizer root;
        
 
        public FastReader() {
            br = new BufferedReader(new InputStreamReader(System.in));
        }
 
        String next() {
            while (root == null || !root.hasMoreTokens()) {
                try {
                    root = new StringTokenizer(br.readLine());
                } catch (Exception addd) {
                    addd.printStackTrace();
                }
            }
            return root.nextToken();
        }
 
        int nextInt() {
            return Integer.parseInt(next());
        }
 
        double nextDouble() {
            return Double.parseDouble(next());
        }
 
        long nextLong() {
            return Long.parseLong(next());
        }
 
        String nextLine() {
            String str = "";
            try {
                str = br.readLine();
            } catch (Exception addd) {
                addd.printStackTrace();
            }
            return str;
        }
         public int[] nextIntArray(int arraySize) {
            int array[] = new int[arraySize];
 
            for (int i = 0; i < arraySize; i++) {
                array[i] = nextInt();
            }
 
            return array;
        }
}
public static class Pair implements Comparable<Pair>{
         long x;
        int  y;
        public Pair(){}
        public Pair(long x1, int y1) {
            x=x1;
            y=y1;
        }
        @Override
        public int hashCode() {
            return (int)(x + 31 * y);
        }
        public String toString() {
            return x + " " + y;
        }
        @Override
        public boolean equals(Object o){
            if (o == this) return true;
            if (o.getClass() != getClass()) return false;
            Pair t = (Pair)o;
            return t.x == x && t.y == y;
        }
        public  int compareTo(Pair o)
        {
            return (int)(x-o.x);
        }
}

}







