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 sz = 100010 * 5;
char s[sz];
int a[sz / 5], b[sz];
int tot = 0, first = 1, last;
int fa[sz * 2], son[sz * 2][26], step[sz * 2];
long long appear[sz * 2];
int Link[sz];
int Q[sz * 2], op, ed, du[sz * 2];
void add(int num, int c) {
  int p = last;
  if (son[p][c]) {
    int q = son[p][c];
    if (step[p] + 1 == step[q])
      last = Link[num] = q;
    else {
      int nq = ++tot;
      step[nq] = step[p] + 1;
      for (int i = 0; i < 26; i++) son[nq][i] = son[q][i];
      fa[nq] = fa[q];
      fa[q] = nq;
      for (; p && son[p][c] == q; p = fa[p]) son[p][c] = nq;
      last = Link[num] = nq;
    }
  } else {
    int np = ++tot;
    step[np] = step[p] + 1;
    last = Link[num] = np;
    for (; p && son[p][c] == 0; p = fa[p]) son[p][c] = np;
    if (p == 0)
      fa[np] = first;
    else if (step[p] + 1 == step[son[p][c]])
      fa[np] = son[p][c];
    else {
      int q = son[p][c], nq = ++tot;
      step[nq] = step[p] + 1;
      for (int i = 0; i < 26; i++) son[nq][i] = son[q][i];
      fa[nq] = fa[q];
      fa[q] = fa[np] = nq;
      for (; p && son[p][c] == q; p = fa[p]) son[p][c] = nq;
    }
  }
}
int main() {
  int n;
  while (~scanf("%d", &n)) {
    int m, i, L = 0, u, v, j, len;
    tot = 1;
    first = 1;
    for (i = 1; i <= n; i++) {
      scanf("%s", s + L + 1);
      len = strlen(s + L + 1);
      last = 1;
      for (j = L + 1; j <= L + len; j++) {
        add(j, s[j] - 'a');
        b[j] = i;
      }
      L += len;
    }
    for (i = 1; i <= n; i++) scanf("%d", &a[i]);
    op = 0;
    ed = 0;
    for (i = 1; i <= L; i++) appear[Link[i]] += (long long)a[b[i]];
    for (i = 1; i <= tot; i++)
      if (fa[i]) du[fa[i]]++;
    for (i = 1; i <= tot; i++)
      if (du[i] == 0) Q[++ed] = i;
    while (op < ed) {
      u = Q[++op];
      if (fa[u] == 0) continue;
      appear[fa[u]] += appear[u];
      if (--du[fa[u]] == 0) Q[++ed] = fa[u];
    }
    long long ans = 0;
    for (int i = 2; i <= tot; i++)
      if (ans < step[i] * appear[i]) ans = step[i] * appear[i];
    cout << ans << endl;
  }
  return 0;
}