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;
long long read() {
  long long a = 0, b = getchar(), c = 1;
  while (!isdigit(b)) c = b == '-' ? -1 : 1, b = getchar();
  while (isdigit(b)) a = a * 10 + b - '0', b = getchar();
  return a * c;
}
const int N = 100005;
int n, cnt, np, st[N], ed[N], mx[N * 10], ch[N * 10][26], fa[N * 10];
long long c[N * 10];
vector<int> to[N * 10];
char s[N * 5];
void gao(int x, int y);
int main() {
  n = read();
  for (int i = 1; i <= n; i++) {
    st[i] = ed[i - 1] + 1;
    scanf("%s", s + st[i]);
    int l = strlen(s + st[i]);
    ed[i] = ed[i - 1] + l;
  }
  cnt = 1;
  for (int i = 1; i <= n; i++) {
    int x = read();
    np = 1;
    for (int j = st[i]; j <= ed[i]; j++) gao(s[j] - 'a', x);
  }
  long long ans = 0;
  for (int i = 2; i <= cnt; i++) to[mx[i]].push_back(i);
  for (int i = ed[n]; i; i--)
    for (int j = 0; j < to[i].size(); j++) {
      c[fa[to[i][j]]] += c[to[i][j]];
      ans = max(ans, i * c[to[i][j]]);
    }
  printf("%lld", ans);
  return 0;
}
void gao(int x, int y) {
  if (ch[np][x] and mx[ch[np][x]] == mx[np] + 1)
    return (void)(np = ch[np][x], c[np] += y);
  int p = np;
  np = ++cnt, mx[np] = mx[p] + 1, c[np] = y;
  for (; p and !ch[p][x]; p = fa[p]) ch[p][x] = np;
  if (!p)
    fa[np] = 1;
  else {
    int q = ch[p][x];
    if (mx[q] == mx[p] + 1)
      fa[np] = q;
    else {
      int nq = ++cnt;
      mx[nq] = mx[p] + 1;
      memcpy(ch[nq], ch[q], sizeof(ch[q]));
      fa[nq] = fa[q];
      fa[q] = fa[np] = nq;
      for (; p and ch[p][x] == q; p = fa[p]) ch[p][x] = nq;
    }
  }
}