#include <bits/stdc++.h>
using namespace std;
const int OO = 0x3f3f3f3f;
const int _OO = -1 * OO;
const double EPS = (1e-9);
const double pi = 3.14159265;
using namespace std;
int dcmp(double x, double y) { return fabs(x - y) <= EPS ? 0 : x < y ? -1 : 1; }
int dx[] = {1, -1, 0, 0};
int dy[] = {0, 0, -1, 1};
int dx8[8] = {1, 1, 1, 0, 0, -1, -1, -1};
int dy8[8] = {1, -1, 0, 1, -1, 1, -1, 0};
long long gcd(long long int a, long long int b) {
  if (b == 0) return a;
  return gcd(b, a % b);
}
long long lcm(int a, int b) { return (a / gcd(a, b)) * b; }
const int N = 1e5 + 7;
string s[N][3];
bool GO[N][2][2];
int arr[N], n;
long long dp[N][2];
long long solve(int idx, bool lst) {
  if (idx == n) {
    return (lst == 1 ? arr[idx - 1] : 0);
  }
  long long& ret = dp[idx][lst];
  if (~ret) return ret;
  ret = 1e18;
  if (GO[idx][0][lst]) {
    ret = min(ret, solve(idx + 1, 0) + (lst == 1 ? arr[idx - 1] : 0));
  }
  if (GO[idx][1][lst]) {
    ret = min(ret, solve(idx + 1, 1) + (lst == 1 ? arr[idx - 1] : 0));
  }
  return ret;
}
int main() {
  ios_base::sync_with_stdio(0), cin.tie(0), cout.tie(0);
  memset(dp, -1, sizeof(dp));
  cin >> n;
  for (int i = 0; i < n; i++) cin >> arr[i];
  for (int i = 0; i < n; i++) {
    cin >> s[i][0];
    s[i][1] = s[i][0];
    reverse(s[i][1].begin(), s[i][1].end());
  }
  for (int i = 1; i < n; i++) {
    if (s[i][0] >= s[i - 1][0]) GO[i][0][0] = 1;
    if (s[i][0] >= s[i - 1][1]) GO[i][0][1] = 1;
    if (s[i][1] >= s[i - 1][0]) GO[i][1][0] = 1;
    if (s[i][1] >= s[i - 1][1]) GO[i][1][1] = 1;
  }
  long long cnt = min(solve(1, 0), solve(1, 1));
  cout << (cnt >= 1e18 ? -1 : cnt);
  return 0;
}

### Prompt

Generate a CPP solution to the following problem:

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.

### Response

```cpp
#include <bits/stdc++.h>
using namespace std;
const int OO = 0x3f3f3f3f;
const int _OO = -1 * OO;
const double EPS = (1e-9);
const double pi = 3.14159265;
using namespace std;
int dcmp(double x, double y) { return fabs(x - y) <= EPS ? 0 : x < y ? -1 : 1; }
int dx[] = {1, -1, 0, 0};
int dy[] = {0, 0, -1, 1};
int dx8[8] = {1, 1, 1, 0, 0, -1, -1, -1};
int dy8[8] = {1, -1, 0, 1, -1, 1, -1, 0};
long long gcd(long long int a, long long int b) {
  if (b == 0) return a;
  return gcd(b, a % b);
}
long long lcm(int a, int b) { return (a / gcd(a, b)) * b; }
const int N = 1e5 + 7;
string s[N][3];
bool GO[N][2][2];
int arr[N], n;
long long dp[N][2];
long long solve(int idx, bool lst) {
  if (idx == n) {
    return (lst == 1 ? arr[idx - 1] : 0);
  }
  long long& ret = dp[idx][lst];
  if (~ret) return ret;
  ret = 1e18;
  if (GO[idx][0][lst]) {
    ret = min(ret, solve(idx + 1, 0) + (lst == 1 ? arr[idx - 1] : 0));
  }
  if (GO[idx][1][lst]) {
    ret = min(ret, solve(idx + 1, 1) + (lst == 1 ? arr[idx - 1] : 0));
  }
  return ret;
}
int main() {
  ios_base::sync_with_stdio(0), cin.tie(0), cout.tie(0);
  memset(dp, -1, sizeof(dp));
  cin >> n;
  for (int i = 0; i < n; i++) cin >> arr[i];
  for (int i = 0; i < n; i++) {
    cin >> s[i][0];
    s[i][1] = s[i][0];
    reverse(s[i][1].begin(), s[i][1].end());
  }
  for (int i = 1; i < n; i++) {
    if (s[i][0] >= s[i - 1][0]) GO[i][0][0] = 1;
    if (s[i][0] >= s[i - 1][1]) GO[i][0][1] = 1;
    if (s[i][1] >= s[i - 1][0]) GO[i][1][0] = 1;
    if (s[i][1] >= s[i - 1][1]) GO[i][1][1] = 1;
  }
  long long cnt = min(solve(1, 0), solve(1, 1));
  cout << (cnt >= 1e18 ? -1 : cnt);
  return 0;
}

```