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;
typedef struct Node {
  int begin;
  int end;
  int depth;
  long long cost;
  Node *parent;
  map<int, Node *> children;
  Node *suffixLink;
} Node;
Node *init(int begin, int end, int depth, Node *parent) {
  Node *x = new Node;
  x->begin = begin;
  x->end = end;
  x->parent = parent;
  x->depth = depth;
  x->suffixLink = NULL;
  x->cost = 0;
  return x;
}
Node *buildSuffixTree(int *a, int n) {
  Node *root = init(0, 0, 0, NULL);
  Node *node = root;
  for (int i = 0, tail = 0; i < n; i++, tail++) {
    Node *last = NULL;
    while (tail >= 0) {
      Node *ch = node->children[a[i - tail]];
      while (ch != NULL && tail >= ch->end - ch->begin) {
        tail -= ch->end - ch->begin;
        node = ch;
        ch = ch->children[a[i - tail]];
      }
      if (ch == NULL) {
        node->children[a[i]] =
            init(i, n, node->depth + node->end - node->begin, node);
        if (last != NULL) last->suffixLink = node;
        last = NULL;
      } else {
        int t = a[ch->begin + tail];
        if (t == a[i]) {
          if (last != NULL) last->suffixLink = node;
          break;
        } else {
          Node *splitNode = init(ch->begin, ch->begin + tail,
                                 node->depth + node->end - node->begin, node);
          splitNode->children[a[i]] = init(i, n, ch->depth + tail, splitNode);
          splitNode->children[t] = ch;
          ch->begin += tail;
          ch->depth += tail;
          ch->parent = splitNode;
          node->children[a[i - tail]] = splitNode;
          if (last != NULL) last->suffixLink = splitNode;
          last = splitNode;
        }
      }
      if (node == root) {
        --tail;
      } else {
        node = node->suffixLink;
      }
    }
  }
  return root;
}
const long long mm = 5e5 + 4;
const long long ms = 1e5 + 4;
map<long long, long long> h;
int *s;
void dfs1(Node *x) {
  auto it = h.lower_bound(x->begin);
  long long idx = it->first;
  if (x->begin <= idx && idx < x->end) {
    x->end = idx;
    x->cost += it->second;
    x->children.clear();
  } else {
    for (auto &got : x->children) {
      auto y = got.second;
      if (y != NULL) {
        dfs1(y);
      }
    }
  }
}
long long final = 0;
long long dfs2(Node *x) {
  for (auto &got : x->children) {
    auto y = got.second;
    if (y != NULL) {
      x->cost += dfs2(y);
    }
  }
  long long len = x->depth + (x->end) - (x->begin);
  assert(len >= 0);
  if (x->begin != x->end) final = max(final, ((long long)x->cost) * len);
  return x->cost;
}
string t[ms];
long long c[ms];
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(0);
  long long n;
  cin >> n;
  for (long long i = 0; i < n; i++) cin >> t[i];
  for (long long i = 0; i < n; i++) cin >> c[i];
  long long slen = 0;
  for (long long i = 0; i < n; i++) {
    slen += t[i].length() + 1;
  }
  s = new int[slen];
  long long x = 0;
  for (long long i = 0; i < n; i++) {
    for (long long y = 0; y < t[i].length(); y++) {
      s[x] = t[i][y] - 'a';
      ++x;
    }
    s[x] = 26 + i;
    h[x] = c[i];
    ++x;
  }
  Node *root = buildSuffixTree(s, slen);
  dfs1(root);
  dfs2(root);
  printf("%lld\n", final);
}