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.io.*;
 
 
 public class gfg  {
 
	static PrintWriter out = new PrintWriter(System.out);
	
	public static void main(String[] args) throws IOException{
		Reader.init(System.in);
		int t = 1;//Reader.nextInt();
		while(t-->0){
			solve();
		}
		out.close();
	}
   // static int n =0,m=0;
	static long mod =1000000007;
	static int MOD = 1000000007;
	static int[] dx = {0,0,1,-1};
	static int[] dy = {1,-1,0,0};
	//static int d8i[8]={-1, -1, 0, 1, 1, 1, 0, -1}, d8j[8]={0, 1, 1, 1, 0, -1, -1, -1};
	static ArrayList<Integer>[] graph; 
	static int n=0,m=0,k=0;
	static int first=0,dist=0,max=0;
	static String[] arr;
	static long[] cost ;
	static long inf = Long.MAX_VALUE/4;
 
	//static HashSet<Integer> set = new HashSet<>();
	static HashMap<Integer,Integer> map = new HashMap<>();
	static Long[][] dp = new Long[200000][3];
	static void solve() throws IOException{
		n = Reader.nextInt();
	   	arr = new String[n];
	   	cost = new long[n];
	   	for(int i=0;i<n;i++)
	   		cost[i] = Reader.nextLong();
	   	for(int i=0;i<n;i++)
	   		arr[i] = Reader.next();
	   	long ans = rec(0,0);
	   	if(ans>=inf)
	   		System.out.println(-1);
	   	else
	   		System.out.println(ans);


	}

	static long rec(int ind, int prev_rev){
		if(ind>=n)
			return 0;
		if(dp[ind][prev_rev]!=null)
			return dp[ind][prev_rev];
		if(ind == 0){
			// either two
			return dp[ind][prev_rev]=Math.min(rec(ind+1,0),cost[ind]+rec(ind+1,1));
		}
		else{
			if(prev_rev==1){
				long min = inf;
				String prevstr = reverse(arr[ind-1]);
				String cur = arr[ind];
				String currev = reverse(arr[ind]);
				int value1 = prevstr.compareTo(cur);
				int value2 = prevstr.compareTo(currev);
				// we need value <= 0
				if(value1<=0){
					min = Math.min(min,rec(ind+1,0));
				}
				if(value2<=0){
					min= Math.min(min,cost[ind]+rec(ind+1,1));
				}
				return dp[ind][prev_rev]=min;

			}
			else{
				long min = inf;
				String prevstr = arr[ind-1];
				String cur = arr[ind];
				String currev = reverse(arr[ind]);
				int value1 = prevstr.compareTo(cur);
				int value2 = prevstr.compareTo(currev);
				// we need value <= 0
				if(value1<=0){
					min = Math.min(min,rec(ind+1,0));
				}
				if(value2<=0){
					min= Math.min(min,cost[ind]+rec(ind+1,1));
				}
				return dp[ind][prev_rev]=min;
			}
		}
	}
	
	static String reverse(String s){
		StringBuilder str = new StringBuilder(s);
		str = str.reverse();
		String x = new String(str);
		return x;
	}
 
	// ggratest common divisor println
	static int gcd(int a , int b){
		if(b==0)
			return a;
		else
			return gcd(b,a%b);
	}
 
	// least common multiple
	static int lcm(int a, int b){
		return (a*b)/gcd(a,b);
	}
	
	// a^b
	static long fastpow(long a, long b){
		long res = 1;a=a%mod;
		while(b>0){
			if(b%2==1)
				res = (res*a)%mod;
			a = (a*a)%mod;
			b = b>>1;
		}
		return res;
	}
 
 
	
 
 
	// SEGMENT TREE
	// static void update(int cur,int val, int ind, int l, int h){
	//     if(l==h){
	//         seg[cur] = val;
	//         return;
	//     }
	//     int mid = (l+h)/2;
	//     if(ind<=mid){
	//         update(2*cur+1,val,ind,l,mid);
	//     }
	//     else{
	//         update(2*cur+2,val,ind,mid+1,h);
	//     }
	//     seg[cur] = seg[2*cur+1] + seg[2*cur+2];
	// }
	
	// static int query(int cur, int l, int h , int reql, int reqh){
	//     if(reql>reqh || l>h)
	//         return 0;
	//     if(l==reql && h==reqh)
	//         return seg[cur];
	//     int mid = (l+h)/2;
	//     return query(2*cur+1,l,mid,reql,Math.min(reqh,mid)) + query(2*cur+2,mid+1,h,Math.max(mid+1,reql),reqh);
	//}
 
	// priority queue with comparator
	// PriorityQueue<Interval> pq = new PriorityQueue<>(new Comparator<Interval>() {
	//             public int compare(Interval a, Interval b){
	//                 int diff1 = a.r - a.l;
	//                 int diff2 = b.r - b.l;
 
	//                 if(diff1 == diff2){
	//                     return a.l - b.l;
	//                 }
 
	//                 return diff2 - diff1;
	//             }
	// });
}
 
class trienode{
		int v;
		trienode[] child ;
		boolean leaf;
		int cnt;
		int p=0;
		trienode(int vv){
			child = new trienode[2];
			leaf=false;
			cnt=0;
			v=vv;
		}
		trienode(){
			child = new trienode[2];
			leaf=false;
			cnt=0;
			v=0;
		}
	}
class Reader {
	static BufferedReader reader;
	static StringTokenizer tokenizer;
 
	/** call this method to initialize reader for InputStream */
	static void init(InputStream input) {
		reader = new BufferedReader(
					 new InputStreamReader(input) );
		tokenizer = new StringTokenizer("");
	}
 
	/** get next word */
	static String next() throws IOException {
		while ( ! tokenizer.hasMoreTokens() ) {
			//TODO add check for eof if necessary
			tokenizer = new StringTokenizer(
				   reader.readLine() );
		}
		return tokenizer.nextToken();
	}
 
	static int nextInt() throws IOException {
		return Integer.parseInt( next() );
	}
	static long nextLong() throws IOException {
		return Long.parseLong( next() );
	}
	
	static double nextDouble() throws IOException {
		return Double.parseDouble( next() );
	}
}
class newcomparator implements Comparator<Integer>{
	//@Override
	public int compare(Integer a, Integer  b){
		return a<=b?1:-1;
	}
 }
class node {
	int f;
	int s;// cost to rreach\
	node(int ff,int ss){
		f=ff;
		s=ss;
	}
}
class mergesort{
	static void sort(int l, int h, int[] arr){
		if(l<h){
			int mid = (l+h)/2;
			
			sort(l,mid,arr);
			sort(mid+1,h,arr);
			merge(l,mid,h,arr);
		}
	}
	static void merge(int l, int m , int h , int [] arr){
 
		int[] left = new int[m-l+1];
		int[] right = new int[h-m];
		for(int i= 0 ;i< m-l+1;i++){
			left[i] = arr[l+i];
		}
		for(int i=0;i<h-m;i++){
			right[i] = arr[m+1+i];
		}
		//now left and right arrays are assumed to be sorted and we have tp merge them together 
		// int the original aaray.
		int i=l;
		int lindex = 0;
		int rindex =  0;
		
		while(lindex<m-l+1 && rindex<h-m){
			if(left[lindex]<=right[rindex]){
				arr[i]=left[lindex];
				lindex++;
				i++;
			}
			else{
				arr[i]=right[rindex];
				rindex++;
				i++;
			}
		}
		while(lindex<m-l+1){
			arr[i]=left[lindex];
			lindex++;
			i++;
		}
		while(rindex<h-m){
			arr[i]=right[rindex];
			rindex++;
			i++;
		}
	}
	
}
