#include <bits/stdc++.h>
using namespace std;
const int N = 1e6 + 5;
int ch[N][26], fa[N], d[N], ct, last;
void extend(int, int);
int id[N], c[N], C[N];
long long val[N];
string s[N];
int main() {
  int n;
  cin >> n;
  ct = 1;
  for (int i = 1; i <= n; i++) cin >> s[i];
  for (int i = 1; i <= n; i++) cin >> C[i];
  for (int i = 1; i <= n; i++) {
    last = 1;
    for (char c : s[i]) extend(c - 'a', C[i]);
  }
  for (int i = 1; i <= ct; i++) c[d[i]]++;
  for (int i = 1; i <= ct; i++) c[i] += c[i - 1];
  for (int i = 1; i <= ct; i++) id[c[d[i]]--] = i;
  long long ans = 0;
  for (int i = ct; i > 1; i--) {
    int p = id[i];
    val[fa[p]] += val[p];
    ans = max(ans, val[p] * d[p]);
  }
  cout << ans;
}
void extend(int c, int x) {
  int p = last;
  if (ch[p][c] && d[ch[p][c]] == d[p] + 1) {
    last = ch[p][c];
    val[last] += x;
    return;
  }
  int np = ++ct;
  d[np] = d[p] + 1, last = np;
  val[np] += x;
  while (p && !ch[p][c]) ch[p][c] = np, p = fa[p];
  if (!p)
    fa[np] = 1;
  else {
    int q = ch[p][c];
    if (d[q] == d[p] + 1)
      fa[np] = q;
    else {
      int nq = ++ct;
      memcpy(ch[nq], ch[q], 26 << 2);
      d[nq] = d[p] + 1;
      fa[nq] = fa[q];
      fa[np] = fa[q] = nq;
      while (p && ch[p][c] == q) ch[p][c] = nq, p = fa[p];
    }
  }
}
