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 long double eps = 1e-16;
const int MAXLEN = 100005;
const int MODP = 1000000007;
template <typename T>
vector<T> readVector0(int n) {
  vector<T> res(n);
  for (int i = 0; i < n; i++) cin >> res[i];
  return res;
}
template <typename T>
vector<T> readVector1(int n) {
  vector<T> res(n + 1);
  for (int i = 1; i <= n; i++) cin >> res[i];
  return res;
}
vector<int> sss = {1, 3};
vector<int> ss;
vector<int> term;
vector<long long> cost;
long long max_cost;
vector<int> depth;
int n;
template <typename T>
class SuffixTree {
 public:
  vector<T> &s = sss;
  vector<int> pos;
  vector<int> len;
  vector<int> par;
  vector<map<T, int> > to;
  vector<map<T, int> > link;
  int sz = 2;
  SuffixTree(vector<T> &ss, const int maxlen = MAXLEN,
             const int alphabet_sz = 256) {
    s = ss;
    pos.resize(maxlen, 0);
    len.resize(maxlen, 0);
    par.resize(maxlen, 0);
    to.resize(maxlen);
    link.resize(maxlen);
    len[1] = 1;
    pos[1] = 0;
    par[1] = 0;
    for (int c = -n; c < alphabet_sz; c++) link[0][c] = 1;
  }
  void attach(int child, int parent, T c, int child_len) {
    to[parent][c] = child;
    len[child] = child_len;
    par[child] = parent;
  }
  void extend(int i) {
    int v;
    int vlen = s.size() - i;
    int old = sz - 1;
    stack<int> path;
    for (v = old; !link[v].count(s[i]); v = par[v]) {
      vlen -= len[v];
      path.push(v);
    }
    int w = link[v][s[i]];
    if (to[w].count(s[i + vlen])) {
      int u = to[w][s[i + vlen]];
      for (pos[sz] = pos[u] - len[u]; s[pos[sz]] == s[i + vlen];
           pos[sz] += len[v]) {
        v = path.top();
        path.pop();
        vlen += len[v];
      }
      attach(sz, w, s[pos[u] - len[u]], len[u] - (pos[u] - pos[sz]));
      attach(u, sz, s[pos[sz]], pos[u] - pos[sz]);
      w = link[v][s[i]] = sz;
      sz++;
    }
    link[old][s[i]] = sz;
    attach(sz, w, s[i + vlen], s.size() - (i + vlen));
    pos[sz] = s.size();
    if (s[i] < 0)
      term[sz] = i;
    else
      term[sz] = term[old];
    sz++;
  }
  long long dfs(int v) {
    long long sum_c = 0;
    depth[v] = depth[par[v]] + len[v];
    if (to[v].empty()) {
      if (s[pos[v] - len[v]] >= 0) {
        int l = depth[par[v]] + term[v] - pos[v] + len[v];
        if (max_cost < l * cost[pos[v] - len[v]])
          max_cost = l * cost[pos[v] - len[v]];
      }
      return cost[pos[v] - len[v]];
    } else
      for (auto u : to[v]) {
        sum_c += dfs(u.second);
      }
    if (sum_c * depth[v] > max_cost) max_cost = sum_c * depth[v];
    return sum_c;
  }
};
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  cin >> n;
  string t;
  int c_$ = -1;
  for (int i = 0; i < n; ++i) {
    cin >> t;
    vector<char> snew(t.begin(), t.end());
    ss.insert(ss.end(), t.begin(), t.end());
    ss.push_back(c_$--);
  }
  int cc;
  int cnt = 0;
  cost.resize(ss.size(), 0);
  depth.resize(2 * ss.size(), 0);
  term.resize(2 * ss.size(), -1);
  max_cost = 0;
  for (int i = 0; i < n; ++i) {
    cin >> cc;
    int cnt2 = 0;
    while (ss[cnt] >= 0) {
      cost[cnt++] = cc;
    }
    cost[cnt++] = cc;
  }
  SuffixTree<int> st(ss, 2 * ss.size(), 256);
  for (int i = ss.size() - 1; i >= 0; i--) st.extend(i);
  st.len[1] = 0;
  st.dfs(1);
  cout << max_cost;
  return 0;
}