#include <bits/stdc++.h>
using namespace std;
const int N = 1000100;
int ch[N][26], val[N], fa[N], cnt = 1;
void extend(int p, int x) {
  if (!ch[p][x]) {
    int np = ++cnt;
    val[np] = val[p] + 1;
    for (; p && !ch[p][x]; p = fa[p]) ch[p][x] = np;
    if (p == 0) {
      fa[np] = 1;
      return;
    }
    int q = ch[p][x];
    if (val[q] == val[p] + 1)
      fa[np] = q;
    else {
      int nq = ++cnt;
      for (int i = 0; i <= 25; i++) ch[nq][i] = ch[q][i];
      val[nq] = val[p] + 1;
      fa[nq] = fa[q];
      fa[np] = fa[q] = nq;
      for (; p && ch[p][x] == q; p = fa[p]) ch[p][x] = nq;
    }
  } else {
    int q = ch[p][x];
    if (val[q] != val[p] + 1) {
      int nq = ++cnt;
      for (int i = 0; i <= 25; i++) ch[nq][i] = ch[q][i];
      val[nq] = val[p] + 1;
      fa[nq] = fa[q];
      fa[q] = nq;
      for (; p && ch[p][x] == q; p = fa[p]) ch[p][x] = nq;
    }
  }
}
vector<int> que[N];
void insert(char s[], int k) {
  int p = 1;
  for (int i = 0; s[i]; i++) {
    int x = s[i] - 'a';
    extend(p, x);
    p = ch[p][x];
    que[k].push_back(p);
  }
}
long long dp[N], ans;
vector<int> son[N];
void solve(int u) {
  for (int v : son[u]) {
    solve(v);
    dp[u] += dp[v];
  }
  ans = max(ans, dp[u] * val[u]);
}
char s[N];
int main() {
  int n;
  cin >> n;
  for (int i = 1; i <= n; i++) {
    scanf("%s", s);
    insert(s, i);
  }
  for (int i = 1; i <= n; i++) {
    int add;
    scanf("%d", &add);
    for (int u : que[i]) dp[u] += add;
  }
  for (int i = 1; i <= cnt; i++) son[fa[i]].push_back(i);
  solve(1);
  cout << ans << endl;
  return 0;
}
