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 N = 660000;
int wa[N], wb[N], wc[N], wd[N], sa[N], rnk[N], lcp[N], dp[N][22], pw2[26];
int len;
bool is_equal(int *r, int a, int b, int l, int n) {
  int la = r[a], lb = r[b], ra, rb;
  ra = a + l < n ? r[a + l] : -1;
  rb = b + l < n ? r[b + l] : -1;
  return (la == lb) && (ra == rb);
}
void build_suffix_array(string r) {
  int n = len, m = 256;
  int *x = wa, *y = wb;
  int i, tot, p;
  memset(wc, 0, sizeof(int) * m);
  for (i = 0; i < n; i++) wc[x[i] = r[i]]++;
  for (i = 1; i < m; i++) wc[i] += wc[i - 1];
  for (i = n - 1; i >= 0; i--) sa[--wc[x[i]]] = i;
  for (tot = 0, p = 1; tot + 1 < n; p <<= 1, m = tot + 1) {
    for (tot = 0, i = n - p; i < n; i++) y[tot++] = i;
    for (i = 0; i < n; i++)
      if (sa[i] >= p) y[tot++] = sa[i] - p;
    for (i = 0; i < n; i++) wd[i] = x[y[i]];
    memset(wc, 0, sizeof(int) * m);
    for (i = 0; i < n; i++) wc[wd[i]]++;
    for (i = 1; i < m; i++) wc[i] += wc[i - 1];
    for (i = n - 1; i >= 0; i--) sa[--wc[wd[i]]] = y[i];
    swap(x, y);
    x[sa[0]] = tot = 0;
    for (i = 1; i < n; i++)
      x[sa[i]] = is_equal(y, sa[i - 1], sa[i], p, n) ? tot : ++tot;
  }
}
void build_lcp(string r) {
  int n = len;
  int i;
  for (i = 0; i < n; i++) rnk[sa[i]] = i;
  for (lcp[0] = i = 0; i < n; i++)
    if (rnk[i]) {
      lcp[rnk[i]] = (i == 0) ? 0 : max(0, lcp[rnk[i - 1]] - 1);
      for (; r[i + lcp[rnk[i]]] == r[sa[rnk[i] - 1] + lcp[rnk[i]]];
           lcp[rnk[i]]++)
        ;
    }
  for (int i = 1; i < n; i++) dp[i][0] = lcp[i];
  pw2[0] = 1;
  for (int j = 1; j < 22; j++) {
    pw2[j] = pw2[j - 1] * 2;
    for (int i = 1; i + pw2[j] - 1 < n; i++)
      dp[i][j] = min(dp[i][j - 1], dp[i + pw2[j - 1]][j - 1]);
  }
}
int get_lcp(int i, int j) {
  int n = len;
  if (i >= n || j >= n) return 0;
  i = rnk[i], j = rnk[j];
  if (i == j) return n - sa[i];
  if (i > j) swap(i, j);
  i++;
  int p = 31 - __builtin_clz(j - i + 1);
  return min(dp[i][p], dp[j - pw2[p] + 1][p]);
}
string s[N], t;
const long long INF = 2e18;
long long dp2[N];
int c[N], rem[N], sum[N], p[N];
vector<int> vec[N], era[N];
set<int> S;
multiset<long long> V;
bool vis[N];
int main() {
  int n;
  scanf("%d", &n);
  for (int i = 1; i <= n; i++) {
    cin >> s[i];
    s[i] += "$";
    t += s[i];
    sum[i] = sum[i - 1] + s[i].length();
  }
  len = t.length();
  for (int i = 1; i <= n; i++) scanf("%d", c + i);
  for (int i = 1, j = 0; j < len; j++) {
    if (j == sum[i]) i++;
    p[j] = i;
    rem[j] = sum[i] - 1 - j;
  }
  build_suffix_array(t);
  build_lcp(t);
  int hd = 0;
  for (; t[sa[hd]] == '$'; hd++)
    ;
  for (int i = 0; i < len; i++) {
    dp2[i] = dp2[max(i - 1, 0)] + c[p[sa[i]]];
  }
  for (int i = hd; i < len; i++) {
    vec[min(rem[sa[i]], lcp[i])].push_back(i);
    era[rem[sa[i]]].push_back(i);
  }
  long long max_sum = -INF, ans = 0;
  S.insert(len);
  set<int>::iterator it;
  for (int i = 0; i <= len; i++) {
    if (!V.empty()) ans = max(ans, *(V.rbegin()) * i);
    for (int x : vec[i]) {
      it = S.lower_bound(x);
      if (it == S.begin()) {
        int big = *it;
        S.insert(x);
        V.insert(dp2[big - 1] - dp2[x - 1]);
        continue;
      }
      int big = *it, small = *(--it);
      S.insert(x);
      if (!vis[small]) V.erase(V.lower_bound(dp2[big - 1] - dp2[small - 1]));
      V.insert(dp2[big - 1] - dp2[x - 1]);
      V.insert(dp2[x - 1] - dp2[small - 1]);
    }
    for (int x : era[i]) {
      vis[x] = true;
      it = S.lower_bound(x);
      int big = *(++it);
      V.erase(V.lower_bound(dp2[big - 1] - dp2[x - 1]));
    }
  }
  printf("%lld\n", ans);
  return 0;
}