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 long long MOD = 1e9 + 7;
const double eps = 1e-10;
const int INF = 0x3f3f3f3f;
const long long INFLL = 0x3f3f3f3f3f3f3f3fll;
const int MAXN = 2e5 + 5;
const int maxn = 2e6 + 100;
struct SAM {
  int ch[maxn][26], pre[maxn], len[maxn], tag[maxn], in[maxn], endnum[maxn];
  long long val[maxn];
  int last, tot;
  void init() {
    last = tot = 0;
    memset(ch[0], -1, sizeof ch[0]);
    pre[0] = -1;
    len[0] = in[0] = 0;
  }
  void reset() { last = 0; }
  int extend(int c, int ind, int v) {
    int p = last, np = ++tot;
    len[np] = len[p] + 1;
    tag[np] = ind;
    val[np] += v;
    memset(ch[np], -1, sizeof ch[np]);
    in[np] = 0;
    while (~p && ch[p][c] == -1) ch[p][c] = np, p = pre[p];
    if (p == -1) {
      pre[np] = 0;
      in[0]++;
    } else {
      int q = ch[p][c];
      if (len[q] != len[p] + 1) {
        int nq = ++tot;
        memcpy(ch[nq], ch[q], sizeof ch[q]);
        in[nq] = tag[nq] = 0;
        len[nq] = len[p] + 1;
        pre[nq] = pre[q];
        pre[q] = pre[np] = nq;
        in[nq] += 2;
        while (~p && ch[p][c] == q) ch[p][c] = nq, p = pre[p];
      } else {
        pre[np] = q;
        in[q]++;
      }
    }
    last = np;
    return len[np] - len[pre[np]];
  }
  int who[maxn], a[maxn];
  void Sort() {
    for (int i = 1; i <= tot; i++) a[i] = 0;
    for (int i = 1; i <= tot; i++) a[len[i]]++;
    for (int i = 1; i <= tot; i++) a[i] += a[i - 1];
    for (int i = 1; i <= tot; i++) who[a[len[i]]--] = i;
  }
  void top_sort() {
    queue<int> q;
    for (int i = 1; i <= tot; i++) {
      if (!in[i]) q.push(i);
      endnum[i] = 0;
    }
    while (!q.empty()) {
      int u = q.front();
      q.pop();
      if (tag[u]) endnum[u]++;
      if (u) endnum[pre[u]] += endnum[u], val[pre[u]] += val[u];
      if (u)
        if (!--in[pre[u]]) q.push(pre[u]);
    }
  }
  long long solve() {
    top_sort();
    long long ans = 0;
    for (int i = 1; i <= tot; i++) {
      if (len[i] == len[pre[i]]) continue;
      if (val[i] < 0)
        ans = max(ans, val[i] * (len[pre[i]] + 1));
      else
        ans = max(ans, val[i] * len[i]);
    }
    return ans;
  }
} sam;
char s[1000005];
char* p[100005];
int a[100005];
int main() {
  int n;
  cin >> n;
  int pos = 0;
  sam.init();
  for (int i = 1; i <= n; i++) {
    scanf("%s", s + pos);
    p[i] = s + pos;
    pos += strlen(s + pos) + 1;
  }
  for (int i = 1; i <= n; i++) scanf("%d", &a[i]);
  int tot = 0;
  for (int i = 1; i <= n; i++) {
    char* c = p[i];
    while (*c) {
      sam.extend((*c) - 'a', ++tot, a[i]);
      c++;
    }
    sam.reset();
  }
  printf("%lld\n", sam.solve());
  return 0;
}