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;
template <class T>
inline void gn(T &x) {
  char c;
  bool f = 0;
  while (!isdigit(c = getchar())) f = (c == '-');
  x = c - '0';
  while (isdigit(c = getchar())) x = x * 10 + c - '0';
  if (f) x = -x;
}
int n, a, b, l, cnt = 1, la;
int x[500005], y[500005];
int z[100005];
int fa[1000005], nex[1000005][26], len[1000005], v[1000005], u[1000005];
long long sg[1000005], ans;
char d;
inline void extend(int c) {
  int p = la, np = la = ++cnt;
  len[np] = len[p] + 1;
  for (; p && !nex[p][c]; p = fa[p]) nex[p][c] = np;
  if (!p)
    fa[np] = 1;
  else {
    int q = nex[p][c], nq;
    if (len[q] == len[p] + 1)
      fa[np] = q;
    else {
      len[nq = ++cnt] = len[p] + 1;
      fa[nq] = fa[q];
      fa[q] = fa[np] = nq;
      memcpy(nex[nq], nex[q], sizeof nex[q]);
      for (; nex[p][c] == q; p = fa[p]) nex[p][c] = nq;
    }
  }
}
inline bool p() {
  d = getchar();
  return d < 'a' || d > 'z';
}
inline void maxx(long long b) {
  if (ans < b) ans = b;
}
inline void solve() {
  for (int i = 1; i <= cnt; ++i) ++v[len[i]];
  for (int i = 1; i <= cnt; ++i) v[i] += v[i - 1];
  for (int i = 1; i <= cnt; ++i) u[v[len[i]]--] = i;
  for (int i = cnt; i >= 1; --i) sg[fa[u[i]]] += sg[u[i]];
  for (int i = cnt; i >= 1; --i) maxx(len[i] * sg[i]);
}
int main() {
  gn(n);
  for (int i = 1; i <= n; ++i) {
    while (p())
      ;
    y[l + 1] = 1;
    while (1) {
      x[++l] = a = d - 'a';
      if (y[l]) la = 1;
      extend(a);
      if (p()) break;
    }
  }
  for (int i = 1; i <= n; ++i) gn(z[i]);
  for (int i = 1; i <= l; ++i) {
    if (y[i]) a = 1, ++b;
    a = nex[a][x[i]], sg[a] += z[b];
  }
  solve();
  printf("%I64d\n", ans);
  return 0;
}