#include <bits/stdc++.h>
using namespace std;
const int sz = 100010 * 5;
char s[sz];
int a[sz / 5], b[sz];
int tot = 0, first = 1, last;
int fa[sz * 2], son[sz * 2][26], step[sz * 2];
long long appear[sz * 2];
int Link[sz];
int Q[sz * 2], op, ed, du[sz * 2];
void add(int num, int c) {
  int p = last;
  if (son[p][c]) {
    int q = son[p][c];
    if (step[p] + 1 == step[q])
      last = Link[num] = q;
    else {
      int nq = ++tot;
      step[nq] = step[p] + 1;
      for (int i = 0; i < 26; i++) son[nq][i] = son[q][i];
      fa[nq] = fa[q];
      fa[q] = nq;
      for (; p && son[p][c] == q; p = fa[p]) son[p][c] = nq;
      last = Link[num] = nq;
    }
  } else {
    int np = ++tot;
    step[np] = step[p] + 1;
    last = Link[num] = np;
    for (; p && son[p][c] == 0; p = fa[p]) son[p][c] = np;
    if (p == 0)
      fa[np] = first;
    else if (step[p] + 1 == step[son[p][c]])
      fa[np] = son[p][c];
    else {
      int q = son[p][c], nq = ++tot;
      step[nq] = step[p] + 1;
      for (int i = 0; i < 26; i++) son[nq][i] = son[q][i];
      fa[nq] = fa[q];
      fa[q] = fa[np] = nq;
      for (; p && son[p][c] == q; p = fa[p]) son[p][c] = nq;
    }
  }
}
int main() {
  int n;
  while (~scanf("%d", &n)) {
    int m, i, L = 0, u, v, j, len;
    tot = 1;
    first = 1;
    for (i = 1; i <= n; i++) {
      scanf("%s", s + L + 1);
      len = strlen(s + L + 1);
      last = 1;
      for (j = L + 1; j <= L + len; j++) {
        add(j, s[j] - 'a');
        b[j] = i;
      }
      L += len;
    }
    for (i = 1; i <= n; i++) scanf("%d", &a[i]);
    op = 0;
    ed = 0;
    for (i = 1; i <= L; i++) appear[Link[i]] += (long long)a[b[i]];
    for (i = 1; i <= tot; i++)
      if (fa[i]) du[fa[i]]++;
    for (i = 1; i <= tot; i++)
      if (du[i] == 0) Q[++ed] = i;
    while (op < ed) {
      u = Q[++op];
      if (fa[u] == 0) continue;
      appear[fa[u]] += appear[u];
      if (--du[fa[u]] == 0) Q[++ed] = fa[u];
    }
    long long ans = 0;
    for (int i = 2; i <= tot; i++)
      if (ans < step[i] * appear[i]) ans = step[i] * appear[i];
    cout << ans << endl;
  }
  return 0;
}
