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 MaxC = 256;
const int MAX = 600001;
int offset, n;
int suma[MAX], tmp[MAX];
int rank1[MAX], rank2[MAX];
int c[MAX], p[MAX], lcp[MAX];
bool different(int a, int b) {
  if (b + offset >= n) return true;
  if (rank1[a] != rank1[b]) return true;
  if (rank2[a] != rank2[b]) return true;
  return false;
}
void suffix_array(char *text) {
  n = strlen(text);
  for (int i = 0; i < MaxC; i++) suma[i] = 0;
  for (int i = 0; i < n; i++) suma[text[i]]++;
  for (int i = 0; i < MaxC; i++) suma[i + 1] += suma[i];
  for (int i = 0; i < n; i++) c[i] = suma[text[i]] - 1;
  for (offset = 1; offset <= n; offset <<= 1) {
    for (int i = 0; i < n; i++) {
      rank1[i] = c[i];
      rank2[i] = (i + offset < n) ? c[i + offset] : 0;
    }
    fill(suma, suma + n, 0);
    for (int i = 0; i < n; i++) suma[rank2[i]]++;
    for (int i = 0; i < n; i++) suma[i + 1] += suma[i];
    for (int i = 0; i < n; i++) tmp[--suma[rank2[i]]] = i;
    fill(suma, suma + n, 0);
    for (int i = 0; i < n; i++) suma[rank1[i]]++;
    for (int i = 0; i < n; i++) suma[i + 1] += suma[i];
    for (int i = n - 1; i >= 0; i--) p[--suma[rank1[tmp[i]]]] = tmp[i];
    c[p[0]] = 0;
    for (int i = 1; i < n; i++) {
      c[p[i]] = c[p[i - 1]];
      if (different(p[i], p[i - 1])) c[p[i]]++;
    }
  }
  offset = 0;
  for (int i = 0; i < n; i++) {
    if (c[i] == n - 1) {
      lcp[n - 1] = 0;
      continue;
    }
    while (text[i + offset] != '{' &&
           text[i + offset] == text[p[c[i] + 1] + offset])
      offset++;
    lcp[c[i]] = offset;
    if (offset > 0) offset--;
  }
}
const int MAXN = 100000;
char s[MAX];
string w;
int len[MAXN];
long long cost[MAXN];
long long cc[MAX];
long long sc[MAX];
int l[MAX], r[MAX];
int st[MAX];
long long lennn[MAX];
int main() {
  int N;
  cin >> N;
  long long sol = 0ll;
  int pos = 0;
  for (int i = 0; i < N; i++) {
    cin >> w;
    len[i] = w.size();
    for (int j = 0; j < len[i]; j++) {
      lennn[pos] = len[i];
      s[pos++] = w[j];
    }
    s[pos++] = '{';
  }
  for (int i = 0; i < N; i++) {
    cin >> cost[i];
  }
  if (N == 1) {
    sol = (long long)len[0] * (long long)cost[0];
  }
  suffix_array(s);
  pos = 0;
  for (int i = 0; i < N; i++) {
    for (int j = 0; j < len[i]; j++) {
      cc[pos + j] = cost[i];
    }
    cc[pos + len[i]] = 0;
    pos += len[i] + 1;
  }
  int sz = n - N;
  for (int i = 0; i < sz; i++) {
    sc[i] = cc[p[i]];
    if (i > 0) {
      sc[i] += sc[i - 1];
    }
  }
  int sti = 0;
  for (int i = 0; i < sz - 1; i++) {
    l[i] = i;
    while (sti > 0 && lcp[st[sti - 1]] >= lcp[i]) {
      l[i] = l[st[sti - 1]];
      sti--;
    }
    st[sti++] = i;
  }
  sti = 0;
  for (int i = sz - 2; i >= 0; i--) {
    r[i] = i;
    while (sti > 0 && lcp[st[sti - 1]] >= lcp[i]) {
      r[i] = r[st[sti - 1]];
      sti--;
    }
    st[sti++] = i;
  }
  for (int i = 0; i < sz - 1; i++) {
    long long val = sc[r[i] + 1];
    if (l[i] > 0) {
      val -= sc[l[i] - 1];
    }
    val *= (long long)lcp[i];
    sol = max(sol, val);
    int auxx = lcp[i];
    if (i > 0) {
      auxx = max(auxx, lcp[i - 1]);
    }
    if (s[p[i] + auxx] != '{') {
      sol = max(sol, lennn[p[i]] * cc[p[i]]);
    }
  }
  int i = sz - 1;
  lcp[i] = 0;
  int auxx = lcp[i];
  if (i > 0) {
    auxx = max(auxx, lcp[i - 1]);
  }
  if (s[p[i] + auxx] != '{') {
    sol = max(sol, lennn[p[i]] * cc[p[i]]);
  }
  cout << sol << '\n';
}