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 javafx.util.Pair;
 
import java.io.*;
import java.lang.reflect.Array;
import java.math.BigInteger;
import java.text.DecimalFormat;
import java.util.*;
import java.util.Map.Entry;

import java.io.IOException;
import java.io.InputStream;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.InputMismatchException;
 
public class abc {
		
	static String[] temp= {"1110111", "0010010", "1011101", "1011011", "0111010", "1101011", "1101111", "1010010", "1111111", "1111011"};
	static char[] ans;
	
		public static void main(String[] args) throws Exception {
			// TODO Auto-generated method stub
			InputReader s = new InputReader(System.in);
			PrintWriter p = new PrintWriter(System.out);
			int n=s.nextInt();
			int[] arr=new int[n];
			for(int i=0 ; i<arr.length ; i++) {
				arr[i]=s.nextInt();
			}
			String[] words=new String[n];
			for(int i=0 ; i<n ; i++) {
				words[i]=s.nextLine();
			}
			long[][] dp=new long[n][2];
			dp[0][0]=0;
			dp[0][1]=arr[0];
			String prev=words[0];
			String revprev=reverse(prev);
			for(int i=1 ; i<dp.length ; i++) {
				String curr=words[i];
				String revcurr=reverse(curr);
				dp[i][0]=Long.MAX_VALUE;
				dp[i][1]=Long.MAX_VALUE;
				if(curr.compareTo(prev)>=0) {
					dp[i][0]=dp[i-1][0];
				}
				if(revcurr.compareTo(prev)>=0 && dp[i-1][0]!=Long.MAX_VALUE) {
					dp[i][1]=dp[i-1][0]+(long)arr[i];
				}
				if(curr.compareTo(revprev)>=0) {
					dp[i][0]=Math.min(dp[i][0], dp[i-1][1]);
				}
				if(revcurr.compareTo(revprev)>=0 && dp[i-1][1]!=Long.MAX_VALUE) {
					dp[i][1]=Math.min(dp[i][1], dp[i-1][1]+(long)arr[i]);
				}
				prev=curr;
				revprev=revcurr;
			}
			long ans=Math.min(dp[n-1][0], dp[n-1][1]);
			if(ans==Long.MAX_VALUE) {
				 System.out.println(-1);
			}else {
				System.out.println(ans);
			}
			
			p.flush();
			p.close();
		}
	 
		
		public static String reverse(String str) {
			String curr="";
			for(int i=0 ; i<str.length() ; i++) {
				curr=str.charAt(i)+curr;
			}
			return curr;
		}
		
		
		public class Pair implements Comparable<Pair>{
	        int x;
	        int f;
	        public Pair(int a , int b){
	            x=a;
	            f=b;
	        }
	        
	        public int comprae(Pair p){
	            return p.f-this.f;
	        }

			@Override
			public int compareTo(Pair arg0) {
				// TODO Auto-generated method stub
				return 0;
			}
	    }
	    
		
		
		
		
		
		public static int helper(String[] arr , int pos , int k , int[][] dp) {
			if(pos==arr.length && k==0) {
				return 1;
			}
			if(pos==arr.length || k==0) return 0;
			if(dp[pos][k]!=-1) return dp[pos][k];
			for(int i=9 ; i>=0 ; i--) {
				String s1=temp[i];
				String curr=arr[pos];
				int con=convert(curr,s1);
				if(con==-1 || con>k) {
					continue;
				}
				ans[pos]=(char)(i+'0');
				int z=helper(arr,pos+1,k-con,dp);
				if(z==1) {
					dp[pos][k]=1;
					return 1;
				}
				ans[pos]='z';
			}
			return dp[pos][k]=0;
		}
		
		public static int convert(String curr , String s1) {
			int count=0;
			for(int i=0 ; i<7 ; i++) {
				char c1=curr.charAt(i);
				char c2=s1.charAt(i);
				if(c1=='1' && c2=='0') {
					return -1;
				}
				if(c1=='0' && c2=='1') {
					count++;
				}
			}
			return count;
		}
		
		static long power(long x, long y) 
	    { 
			long temp=0l; 
	        if( y == 0) 
	            return 1l; 
	        temp = power(x, y/2);  
	          
	        if (y%2 == 0) 
	            return temp*temp; 
	        else
	        { 
	            if(y > 0) 
	                return x * temp * temp; 
	            else
	                return (temp * temp) / x; 
	        } 
	    }  
		
		
		public static boolean helper(int[] arr1 , int[] arr2) {
			boolean[] temp=new boolean[3];
			if(arr1.length==0) return true;
			if(arr1[0]!=arr2[0]) return false;
			temp[arr1[0]+1]=true;
			for(int i=1 ; i<arr1.length ; i++) {
				
				if(arr1[i]==arr2[i]) {
					
				}else if(arr1[i]<arr2[i]) {
					if(!temp[2]) {
						return false;
					}
				}else {
					if(!temp[0]) {
						return false;
					}
				}
				temp[arr1[i]+1]=true;
			}
			return true;
		}
		
	
		public static ArrayList Divisors(long n) {
			ArrayList<Long> div = new ArrayList<>();
	 
			for (long i = 1; i <= Math.sqrt(n); i++) {
				if (n % i == 0) {
					div.add(i);
	 
					if (n / i != i)
						div.add(n / i);
				}
			}
			return div;
		}
	 
		public static int BinarySearch(long[] a, long k) {
			int n = a.length;
			int i = 0, j = n - 1;
			int mid = 0;
	 
			if (k < a[0])
				return 0;
			else if (k >= a[n - 1])
				return n;
			else {
				while (j - i > 1) {
					mid = (i + j) / 2;
	 
					if (k >= a[mid])
						i = mid;
					else
						j = mid;
				}
			}
	 
			return i + 1;
		}
	 
		public static long GCD(long a, long b) {
			if (b == 0)
				return a;
			else
				return GCD(b, a % b);
		}
	 
		public static long LCM(long a, long b) {
			return (a * b) / GCD(a, b);
		}
	 
		static class InputReader {
	 
			private final InputStream stream;
			private final byte[] buf = new byte[8192];
			private int curChar, snumChars;
			private SpaceCharFilter filter;
	 
			public InputReader(InputStream stream) {
				this.stream = stream;
			}
	 
			public int snext() {
				if (snumChars == -1)
					throw new InputMismatchException();
				if (curChar >= snumChars) {
					curChar = 0;
					try {
						snumChars = stream.read(buf);
					} catch (IOException e) {
						throw new InputMismatchException();
					}
					if (snumChars <= 0)
						return -1;
				}
				return buf[curChar++];
			}
	 
			static class pair implements Comparable<pair> {
				Integer x, y;
	 
				pair(int x, int y) {
					this.x = x;
					this.y = y;
				}
	 
				public int compareTo(pair o) {
					int result = x.compareTo(o.x);
					if (result == 0)
						result = y.compareTo(o.y);
	 
					return result;
				}
	 
				public String toString() {
					return x + " " + y;
				}
	 
				public boolean equals(Object o) {
					if (o instanceof pair) {
						pair p = (pair) o;
						return p.x - x == 0 && p.y - y == 0;
					}
					return false;
				}
	 
				public int hashCode() {
					return new Long(x).hashCode() * 31 + new Long(y).hashCode();
				}
			}
	 
			public int nextInt() {
				int c = snext();
				while (isSpaceChar(c)) {
					c = snext();
				}
				int sgn = 1;
				if (c == '-') {
					sgn = -1;
					c = snext();
				}
				int res = 0;
				do {
					if (c < '0' || c > '9')
						throw new InputMismatchException();
					res *= 10;
					res += c - '0';
					c = snext();
				} while (!isSpaceChar(c));
				return res * sgn;
			}
	 
			public long nextLong() {
				int c = snext();
				while (isSpaceChar(c)) {
					c = snext();
				}
				int sgn = 1;
				if (c == '-') {
					sgn = -1;
					c = snext();
				}
				long res = 0;
				do {
					if (c < '0' || c > '9')
						throw new InputMismatchException();
					res *= 10;
					res += c - '0';
					c = snext();
				} while (!isSpaceChar(c));
				return res * sgn;
			}
	 
			public int[] nextIntArray(int n) {
				int a[] = new int[n];
				for (int i = 0; i < n; i++) {
					a[i] = nextInt();
				}
				return a;
			}
	 
			public String readString() {
				int c = snext();
				while (isSpaceChar(c)) {
					c = snext();
				}
				StringBuilder res = new StringBuilder();
				do {
					res.appendCodePoint(c);
					c = snext();
				} while (!isSpaceChar(c));
				return res.toString();
			}
	 
			public String nextLine() {
				int c = snext();
				while (isSpaceChar(c))
					c = snext();
				StringBuilder res = new StringBuilder();
				do {
					res.appendCodePoint(c);
					c = snext();
				} while (!isEndOfLine(c));
				return res.toString();
			}
	 
			public boolean isSpaceChar(int c) {
				if (filter != null)
					return filter.isSpaceChar(c);
				return c == ' ' || c == '\n' || c == '\r' || c == '\t' || c == -1;
			}
	 
			private boolean isEndOfLine(int c) {
				return c == '\n' || c == '\r' || c == -1;
			}
	 
			public interface SpaceCharFilter {
				public boolean isSpaceChar(int ch);
			}
		}
	 
		static class CodeX {
			public static void sort(long arr[]) {
				merge_sort(arr, 0, arr.length - 1);
			}
	 
			private static void merge_sort(long A[], long start, long end) {
				if (start < end) {
					long mid = (start + end) / 2;
					merge_sort(A, start, mid);
					merge_sort(A, mid + 1, end);
					merge(A, start, mid, end);
				}
	 
			}
	 
			private static void merge(long A[], long start, long mid, long end) {
				long p = start, q = mid + 1;
				long Arr[] = new long[(int) (end - start + 1)];
				long k = 0;
	 
				for (int i = (int) start; i <= end; i++) {
					if (p > mid)
						Arr[(int) k++] = A[(int) q++];
	 
					else if (q > end)
						Arr[(int) k++] = A[(int) p++];
	 
					else if (A[(int) p] < A[(int) q])
						Arr[(int) k++] = A[(int) p++];
	 
					else
						Arr[(int) k++] = A[(int) q++];
				}
				for (int i = 0; i < k; i++) {
					A[(int) start++] = Arr[i];
				}
	 
			}
	 
		}
	 
	}

	
