#include <bits/stdc++.h>
using namespace std;
const int N = 660000;
int wa[N], wb[N], wc[N], wd[N], sa[N], rnk[N], lcp[N], dp[N][22], pw2[26];
int len;
bool is_equal(int *r, int a, int b, int l, int n) {
  int la = r[a], lb = r[b], ra, rb;
  ra = a + l < n ? r[a + l] : -1;
  rb = b + l < n ? r[b + l] : -1;
  return (la == lb) && (ra == rb);
}
void build_suffix_array(string r) {
  int n = len, m = 256;
  int *x = wa, *y = wb;
  int i, tot, p;
  memset(wc, 0, sizeof(int) * m);
  for (i = 0; i < n; i++) wc[x[i] = r[i]]++;
  for (i = 1; i < m; i++) wc[i] += wc[i - 1];
  for (i = n - 1; i >= 0; i--) sa[--wc[x[i]]] = i;
  for (tot = 0, p = 1; tot + 1 < n; p <<= 1, m = tot + 1) {
    for (tot = 0, i = n - p; i < n; i++) y[tot++] = i;
    for (i = 0; i < n; i++)
      if (sa[i] >= p) y[tot++] = sa[i] - p;
    for (i = 0; i < n; i++) wd[i] = x[y[i]];
    memset(wc, 0, sizeof(int) * m);
    for (i = 0; i < n; i++) wc[wd[i]]++;
    for (i = 1; i < m; i++) wc[i] += wc[i - 1];
    for (i = n - 1; i >= 0; i--) sa[--wc[wd[i]]] = y[i];
    swap(x, y);
    x[sa[0]] = tot = 0;
    for (i = 1; i < n; i++)
      x[sa[i]] = is_equal(y, sa[i - 1], sa[i], p, n) ? tot : ++tot;
  }
}
void build_lcp(string r) {
  int n = len;
  int i;
  for (i = 0; i < n; i++) rnk[sa[i]] = i;
  for (lcp[0] = i = 0; i < n; i++)
    if (rnk[i]) {
      lcp[rnk[i]] = (i == 0) ? 0 : max(0, lcp[rnk[i - 1]] - 1);
      for (; r[i + lcp[rnk[i]]] == r[sa[rnk[i] - 1] + lcp[rnk[i]]];
           lcp[rnk[i]]++)
        ;
    }
  for (int i = 1; i < n; i++) dp[i][0] = lcp[i];
  pw2[0] = 1;
  for (int j = 1; j < 22; j++) {
    pw2[j] = pw2[j - 1] * 2;
    for (int i = 1; i + pw2[j] - 1 < n; i++)
      dp[i][j] = min(dp[i][j - 1], dp[i + pw2[j - 1]][j - 1]);
  }
}
int get_lcp(int i, int j) {
  int n = len;
  if (i >= n || j >= n) return 0;
  i = rnk[i], j = rnk[j];
  if (i == j) return n - sa[i];
  if (i > j) swap(i, j);
  i++;
  int p = 31 - __builtin_clz(j - i + 1);
  return min(dp[i][p], dp[j - pw2[p] + 1][p]);
}
string s[N], t;
const long long INF = 2e18;
long long dp2[N];
int c[N], rem[N], sum[N], p[N];
vector<int> vec[N], era[N];
set<int> S;
multiset<long long> V;
bool vis[N];
int main() {
  int n;
  scanf("%d", &n);
  for (int i = 1; i <= n; i++) {
    cin >> s[i];
    s[i] += "$";
    t += s[i];
    sum[i] = sum[i - 1] + s[i].length();
  }
  len = t.length();
  for (int i = 1; i <= n; i++) scanf("%d", c + i);
  for (int i = 1, j = 0; j < len; j++) {
    if (j == sum[i]) i++;
    p[j] = i;
    rem[j] = sum[i] - 1 - j;
  }
  build_suffix_array(t);
  build_lcp(t);
  int hd = 0;
  for (; t[sa[hd]] == '$'; hd++)
    ;
  for (int i = 0; i < len; i++) {
    dp2[i] = dp2[max(i - 1, 0)] + c[p[sa[i]]];
  }
  for (int i = hd; i < len; i++) {
    vec[min(rem[sa[i]], lcp[i])].push_back(i);
    era[rem[sa[i]]].push_back(i);
  }
  long long max_sum = -INF, ans = 0;
  S.insert(len);
  set<int>::iterator it;
  for (int i = 0; i <= len; i++) {
    if (!V.empty()) ans = max(ans, *(V.rbegin()) * i);
    for (int x : vec[i]) {
      it = S.lower_bound(x);
      if (it == S.begin()) {
        int big = *it;
        S.insert(x);
        V.insert(dp2[big - 1] - dp2[x - 1]);
        continue;
      }
      int big = *it, small = *(--it);
      S.insert(x);
      if (!vis[small]) V.erase(V.lower_bound(dp2[big - 1] - dp2[small - 1]));
      V.insert(dp2[big - 1] - dp2[x - 1]);
      V.insert(dp2[x - 1] - dp2[small - 1]);
    }
    for (int x : era[i]) {
      vis[x] = true;
      it = S.lower_bound(x);
      int big = *(++it);
      V.erase(V.lower_bound(dp2[big - 1] - dp2[x - 1]));
    }
  }
  printf("%lld\n", ans);
  return 0;
}
