#include <bits/stdc++.h>
using namespace std;
const int MaxC = 256;
const int MAX = 600001;
int offset, n;
int suma[MAX], tmp[MAX];
int rank1[MAX], rank2[MAX];
int c[MAX], p[MAX], lcp[MAX];
bool different(int a, int b) {
  if (b + offset >= n) return true;
  if (rank1[a] != rank1[b]) return true;
  if (rank2[a] != rank2[b]) return true;
  return false;
}
void suffix_array(char *text) {
  n = strlen(text);
  for (int i = 0; i < MaxC; i++) suma[i] = 0;
  for (int i = 0; i < n; i++) suma[text[i]]++;
  for (int i = 0; i < MaxC; i++) suma[i + 1] += suma[i];
  for (int i = 0; i < n; i++) c[i] = suma[text[i]] - 1;
  for (offset = 1; offset <= n; offset <<= 1) {
    for (int i = 0; i < n; i++) {
      rank1[i] = c[i];
      rank2[i] = (i + offset < n) ? c[i + offset] : 0;
    }
    fill(suma, suma + n, 0);
    for (int i = 0; i < n; i++) suma[rank2[i]]++;
    for (int i = 0; i < n; i++) suma[i + 1] += suma[i];
    for (int i = 0; i < n; i++) tmp[--suma[rank2[i]]] = i;
    fill(suma, suma + n, 0);
    for (int i = 0; i < n; i++) suma[rank1[i]]++;
    for (int i = 0; i < n; i++) suma[i + 1] += suma[i];
    for (int i = n - 1; i >= 0; i--) p[--suma[rank1[tmp[i]]]] = tmp[i];
    c[p[0]] = 0;
    for (int i = 1; i < n; i++) {
      c[p[i]] = c[p[i - 1]];
      if (different(p[i], p[i - 1])) c[p[i]]++;
    }
  }
  offset = 0;
  for (int i = 0; i < n; i++) {
    if (c[i] == n - 1) {
      lcp[n - 1] = 0;
      continue;
    }
    while (text[i + offset] != '{' &&
           text[i + offset] == text[p[c[i] + 1] + offset])
      offset++;
    lcp[c[i]] = offset;
    if (offset > 0) offset--;
  }
}
const int MAXN = 100000;
char s[MAX];
string w;
int len[MAXN];
long long cost[MAXN];
long long cc[MAX];
long long sc[MAX];
int l[MAX], r[MAX];
int st[MAX];
long long lennn[MAX];
int main() {
  int N;
  cin >> N;
  long long sol = 0ll;
  int pos = 0;
  for (int i = 0; i < N; i++) {
    cin >> w;
    len[i] = w.size();
    for (int j = 0; j < len[i]; j++) {
      lennn[pos] = len[i];
      s[pos++] = w[j];
    }
    s[pos++] = '{';
  }
  for (int i = 0; i < N; i++) {
    cin >> cost[i];
  }
  if (N == 1) {
    sol = (long long)len[0] * (long long)cost[0];
  }
  suffix_array(s);
  pos = 0;
  for (int i = 0; i < N; i++) {
    for (int j = 0; j < len[i]; j++) {
      cc[pos + j] = cost[i];
    }
    cc[pos + len[i]] = 0;
    pos += len[i] + 1;
  }
  int sz = n - N;
  for (int i = 0; i < sz; i++) {
    sc[i] = cc[p[i]];
    if (i > 0) {
      sc[i] += sc[i - 1];
    }
  }
  int sti = 0;
  for (int i = 0; i < sz - 1; i++) {
    l[i] = i;
    while (sti > 0 && lcp[st[sti - 1]] >= lcp[i]) {
      l[i] = l[st[sti - 1]];
      sti--;
    }
    st[sti++] = i;
  }
  sti = 0;
  for (int i = sz - 2; i >= 0; i--) {
    r[i] = i;
    while (sti > 0 && lcp[st[sti - 1]] >= lcp[i]) {
      r[i] = r[st[sti - 1]];
      sti--;
    }
    st[sti++] = i;
  }
  for (int i = 0; i < sz - 1; i++) {
    long long val = sc[r[i] + 1];
    if (l[i] > 0) {
      val -= sc[l[i] - 1];
    }
    val *= (long long)lcp[i];
    sol = max(sol, val);
    int auxx = lcp[i];
    if (i > 0) {
      auxx = max(auxx, lcp[i - 1]);
    }
    if (s[p[i] + auxx] != '{') {
      sol = max(sol, lennn[p[i]] * cc[p[i]]);
    }
  }
  int i = sz - 1;
  lcp[i] = 0;
  int auxx = lcp[i];
  if (i > 0) {
    auxx = max(auxx, lcp[i - 1]);
  }
  if (s[p[i] + auxx] != '{') {
    sol = max(sol, lennn[p[i]] * cc[p[i]]);
  }
  cout << sol << '\n';
}
