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>
inline long long gcd(long long a, long long b) { return b ? gcd(b, a % b) : a; }
inline void sub(long long &a, long long b) {
  a -= b;
  if (a < 0) a += 1000000009;
}
inline void add(long long &a, long long b) {
  a += b;
  if (a >= 1000000009) a -= 1000000009;
}
template <typename T>
inline T const &MAX(T const &a, T const &b) {
  return a > b ? a : b;
}
template <typename T>
inline T const &MIN(T const &a, T const &b) {
  return a < b ? a : b;
}
inline long long qp(long long a, long long b) {
  long long ans = 1;
  while (b) {
    if (b & 1) ans = ans * a % 1000000009;
    a = a * a % 1000000009, b >>= 1;
  }
  return ans;
}
inline long long qp(long long a, long long b, long long c) {
  long long ans = 1;
  while (b) {
    if (b & 1) ans = ans * a % c;
    a = a * a % c, b >>= 1;
  }
  return ans;
}
using namespace std;
const unsigned long long ba = 233;
const double eps = 1e-5;
const long long INF = 0x3f3f3f3f3f3f3f3f;
const int N = 600000 + 10, maxn = 1000000 + 10, inf = 0x3f3f3f3f;
char s[N];
vector<char> v[100010];
struct SAM {
  int last, cnt;
  int ch[N << 1][26], fa[N << 1], l[N << 1];
  int a[N << 1], c[N << 1];
  long long sz[N << 1];
  SAM() { cnt = 1; }
  void ins(int c, int x) {
    if (ch[last][c]) {
      int p = last, q = ch[last][c];
      if (l[q] == l[p] + 1)
        last = q;
      else {
        int nq = ++cnt;
        l[nq] = l[p] + 1;
        memcpy(ch[nq], ch[q], sizeof ch[q]);
        fa[nq] = fa[q];
        fa[q] = last = nq;
        for (; ch[p][c] == q; p = fa[p]) ch[p][c] = nq;
      }
      sz[last] += x;
      return;
    }
    int p = last, np = ++cnt;
    last = np;
    l[np] = l[p] + 1;
    for (; p && !ch[p][c]; p = fa[p]) ch[p][c] = np;
    if (!p)
      fa[np] = 1;
    else {
      int q = ch[p][c];
      if (l[p] + 1 == l[q])
        fa[np] = q;
      else {
        int nq = ++cnt;
        l[nq] = l[p] + 1;
        memcpy(ch[nq], ch[q], sizeof(ch[q]));
        fa[nq] = fa[q];
        fa[q] = fa[np] = nq;
        for (; ch[p][c] == q; p = fa[p]) ch[p][c] = nq;
      }
    }
    sz[np] += x;
  }
  void build(int id, int x) {
    last = 1;
    for (int i = 0; i < v[id].size(); i++) ins(v[id][i] - 'a', x);
  }
  void topo() {
    for (int i = 1; i <= cnt; i++) c[l[i]]++;
    for (int i = 1; i <= cnt; i++) c[i] += c[i - 1];
    for (int i = 1; i <= cnt; i++) a[c[l[i]]--] = i;
  }
  void cal() {
    topo();
    for (int i = cnt; i; i--) sz[fa[a[i]]] += sz[a[i]];
    long long ans = 0;
    for (int i = 2; i <= cnt; i++) ans = max(ans, sz[i] * l[i]);
    printf("%lld\n", ans);
  }
} sam;
int main() {
  int n;
  scanf("%d", &n);
  for (int i = 1; i <= n; i++) {
    scanf("%s", s);
    for (int j = 0; s[j]; j++) v[i].push_back(s[j]);
  }
  for (int i = 1; i <= n; i++) {
    int x;
    scanf("%d", &x);
    sam.build(i, x);
  }
  sam.cal();
  return 0;
}