#include <bits/stdc++.h>
using namespace std;
const long long MOD = 1e9 + 7;
const double eps = 1e-10;
const int INF = 0x3f3f3f3f;
const long long INFLL = 0x3f3f3f3f3f3f3f3fll;
const int MAXN = 2e5 + 5;
const int maxn = 2e6 + 100;
struct SAM {
  int ch[maxn][26], pre[maxn], len[maxn], tag[maxn], in[maxn], endnum[maxn];
  long long val[maxn];
  int last, tot;
  void init() {
    last = tot = 0;
    memset(ch[0], -1, sizeof ch[0]);
    pre[0] = -1;
    len[0] = in[0] = 0;
  }
  void reset() { last = 0; }
  int extend(int c, int ind, int v) {
    int p = last, np = ++tot;
    len[np] = len[p] + 1;
    tag[np] = ind;
    val[np] += v;
    memset(ch[np], -1, sizeof ch[np]);
    in[np] = 0;
    while (~p && ch[p][c] == -1) ch[p][c] = np, p = pre[p];
    if (p == -1) {
      pre[np] = 0;
      in[0]++;
    } else {
      int q = ch[p][c];
      if (len[q] != len[p] + 1) {
        int nq = ++tot;
        memcpy(ch[nq], ch[q], sizeof ch[q]);
        in[nq] = tag[nq] = 0;
        len[nq] = len[p] + 1;
        pre[nq] = pre[q];
        pre[q] = pre[np] = nq;
        in[nq] += 2;
        while (~p && ch[p][c] == q) ch[p][c] = nq, p = pre[p];
      } else {
        pre[np] = q;
        in[q]++;
      }
    }
    last = np;
    return len[np] - len[pre[np]];
  }
  int who[maxn], a[maxn];
  void Sort() {
    for (int i = 1; i <= tot; i++) a[i] = 0;
    for (int i = 1; i <= tot; i++) a[len[i]]++;
    for (int i = 1; i <= tot; i++) a[i] += a[i - 1];
    for (int i = 1; i <= tot; i++) who[a[len[i]]--] = i;
  }
  void top_sort() {
    queue<int> q;
    for (int i = 1; i <= tot; i++) {
      if (!in[i]) q.push(i);
      endnum[i] = 0;
    }
    while (!q.empty()) {
      int u = q.front();
      q.pop();
      if (tag[u]) endnum[u]++;
      if (u) endnum[pre[u]] += endnum[u], val[pre[u]] += val[u];
      if (u)
        if (!--in[pre[u]]) q.push(pre[u]);
    }
  }
  long long solve() {
    top_sort();
    long long ans = 0;
    for (int i = 1; i <= tot; i++) {
      if (len[i] == len[pre[i]]) continue;
      if (val[i] < 0)
        ans = max(ans, val[i] * (len[pre[i]] + 1));
      else
        ans = max(ans, val[i] * len[i]);
    }
    return ans;
  }
} sam;
char s[1000005];
char* p[100005];
int a[100005];
int main() {
  int n;
  cin >> n;
  int pos = 0;
  sam.init();
  for (int i = 1; i <= n; i++) {
    scanf("%s", s + pos);
    p[i] = s + pos;
    pos += strlen(s + pos) + 1;
  }
  for (int i = 1; i <= n; i++) scanf("%d", &a[i]);
  int tot = 0;
  for (int i = 1; i <= n; i++) {
    char* c = p[i];
    while (*c) {
      sam.extend((*c) - 'a', ++tot, a[i]);
      c++;
    }
    sam.reset();
  }
  printf("%lld\n", sam.solve());
  return 0;
}
