Problem: You are given n strings ti. Each string has cost ci.

Let's define the function of string <image>, where ps, i is the number of occurrences of s in ti, |s| is the length of the string s. Find the maximal value of function f(s) over all strings.

Note that the string s is not necessarily some string from t.

Input

The first line contains the only integer n (1 ≤ n ≤ 105) — the number of strings in t.

Each of the next n lines contains contains a non-empty string ti. ti contains only lowercase English letters.

It is guaranteed that the sum of lengths of all strings in t is not greater than 5·105.

The last line contains n integers ci ( - 107 ≤ ci ≤ 107) — the cost of the i-th string.

Output

Print the only integer a — the maximal value of the function f(s) over all strings s. Note one more time that the string s is not necessarily from t.

Examples

Input

2
aa
bb
2 1


Output

4


Input

2
aa
ab
2 1


Output

5

Solution (in CPP):

Solution:
#include <bits/stdc++.h>
using namespace std;
const int INF = 0x3f3f3f3f;
const long long int llINF = 0x3f3f3f3f3f3f3f;
const int MOD = 1e9 + 7;
long long int ans;
long long int cost[100100];
int A;
const int N = 2000000;
const int NS = 100100;
int en = 1;
int ns;
vector<int> S[NS];
struct node {
  int l, r, p;
  int si, suf;
  map<int, int> f;
  node() : l(0), r(-1), p(0), si(0) {}
  node(int L, int R, int P, int S) : l(L), r(R), p(P), si(S) {}
  inline int len() { return r - l + 1; }
  inline int operator[](int i) { return S[si][i + l]; }
} t[N];
inline int new_node(int L, int R, int P, int S) {
  t[en] = node(L, R, P, S);
  t[en].f.clear();
  return en++;
}
void clear() {
  en = 1;
  t[0].f.clear();
}
void build(vector<int> &s, int k) {
  s.push_back(26 + k);
  S[k] = s;
  int n = s.size();
  int i, cn, cd;
  i = cn = cd = 0;
  for (int j = 0; j < n; j++) {
    for (; i <= j; i++) {
      if (cd == t[cn].len() && t[cn].f.count(s[j])) {
        cn = t[cn].f[s[j]];
        cd = 0;
      }
      if (cd < t[cn].len() && t[cn][cd] == s[j]) {
        cd++;
        break;
      }
      if (cd == t[cn].len()) {
        t[cn].f[s[j]] = new_node(j, n - 1, cn, k);
        if (cn) {
          cn = t[cn].suf;
          cd = t[cn].len();
        }
      } else {
        int mid = new_node(t[cn].l, t[cn].l + cd - 1, t[cn].p, t[cn].si);
        t[t[mid].p].f[t[mid][0]] = mid;
        t[mid].f[s[j]] = new_node(j, n - 1, mid, k);
        t[mid].f[t[cn][cd]] = cn;
        t[cn].p = mid;
        t[cn].l += cd;
        if (ns) t[ns].suf = mid;
        cn = t[mid].p;
        int g;
        if (cn) {
          cn = t[cn].suf;
          g = j - cd;
        } else {
          g = i + 1;
        }
        while (g < j && g + t[t[cn].f[s[g]]].len() <= j) {
          cn = t[cn].f[s[g]];
          g += t[cn].len();
        }
        if (g == j) {
          t[mid].suf = cn;
          cd = t[cn].len();
          ns = 0;
        } else {
          ns = mid;
          cn = t[cn].f[s[g]];
          cd = j - g;
        }
      }
    }
  }
}
long long int dfs(int cn, long long int d) {
  long long int res = 0;
  bool any = false;
  for (auto x : t[cn].f) {
    any = true;
    res += dfs(x.second, d + t[cn].len());
  }
  int cd = t[cn].len() - 1;
  if (!any) {
    int tipo = t[cn][cd] - 26;
    res = cost[tipo];
    if (t[cn].len() > 1) {
      ans = max(ans, res * (d + t[cn].len() - 1));
    }
  } else {
    ans = max(ans, res * (d + t[cn].len()));
  }
  return res;
}
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  cout.tie(NULL);
  int n;
  cin >> n;
  A = n;
  for (int i = 0; i < n; i++) {
    string t;
    cin >> t;
    vector<int> v;
    v.clear();
    for (auto x : t) v.push_back(x - 'a');
    build(v, i);
  }
  for (int i = 0; i < n; i++) cin >> cost[i];
  long long int x = dfs(0, 0);
  cout << ans << endl;
}