#include <bits/stdc++.h>
using namespace std;
struct Edge {
  int to, nxt;
} edge[1000100];
struct SAM {
  int len, fa, ch[30];
} point[1001000];
string S[100100];
int n, cnt, head[1000100], lst = 1, idx = 1;
long long sum[1000100];
long long ans;
void addedge(int x, int y) {
  edge[++cnt].to = y;
  edge[cnt].nxt = head[x];
  head[x] = cnt;
}
void dfs(int x) {
  for (int i = head[x]; i; i = edge[i].nxt) {
    int upup = edge[i].to;
    dfs(upup);
    sum[x] += sum[upup];
  }
  ans = max(ans, 1ll * sum[x] * point[x].len);
}
void append(int x, int val) {
  if (point[lst].ch[x] && point[point[lst].ch[x]].len == point[lst].len + 1) {
    lst = point[lst].ch[x];
    sum[lst] += val;
    return;
  }
  int p = lst, nowp = lst = ++idx;
  sum[idx] = val;
  point[nowp].len = point[p].len + 1;
  while (p && !point[p].ch[x]) point[p].ch[x] = nowp, p = point[p].fa;
  if (!p)
    point[nowp].fa = 1;
  else {
    int q = point[p].ch[x];
    if (point[p].len == point[q].len - 1)
      point[nowp].fa = q;
    else {
      int nowq = ++idx;
      point[nowq] = point[q];
      point[nowq].len = point[p].len + 1;
      point[q].fa = point[nowp].fa = nowq;
      while (p && point[p].ch[x] == q) point[p].ch[x] = nowq, p = point[p].fa;
    }
  }
}
int main() {
  scanf("%d", &n);
  for (int i = 1; i <= n; i++) cin >> S[i];
  for (int i = 1, x; i <= n; i++) {
    lst = 1;
    scanf("%d", &x);
    int len = S[i].length();
    for (int j = 0; j < len; j++) append(S[i][j] - 'a', x);
  }
  for (int i = 2; i <= idx; i++) addedge(point[i].fa, i);
  dfs(1);
  printf("%lld\n", ans);
}
