#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);
}
