#include <bits/stdc++.h>   using namespace std;   double EPS = 1e-9;   long long INF = 1000000000000000005ll;   double PI = acos(-1);   inline string IntToString(long long int a) {     char x[100];     sprintf(x,  %lld , a);     string s = x;     return s;   }   inline long long int StringToInt(string a) {     char x[100];     long long int res;     strcpy(x, a.c_str());     sscanf(x,  %lld , &res);     return res;   }   inline string uppercase(string s) {     int n = (int)s.size();     for (long long int i = 0; i < n; i++)       if (s[i] >=  a  && s[i] <=  z ) s[i] = s[i] -  a  +  A ;     return s;   }   inline string lowercase(string s) {     int n = (int)s.size();     for (long long int i = 0; i < n; i++)       if (s[i] >=  A  && s[i] <=  Z ) s[i] = s[i] -  A  +  a ;     return s;   }   bool sortcol(const vector<long long int>& v1, const vector<long long int>& v2) {     return v1[0] > v2[0];   }   int main() {     ios::sync_with_stdio(0);     cin.tie(0);     cout.tie(0);     long long int n;     cin >> n;     long long int c[n];     string r[n];     string s[n];     for (long long int i = 0; i < n; i++) cin >> c[i];     for (long long int i = 0; i < n; i++) {       cin >> r[i];       s[i] = r[i];       reverse(r[i].begin(), r[i].end());     }     long long int dp[n + 1][2];     memset(dp, 0, sizeof(dp));     dp[0][1] = c[0];     for (long long int i = 1; i < n; i++) {       dp[i][0] = INF;       dp[i][1] = INF;       if (s[i] >= s[i - 1]) {         dp[i][0] = min(dp[i - 1][0], dp[i][0]);       }       if (s[i] >= r[i - 1]) {         dp[i][0] = min(dp[i - 1][1], dp[i][0]);       }       if (r[i] >= s[i - 1]) {         dp[i][1] = min(dp[i - 1][0] + c[i], dp[i][1]);       }       if (r[i] >= r[i - 1]) {         dp[i][1] = min(dp[i - 1][1] + c[i], dp[i][1]);       }     }     long long int mi = min(dp[n - 1][0], dp[n - 1][1]);     if (mi != INF)       cout << min(dp[n - 1][0], dp[n - 1][1]) <<   n ;     else       cout << -1 <<   n ;     return 0;   }   