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 = 5e5 + 9;
long long a[N * 2];
vector<int> g[N * 2];
struct SuffixAutomaton {
  struct node {
    int len, link, firstpos;
    map<char, int> nxt;
  };
  int sz, last;
  vector<node> t;
  vector<int> terminal;
  vector<long long> dp;
  SuffixAutomaton() {}
  SuffixAutomaton(int n) {
    t.resize(2 * n);
    terminal.resize(2 * n);
    dp.resize(2 * n, -1);
    sz = 1;
    last = 0;
    t[0].len = 0;
    t[0].link = -1;
    t[0].firstpos = 0;
  }
  void extend(char c) {
    if (t[last].nxt.count(c) && t[t[last].nxt[c]].len == t[last].len + 1) {
      last = t[last].nxt[c];
      return;
    }
    int cur = sz++;
    t[cur].len = t[last].len + 1;
    t[cur].firstpos = t[cur].len;
    int p = last;
    while (p != -1 && !t[p].nxt.count(c)) {
      t[p].nxt[c] = cur;
      p = t[p].link;
    }
    if (p == -1)
      t[cur].link = 0;
    else {
      int q = t[p].nxt[c];
      if (t[p].len + 1 == t[q].len)
        t[cur].link = q;
      else {
        int clone = sz++;
        t[clone].len = t[p].len + 1;
        t[clone].nxt = t[q].nxt;
        t[clone].link = t[q].link;
        t[clone].firstpos = t[q].firstpos;
        while (p != -1 && t[p].nxt[c] == q) {
          t[p].nxt[c] = clone;
          p = t[p].link;
        }
        t[q].link = t[cur].link = clone;
      }
    }
    last = cur;
  }
  void dfs(int u) {
    for (auto v : g[u]) {
      dfs(v);
      a[u] += a[v];
    }
  }
};
string s[N];
int b[N];
int32_t main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  int n;
  cin >> n;
  for (int i = 0; i < n; i++) cin >> s[i];
  for (int i = 0; i < n; i++) cin >> b[i];
  SuffixAutomaton sa(N);
  for (int i = 0; i < n; i++) {
    for (auto c : s[i]) {
      sa.extend(c);
      a[sa.last] += b[i];
    }
    sa.last = 0;
  }
  for (int i = 1; i < sa.sz; i++) g[sa.t[i].link].push_back(i);
  sa.dfs(0);
  long long ans = 0;
  for (int i = 1; i < sa.sz; i++) ans = max(ans, a[i] * sa.t[i].len);
  cout << ans << '\n';
  return 0;
}