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 maxn = 1000 * 1000;
int sa[maxn], pos[maxn], tmp[maxn], siz[maxn], nexT[maxn], last[maxn],
    tpos[maxn], gap, n;
long long int lcp[maxn], sum[maxn], cost[maxn], ans;
stack<int> stc;
vector<int> s;
bool sufCmp(int i, int j) {
  if (pos[i] != pos[j]) return pos[i] < pos[j];
  i += gap, j += gap;
  return (i < ((int)(s).size()) and j < ((int)(s).size())) ? pos[i] < pos[j]
                                                           : i > j;
}
void build_partisum(void) {
  int ptr = 0;
  for (int i = 0; i < n; i++) {
    tpos[i] = pos[ptr];
    for (int j = 0; j < siz[i]; j++, ptr++) sum[pos[ptr]] = cost[i];
    ptr++;
  }
  for (int i = 1; i <= ((int)(s).size()); i++) sum[i] += sum[i - 1];
  return;
}
long long int get(int st, int en) {
  return (st) ? sum[en] - sum[st - 1] : sum[en];
}
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(0);
  cin >> n;
  for (int i = 0; i < n; i++) {
    string t;
    cin >> t;
    siz[i] = ((int)(t).size());
    for (int j = 0; j < ((int)(t).size()); j++) s.push_back(t[j]);
    s.push_back(500 + i);
  }
  for (int i = 0; i < n; i++) cin >> cost[i];
  for (int i = 0; i < ((int)(s).size()); i++) sa[i] = i, pos[i] = s[i];
  for (gap = 1;; gap <<= 1) {
    sort(sa, sa + ((int)(s).size()), sufCmp);
    for (int i = 0; i + 1 < ((int)(s).size()); i++)
      tmp[i + 1] = tmp[i] + sufCmp(sa[i], sa[i + 1]);
    for (int i = 0; i < ((int)(s).size()); i++) pos[sa[i]] = tmp[i];
    if (tmp[((int)(s).size()) - 1] == ((int)(s).size()) - 1) break;
  }
  build_partisum();
  int match = 0;
  for (int i = 0; i < ((int)(s).size()); i++) {
    if (match) match--;
    if (!pos[i]) continue;
    int cur = i, pre = sa[pos[i] - 1];
    while (s[pre + match] == s[cur + match]) match++;
    lcp[pos[i]] = match;
  }
  for (int i = 0; i < ((int)(s).size()); i++) {
    while (((int)(stc).size()) and lcp[stc.top()] >= lcp[i]) stc.pop();
    last[i] = (((int)(stc).size()) ? stc.top() : 0);
    stc.push(i);
  }
  while (((int)(stc).size())) stc.pop();
  for (int i = ((int)(s).size()) - 1; i >= 0; i--) {
    while (((int)(stc).size()) and lcp[stc.top()] >= lcp[i]) stc.pop();
    nexT[i] = (((int)(stc).size()) ? stc.top() : ((int)(s).size()));
    stc.push(i);
  }
  for (int i = 1; i < ((int)(s).size()); i++)
    if (lcp[i]) {
      long long int val = get(last[i], nexT[i] - 1);
      ((ans) = max((ans), (lcp[i] * val)));
    }
  for (int i = 0; i < n; i++) {
    long long int match = 0;
    ((match) = max((match), (lcp[tpos[i]])));
    ((match) = max((match), (lcp[tpos[i] + 1])));
    if (match < siz[i]) ((ans) = max((ans), (siz[i] * cost[i])));
  }
  cout << ans << endl;
  return 0;
}