#include <bits/stdc++.h>
using namespace std;
inline namespace Infinity {
inline namespace Constant {
constexpr const int eof = EOF;
constexpr const int maxint = INT_MAX;
constexpr const long long maxll = LLONG_MAX;
}  // namespace Constant
inline namespace TypeDefine {
using uint = unsigned int;
using ull = unsigned long long int;
using ld = long double;
template <typename T = vector<int>>
using iter = typename T::iterator;
template <typename T = int>
using vector_vector = vector<vector<T>>;
template <typename T = int, typename U = int>
using vector_pair = vector<pair<T, U>>;
}  // namespace TypeDefine
inline namespace IO {
const char CR = '\n';
const char SP = ' ';
inline void write(const int n) { printf("%d", n); }
inline void write(const unsigned n) { printf("%u", n); }
inline void write(const long long n) { cout << n; }
inline void write(const unsigned long long n) { cout << n; }
inline void writef(const double a, const int n = 15) { printf("%.*f", n, a); }
inline void writef(const long double a, const int n = 18) {
  cout << setprecision(n) << fixed << a;
}
inline void write(const char c) { printf("%c", c); }
inline void write(const char s[]) { printf("%s", s); }
inline void write(const string &s) { printf("%s", s.data()); }
inline void write(const pair<int, int> &p) {
  printf("%d %d", p.first, p.second);
}
template <class T>
inline void write(const T a) {
  for (auto i = a.begin(); i != a.end(); i++) {
    if (i != a.begin()) write(SP);
    write(*i);
  }
}
template <typename T>
inline void writer(T begin, T end) {
  for (write(*begin++); begin != end; ++begin) write(SP), write(*begin);
}
inline void writeln() { write(CR); }
template <typename T>
inline void writeln(const T &a) {
  write(a);
  write(CR);
}
inline void writefln(const double a, int n) {
  printf("%.*f", n, a);
  write(CR);
}
inline void writefln(const long double a, int n = 18) {
  cout << setprecision(n) << fixed << a << endl;
}
inline void writesln(const int *a, const int l, const int r) {
  for (int i = l; i <= r; i++) printf("%d ", a[i]);
  writeln(CR);
}
template <typename T>
inline void writerln(T begin, T end) {
  for (write(*begin++); begin != end; ++begin) write(SP), write(*begin);
  write(CR);
}
template <class T>
inline void writelns(const T &a) {
  for (auto n : a) writeln(n);
}
template <typename T, typename... types>
inline void write(const T &a, const types &...args) {
  write(a);
  write(args...);
}
template <typename... types>
inline void writeln(const types &...args) {
  write(args...);
  write(CR);
}
template <typename... types>
inline void writeSP(const types &...args) {
  write(args...);
  write(SP);
}
inline void writelnYN(bool b) { writeln(b ? "YES" : "NO"); }
inline void writelnyn(bool b) { writeln(b ? "Yes" : "No"); }
string caseSharpSpace(int n) { return "Case #" + to_string(n) + ": "; }
string caseNoSharpSpace(int n) { return "Case " + to_string(n) + ": "; }
string caseSharpNoSpace(int n) { return "Case #" + to_string(n) + ":"; }
string caseNoSharpNoSpace(int n) { return "Case " + to_string(n) + ":"; }
inline int read(int &n) { return scanf("%d", &n); }
inline int read(long long &n) { return cin >> n ? 1 : -1; }
template <typename T, typename... types>
inline int read(T &n, types &...args) {
  return read(n) == -1 ? -1 : read(args...) + 1;
}
inline char getcc() {
  char c;
  do c = getchar();
  while (c == ' ' || c == '\n');
  return c;
}
inline int getint() {
  int n;
  read(n);
  return n;
}
inline long long getll() {
  long long n;
  read(n);
  return n;
}
inline double getdouble() {
  double n;
  scanf("%lf", &n);
  return n;
}
inline vector<int> getints(int n) {
  vector<int> v(n);
  for (int i = 0; i < n; i++) v[i] = getint();
  return v;
}
inline vector<pair<int, int>> getpairs(int n) {
  vector<pair<int, int>> v(n);
  for (int i = 0; i < n; i++) {
    int a = getint(), b = getint();
    v[i] = pair<int, int>(a, b);
  }
  return v;
}
inline void read(string &str, const unsigned size) {
  char s[size];
  scanf("%s", s);
  str.assign(s);
}
inline string getstr(const unsigned size = 1048576) {
  string s;
  read(s, size + 1);
  return s;
}
inline string getln(const unsigned size = 1048576) {
  char s[size + 1];
  scanf("%[^\n]", s);
  getchar();
  return string(s);
}
}  // namespace IO
inline namespace Function {
inline constexpr int ctoi(const char c) { return c - '0'; }
inline constexpr char itoc(const int n) { return n + '0'; }
inline int dtoi(const double d) { return round(d); }
template <typename T>
inline constexpr bool in(T x, T l, T r) {
  return l <= x && x <= r;
}
template <class T>
inline int size(const T &a) {
  return a.size();
}
template <class T>
inline void sort(T &a) {
  std::sort(a.begin(), a.end());
}
template <class T1, class T2>
inline void sort(T1 &a, T2 comp) {
  std::sort(a.begin(), a.end(), comp);
}
template <class T1, typename T2>
inline int lbound(const T1 &a, const T2 k) {
  return lower_bound(a.begin(), a.end(), k) - a.begin();
}
template <class T1, typename T2>
inline int ubound(const T1 &a, const T2 k) {
  return upper_bound(a.begin(), a.end(), k) - a.begin();
}
template <class T1, class T2>
int count(T1 &a, T2 k) {
  return ubound(a, k) - lbound(a, k);
}
template <class T1, class T2>
int find(T1 &a, T2 k) {
  return count(a, k) ? lbound(a, k) : -1;
}
template <typename T>
inline void clear(T &a) {
  memset(a, 0, sizeof a);
}
template <typename T>
T gcd(T a, T b) {
  while (b) {
    T t = a % b;
    a = b;
    b = t;
  }
  return a;
}
template <typename T>
T lcm(T a, T b) {
  return a / gcd(a, b) * b;
}
long long qpow(long long a, long long b, long long c) {
  return b ? qpow(a * a % c, b >> 1, c) * (b & 1 ? a : 1) % c : 1;
}
template <typename T>
T exGcd(T a, T b, T &x, T &y) {
  T d = a;
  if (b) {
    d = exGcd(b, a % b, y, x);
    y -= a / b * x;
  } else {
    x = 1;
    y = 0;
  }
  return d;
}
template <typename T>
T constexpr inline mps(T l, T r, T k) {
  return ((r - (r % k + k) % k) - (l + (k - l % k) % k)) / k + 1;
}
template <typename T>
T constexpr inline sgn(T a) {
  return a == 0 ? 0 : a > 0 ? 1 : -1;
}
template <typename T>
constexpr inline T sq(T a) {
  return a * a;
}
template <typename T>
constexpr inline bool odd(T a) {
  return bool(a % 2);
}
template <typename T>
constexpr inline bool even(T a) {
  return !bool(a % 2);
}
template <typename T1, typename T2>
constexpr inline T1 smod(T1 x, T2 m) {
  return x > m ? x - m : x + m < m ? x + m : x;
}
template <typename T, typename F>
T dichotomy(T l, T r, F check, T prec = 1) {
  while (r - l > prec) {
    T m = l + (r - l) / 2;
    check(m) ? l = m : r = m;
  }
  return l;
}
bool contains(const string &s, const string &t) {
  return s.find(t) != string::npos;
}
template <typename T>
bool contains(const T &s, typename T::value_type t) {
  for (typename T::value_type c : s)
    if (c == t) return true;
  return false;
}
}  // namespace Function
}  // namespace Infinity
int main(int, char *[]) {
  int n = getint();
  vector<int> a = getints(n);
  vector<string> s(n), r(n);
  for (int i = 0; i < n; i++) {
    cin >> s[i];
    r[i].resize(s[i].size());
    reverse_copy(s[i].begin(), s[i].end(), r[i].begin());
  }
  vector<vector<unsigned long long>> d(n, vector<unsigned long long>(2, -1));
  d[0][0] = 0;
  d[0][1] = a[0];
  for (int i = 1; i < n; i++) {
    if (s[i] >= s[i - 1]) {
      d[i][0] = min(d[i][0], d[i - 1][0]);
    }
    if (s[i] >= r[i - 1]) {
      d[i][0] = min(d[i][0], d[i - 1][1]);
    }
    if (r[i] >= s[i - 1] && d[i - 1][0] != -1) {
      d[i][1] = min(d[i][1], d[i - 1][0] + a[i]);
    }
    if (r[i] >= r[i - 1] && d[i - 1][1] != -1) {
      d[i][1] = min(d[i][1], d[i - 1][1] + a[i]);
    }
  }
  writeln((long long)(min(d.back().front(), d.back().back())));
  return 0;
}

### Prompt

Your task is to create 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;
inline namespace Infinity {
inline namespace Constant {
constexpr const int eof = EOF;
constexpr const int maxint = INT_MAX;
constexpr const long long maxll = LLONG_MAX;
}  // namespace Constant
inline namespace TypeDefine {
using uint = unsigned int;
using ull = unsigned long long int;
using ld = long double;
template <typename T = vector<int>>
using iter = typename T::iterator;
template <typename T = int>
using vector_vector = vector<vector<T>>;
template <typename T = int, typename U = int>
using vector_pair = vector<pair<T, U>>;
}  // namespace TypeDefine
inline namespace IO {
const char CR = '\n';
const char SP = ' ';
inline void write(const int n) { printf("%d", n); }
inline void write(const unsigned n) { printf("%u", n); }
inline void write(const long long n) { cout << n; }
inline void write(const unsigned long long n) { cout << n; }
inline void writef(const double a, const int n = 15) { printf("%.*f", n, a); }
inline void writef(const long double a, const int n = 18) {
  cout << setprecision(n) << fixed << a;
}
inline void write(const char c) { printf("%c", c); }
inline void write(const char s[]) { printf("%s", s); }
inline void write(const string &s) { printf("%s", s.data()); }
inline void write(const pair<int, int> &p) {
  printf("%d %d", p.first, p.second);
}
template <class T>
inline void write(const T a) {
  for (auto i = a.begin(); i != a.end(); i++) {
    if (i != a.begin()) write(SP);
    write(*i);
  }
}
template <typename T>
inline void writer(T begin, T end) {
  for (write(*begin++); begin != end; ++begin) write(SP), write(*begin);
}
inline void writeln() { write(CR); }
template <typename T>
inline void writeln(const T &a) {
  write(a);
  write(CR);
}
inline void writefln(const double a, int n) {
  printf("%.*f", n, a);
  write(CR);
}
inline void writefln(const long double a, int n = 18) {
  cout << setprecision(n) << fixed << a << endl;
}
inline void writesln(const int *a, const int l, const int r) {
  for (int i = l; i <= r; i++) printf("%d ", a[i]);
  writeln(CR);
}
template <typename T>
inline void writerln(T begin, T end) {
  for (write(*begin++); begin != end; ++begin) write(SP), write(*begin);
  write(CR);
}
template <class T>
inline void writelns(const T &a) {
  for (auto n : a) writeln(n);
}
template <typename T, typename... types>
inline void write(const T &a, const types &...args) {
  write(a);
  write(args...);
}
template <typename... types>
inline void writeln(const types &...args) {
  write(args...);
  write(CR);
}
template <typename... types>
inline void writeSP(const types &...args) {
  write(args...);
  write(SP);
}
inline void writelnYN(bool b) { writeln(b ? "YES" : "NO"); }
inline void writelnyn(bool b) { writeln(b ? "Yes" : "No"); }
string caseSharpSpace(int n) { return "Case #" + to_string(n) + ": "; }
string caseNoSharpSpace(int n) { return "Case " + to_string(n) + ": "; }
string caseSharpNoSpace(int n) { return "Case #" + to_string(n) + ":"; }
string caseNoSharpNoSpace(int n) { return "Case " + to_string(n) + ":"; }
inline int read(int &n) { return scanf("%d", &n); }
inline int read(long long &n) { return cin >> n ? 1 : -1; }
template <typename T, typename... types>
inline int read(T &n, types &...args) {
  return read(n) == -1 ? -1 : read(args...) + 1;
}
inline char getcc() {
  char c;
  do c = getchar();
  while (c == ' ' || c == '\n');
  return c;
}
inline int getint() {
  int n;
  read(n);
  return n;
}
inline long long getll() {
  long long n;
  read(n);
  return n;
}
inline double getdouble() {
  double n;
  scanf("%lf", &n);
  return n;
}
inline vector<int> getints(int n) {
  vector<int> v(n);
  for (int i = 0; i < n; i++) v[i] = getint();
  return v;
}
inline vector<pair<int, int>> getpairs(int n) {
  vector<pair<int, int>> v(n);
  for (int i = 0; i < n; i++) {
    int a = getint(), b = getint();
    v[i] = pair<int, int>(a, b);
  }
  return v;
}
inline void read(string &str, const unsigned size) {
  char s[size];
  scanf("%s", s);
  str.assign(s);
}
inline string getstr(const unsigned size = 1048576) {
  string s;
  read(s, size + 1);
  return s;
}
inline string getln(const unsigned size = 1048576) {
  char s[size + 1];
  scanf("%[^\n]", s);
  getchar();
  return string(s);
}
}  // namespace IO
inline namespace Function {
inline constexpr int ctoi(const char c) { return c - '0'; }
inline constexpr char itoc(const int n) { return n + '0'; }
inline int dtoi(const double d) { return round(d); }
template <typename T>
inline constexpr bool in(T x, T l, T r) {
  return l <= x && x <= r;
}
template <class T>
inline int size(const T &a) {
  return a.size();
}
template <class T>
inline void sort(T &a) {
  std::sort(a.begin(), a.end());
}
template <class T1, class T2>
inline void sort(T1 &a, T2 comp) {
  std::sort(a.begin(), a.end(), comp);
}
template <class T1, typename T2>
inline int lbound(const T1 &a, const T2 k) {
  return lower_bound(a.begin(), a.end(), k) - a.begin();
}
template <class T1, typename T2>
inline int ubound(const T1 &a, const T2 k) {
  return upper_bound(a.begin(), a.end(), k) - a.begin();
}
template <class T1, class T2>
int count(T1 &a, T2 k) {
  return ubound(a, k) - lbound(a, k);
}
template <class T1, class T2>
int find(T1 &a, T2 k) {
  return count(a, k) ? lbound(a, k) : -1;
}
template <typename T>
inline void clear(T &a) {
  memset(a, 0, sizeof a);
}
template <typename T>
T gcd(T a, T b) {
  while (b) {
    T t = a % b;
    a = b;
    b = t;
  }
  return a;
}
template <typename T>
T lcm(T a, T b) {
  return a / gcd(a, b) * b;
}
long long qpow(long long a, long long b, long long c) {
  return b ? qpow(a * a % c, b >> 1, c) * (b & 1 ? a : 1) % c : 1;
}
template <typename T>
T exGcd(T a, T b, T &x, T &y) {
  T d = a;
  if (b) {
    d = exGcd(b, a % b, y, x);
    y -= a / b * x;
  } else {
    x = 1;
    y = 0;
  }
  return d;
}
template <typename T>
T constexpr inline mps(T l, T r, T k) {
  return ((r - (r % k + k) % k) - (l + (k - l % k) % k)) / k + 1;
}
template <typename T>
T constexpr inline sgn(T a) {
  return a == 0 ? 0 : a > 0 ? 1 : -1;
}
template <typename T>
constexpr inline T sq(T a) {
  return a * a;
}
template <typename T>
constexpr inline bool odd(T a) {
  return bool(a % 2);
}
template <typename T>
constexpr inline bool even(T a) {
  return !bool(a % 2);
}
template <typename T1, typename T2>
constexpr inline T1 smod(T1 x, T2 m) {
  return x > m ? x - m : x + m < m ? x + m : x;
}
template <typename T, typename F>
T dichotomy(T l, T r, F check, T prec = 1) {
  while (r - l > prec) {
    T m = l + (r - l) / 2;
    check(m) ? l = m : r = m;
  }
  return l;
}
bool contains(const string &s, const string &t) {
  return s.find(t) != string::npos;
}
template <typename T>
bool contains(const T &s, typename T::value_type t) {
  for (typename T::value_type c : s)
    if (c == t) return true;
  return false;
}
}  // namespace Function
}  // namespace Infinity
int main(int, char *[]) {
  int n = getint();
  vector<int> a = getints(n);
  vector<string> s(n), r(n);
  for (int i = 0; i < n; i++) {
    cin >> s[i];
    r[i].resize(s[i].size());
    reverse_copy(s[i].begin(), s[i].end(), r[i].begin());
  }
  vector<vector<unsigned long long>> d(n, vector<unsigned long long>(2, -1));
  d[0][0] = 0;
  d[0][1] = a[0];
  for (int i = 1; i < n; i++) {
    if (s[i] >= s[i - 1]) {
      d[i][0] = min(d[i][0], d[i - 1][0]);
    }
    if (s[i] >= r[i - 1]) {
      d[i][0] = min(d[i][0], d[i - 1][1]);
    }
    if (r[i] >= s[i - 1] && d[i - 1][0] != -1) {
      d[i][1] = min(d[i][1], d[i - 1][0] + a[i]);
    }
    if (r[i] >= r[i - 1] && d[i - 1][1] != -1) {
      d[i][1] = min(d[i][1], d[i - 1][1] + a[i]);
    }
  }
  writeln((long long)(min(d.back().front(), d.back().back())));
  return 0;
}

```