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 N = 2e6 + 5;
int ch[N][27], fa[N], d[N], ct, last;
void extend(int, int);
char str[N];
int len;
int pos[N], g[N];
long long val[N];
const int M = 1e5 + 5;
string s[M];
int C[M];
int c[N], id[N], cn[N];
int main() {
  int n;
  cin >> n;
  for (int i = 1; i <= n; i++) cin >> s[i];
  for (int i = 1; i <= n; i++) cin >> C[i];
  ct = last = 1;
  for (int i = 1; i <= n; i++) {
    extend(26, 0);
    for (int j = 0; j < (int)s[i].size(); j++) {
      extend(s[i][j] - 'a', C[i]);
    }
  }
  for (int i = 1; i <= len; i++)
    if (str[i] == 'z' + 1)
      g[i] = i;
    else
      g[i] = g[i - 1];
  for (int i = 1; i <= ct; i++) c[d[i]]++;
  for (int i = 1; i <= ct; i++) c[i] += c[i - 1];
  for (int i = ct; i; i--) id[c[d[i]]--] = i;
  long long ans = 0;
  for (int i = ct; i > 1; i--) {
    int p = id[i];
    val[fa[p]] += val[p];
    int L = d[p];
    if (pos[p] - L + 1 <= g[pos[p]]) L = pos[p] - g[pos[p]];
    if (L > d[fa[p]]) ans = max(ans, val[p] * L);
  }
  cout << ans;
}
void extend(int c, int x) {
  str[++len] = c + 'a';
  int p = last;
  int np = ++ct;
  d[np] = d[p] + 1, last = np;
  pos[np] = len;
  val[np] = x;
  while (p && !ch[p][c]) ch[p][c] = np, p = fa[p];
  if (!p)
    fa[np] = 1;
  else {
    int q = ch[p][c];
    if (d[q] == d[p] + 1)
      fa[np] = q;
    else {
      int nq = ++ct;
      memcpy(ch[nq], ch[q], 27 << 2);
      d[nq] = d[p] + 1;
      pos[nq] = pos[q];
      fa[nq] = fa[q];
      fa[np] = fa[q] = nq;
      while (p && ch[p][c] == q) ch[p][c] = nq, p = fa[p];
    }
  }
}