#include <bits/stdc++.h>
using namespace std;
long long read() {
  long long a = 0, b = getchar(), c = 1;
  while (!isdigit(b)) c = b == '-' ? -1 : 1, b = getchar();
  while (isdigit(b)) a = a * 10 + b - '0', b = getchar();
  return a * c;
}
const int N = 100005;
int n, cnt, np, st[N], ed[N], mx[N * 10], ch[N * 10][26], fa[N * 10];
long long c[N * 10];
vector<int> to[N * 10];
char s[N * 5];
void gao(int x, int y);
int main() {
  n = read();
  for (int i = 1; i <= n; i++) {
    st[i] = ed[i - 1] + 1;
    scanf("%s", s + st[i]);
    int l = strlen(s + st[i]);
    ed[i] = ed[i - 1] + l;
  }
  cnt = 1;
  for (int i = 1; i <= n; i++) {
    int x = read();
    np = 1;
    for (int j = st[i]; j <= ed[i]; j++) gao(s[j] - 'a', x);
  }
  long long ans = 0;
  for (int i = 2; i <= cnt; i++) to[mx[i]].push_back(i);
  for (int i = ed[n]; i; i--)
    for (int j = 0; j < to[i].size(); j++) {
      c[fa[to[i][j]]] += c[to[i][j]];
      ans = max(ans, i * c[to[i][j]]);
    }
  printf("%lld", ans);
  return 0;
}
void gao(int x, int y) {
  if (ch[np][x] and mx[ch[np][x]] == mx[np] + 1)
    return (void)(np = ch[np][x], c[np] += y);
  int p = np;
  np = ++cnt, mx[np] = mx[p] + 1, c[np] = y;
  for (; p and !ch[p][x]; p = fa[p]) ch[p][x] = np;
  if (!p)
    fa[np] = 1;
  else {
    int q = ch[p][x];
    if (mx[q] == mx[p] + 1)
      fa[np] = q;
    else {
      int nq = ++cnt;
      mx[nq] = mx[p] + 1;
      memcpy(ch[nq], ch[q], sizeof(ch[q]));
      fa[nq] = fa[q];
      fa[q] = fa[np] = nq;
      for (; p and ch[p][x] == q; p = fa[p]) ch[p][x] = nq;
    }
  }
}
