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 MAXLEN = 500000;
struct SA {
  struct state {
    int len, link = 0;
    long long sum = 0;
    map<char, int> next;
  };
  state st[MAXLEN * 2];
  int sz, last;
  void sa_init() {
    sz = last = 0;
    st[0].len = 0;
    st[0].sum = 0;
    st[0].link = -1;
    sz++;
  }
  void sa_extend(char c) {
    int cur = sz++;
    st[cur].len = st[last].len + 1;
    int p = last;
    while (p != -1 && !st[p].next.count(c)) {
      st[p].next[c] = cur;
      p = st[p].link;
    }
    if (p == -1) {
      st[cur].link = 0;
    } else {
      int q = st[p].next[c];
      if (st[p].len + 1 == st[q].len) {
        st[cur].link = q;
      } else {
        int clone = sz++;
        st[clone].len = st[p].len + 1;
        st[clone].next = st[q].next;
        st[clone].link = st[q].link;
        while (p != -1 && st[p].next[c] == q) {
          st[p].next[c] = clone;
          p = st[p].link;
        }
        st[q].link = st[cur].link = clone;
      }
    }
    last = cur;
  }
} sa;
string s[100005];
int c[100005];
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(0);
  sa.sa_init();
  int n;
  cin >> n;
  for (int i = 0; i < n; i++) cin >> s[i];
  for (int i = 0; i < n; i++) cin >> c[i];
  for (int i = 0; i < n; i++) {
    int m = s[i].length();
    sa.last = 0;
    for (int j = 0; j < m; j++) sa.sa_extend(s[i][j]);
  }
  for (int i = 0; i < n; i++) {
    int cur = 0;
    int l = s[i].length();
    for (int j = 0; j < l; j++) {
      cur = sa.st[cur].next[s[i][j]];
      sa.st[cur].sum += c[i];
    }
  }
  vector<pair<int, int> > v;
  for (int i = 0; i < sa.sz; i++) {
    v.emplace_back(make_pair(sa.st[i].len, i));
  }
  sort((v).begin(), (v).end());
  reverse((v).begin(), (v).end());
  for (int i = 0; i < (int)(v).size(); i++) {
    int j = v[i].second;
    if (j == 0) continue;
    sa.st[sa.st[j].link].sum += sa.st[j].sum;
  }
  long long ans = 0;
  for (int i = 0; i < sa.sz; i++)
    ans = max(ans, sa.st[i].sum * 1ll * sa.st[i].len);
  printf("%lld\n", ans);
}