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 = 6e5;
struct suffixAutomata {
  int sz, last;
  long long occur[N + N + 2];
  vector<int> tree[N + N + 2];
  struct node {
    int len, link, next[26];
  } aut[N + N + 2];
  void clean(int n = N) {
    for (int i = 0; i <= n + n; i++) {
      memset(aut[i].next, 0, sizeof(aut[i].next));
    }
    sz = 0, last = 0, aut[0].link = -1;
  }
  int con(char c) { return c - 'a'; }
  void insrt(int c, int ad) {
    int now = ++sz;
    aut[now].len = aut[last].len + 1;
    occur[now] = ad;
    int p, q, cl;
    for (p = last; p != -1 && !aut[p].next[c]; p = aut[p].link)
      aut[p].next[c] = now;
    if (p == -1)
      aut[now].link = 0;
    else {
      q = aut[p].next[c];
      if (aut[p].len + 1 == aut[q].len)
        aut[now].link = q;
      else {
        cl = ++sz;
        aut[cl].len = aut[p].len + 1;
        aut[cl].link = aut[q].link;
        memcpy(aut[cl].next, aut[q].next, sizeof(aut[cl].next));
        for (; p != -1 && aut[p].next[c] == q; p = aut[p].link)
          aut[p].next[c] = cl;
        aut[now].link = aut[q].link = cl;
      }
    }
    last = now;
  }
  void insrt(string s, int ad) {
    for (auto x : s) insrt(con(x), ad);
  }
  void makeTree(void) {
    for (int i = 1; i <= sz; i++) tree[aut[i].link].push_back(i);
  }
  void dfs(int nd) {
    for (auto x : tree[nd]) {
      dfs(x);
      occur[nd] += occur[x];
    }
  }
  long long occurences(int n = N) {
    makeTree();
    dfs(0);
    long long mx = 0;
    for (int i = 1; i <= sz; i++) {
      if (aut[aut[i].link].len < aut[i].len)
        mx = max(mx, 1LL * aut[i].len * occur[i]);
    }
    return mx;
  }
} sam;
int cc[N + 2];
string s[N + 2];
int main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  int n;
  cin >> n;
  sam.clean();
  for (int i = 1; i <= n; i++) cin >> s[i];
  for (int i = 1; i <= n; i++) cin >> cc[i];
  for (int i = 1; i <= n; i++) {
    sam.insrt(s[i], cc[i]);
    sam.last = 0;
  }
  cout << sam.occurences() << endl;
  return 0;
}