#include <bits/stdc++.h>
using namespace std;
const int N = 5e5 + 7;
struct node {
  int son[26], pre, len;
} a[N << 1];
long long val[N << 1];
vector<int> g[N << 1];
int tot, tail;
void init() {
  tot = tail = 0;
  memset(a[0].son, 0, sizeof(a[0].son));
  a[0].pre = a[0].len = 0;
}
void ins(int k) {
  if (a[tail].son[k] && a[a[tail].son[k]].len == a[tail].len + 1) {
    tail = a[tail].son[k];
    return;
  }
  int p, q, np = ++tot, nq;
  a[np].len = a[tail].len + 1;
  for (p = tail; p && !a[p].son[k]; p = a[p].pre) a[p].son[k] = np;
  if (!a[p].son[k]) {
    a[p].son[k] = np;
    a[np].pre = p;
  } else {
    q = a[p].son[k];
    if (a[q].len == a[p].len + 1)
      a[np].pre = q;
    else {
      a[nq = ++tot] = a[q];
      a[nq].len = a[p].len + 1;
      a[q].pre = a[np].pre = nq;
      for (; a[p].son[k] == q; p = a[p].pre) a[p].son[k] = nq;
    }
  }
  tail = np;
}
string s[N];
int c[N];
long long ans = 0;
void dfs(int u) {
  for (int v : g[u]) {
    dfs(v);
    val[u] += val[v];
  }
  ans = max(ans, val[u] * a[u].len);
}
int main() {
  ios::sync_with_stdio(0);
  int n;
  init();
  cin >> n;
  for (int i = int(1); i <= int(n); ++i) cin >> s[i];
  for (int i = int(1); i <= int(n); ++i) cin >> c[i];
  for (int i = int(1); i <= int(n); ++i) {
    tail = 0;
    int l = s[i].length();
    for (int j = int(0); j <= int(l - 1); ++j)
      ins(s[i][j] - 'a'), val[tail] += 1ll * c[i];
  }
  for (int i = int(1); i <= int(tot); ++i) g[a[i].pre].push_back(i);
  dfs(0);
  cout << ans << endl;
}
