You are given n strings ti. Each string has cost ci.

Let's define the function of string , where ps, i is the number of occurrences of s in ti, |s| is the length of the string s. Find the maximal value of function f(s) over all strings.

Note that the string s is not necessarily some string from t.

Input

The first line contains the only integer n (1 ≤ n ≤ 105) — the number of strings in t.

Each of the next n lines contains contains a non-empty string ti. ti contains only lowercase English letters.

It is guaranteed that the sum of lengths of all strings in t is not greater than 5·105.

The last line contains n integers ci ( - 107 ≤ ci ≤ 107) — the cost of the i-th string.

Output

Print the only integer a — the maximal value of the function f(s) over all strings s. Note one more time that the string s is not necessarily from t.

Examples

Input
2
aa
bb
2 1
Output
4
Input
2
aa
ab
2 1
Output
5
#include<bits/stdc++.h>
#define ll long long
#define rep2(i,a,b) for(int i=a;i>=b;i--)
#define rep(i,a,b) for(int i=a;i<=b;i++)
using namespace std;
const int maxn=;
char c[maxn],s[maxn]; int End[maxn];
struct SAM{
int ch[maxn][],fa[maxn],maxlen[maxn],cnt,last;
int a[maxn],b[maxn],num[maxn]; ll val[maxn],ans=;
void init()
{
cnt=; memset(ch[],,sizeof(ch[]));
}
int add(int x)
{
int np=++cnt,p=last; last=np;
maxlen[np]=maxlen[p]+;memset(ch[np],,sizeof(ch[np]));
while(p&&!ch[p][x]) ch[p][x]=np,p=fa[p];
if(!p) fa[np]=;
else {
int q=ch[p][x];
if(maxlen[q]==maxlen[p]+) fa[np]=q;
else {
int nq=++cnt; maxlen[nq]=maxlen[p]+;
fa[nq]=fa[q]; fa[q]=fa[np]=nq;
memcpy(ch[nq],ch[q],sizeof(ch[q]));
while(p&&ch[p][x]==q) ch[p][x]=nq,p=fa[p];
}
}
}
void sort()
{
rep(i,,cnt) a[i]=;
rep(i,,cnt) a[maxlen[i]]++;
rep(i,,cnt) a[i]+=a[i-];
rep(i,,cnt) b[a[maxlen[i]]--]=i;
}
void solve(int N)
{
sort();
rep(i,,N) scanf("%d",&num[i]);
rep(i,,N){
int now=;
rep(j,End[i-]+,End[i]){
now=ch[now][s[j]-'a'];
val[now]+=num[i];
}
}
rep2(i,cnt,) val[fa[b[i]]]+=val[b[i]];
rep(i,,cnt) ans=max(ans,val[i]*maxlen[i]);
}
}T;
int a[maxn];
int main()
{
int N,L;
scanf("%d",&N);
T.init();
rep(i,,N) {
scanf("%s",c+);
L=strlen(c+); T.last=;
rep(j,,L) T.add(c[j]-'a');
rep(j,,L) s[End[i-]+j]=c[j];
End[i]=End[i-]+L;
}
T.solve(N);
printf("%lld\n",T.ans);
return ;
}

N个串,给个串有一个价值num[i],现在让你找一个串X,它在第i个字符串出现次数为pi,而且∑len(X)*num[i]*p[i]最大。

思路:后缀自动机,对于每个集合,num之和一样,所以肯定选择最长的一个。

CodeForces - 616F:Expensive Strings (后缀自动机)的更多相关文章

  1. [Educational Round 5][Codeforces 616F. Expensive Strings]

    这题调得我心疲力竭...Educational Round 5就过一段时间再发了_(:з」∠)_ 先后找了三份AC代码对拍,结果有两份都会在某些数据上出点问题...这场的数据有点水啊_(:з」∠)_[ ...

  2. Codeforces 452E Three Strings(后缀自动机)

    上学期很认真地学了一些字符串的常用工具,各种 suffix structre,但是其实对后缀自动机这个部分是理解地不太透彻的,以致于看了师兄A这题的代码后,我完全看不懂,于是乎重新看回一些学习后缀自动 ...

  3. Codeforces 235C Cyclical Quest - 后缀自动机

    Some days ago, WJMZBMR learned how to answer the query "how many times does a string x occur in ...

  4. HDU 6208 The Dominator of Strings 后缀自动机

    The Dominator of Strings Time Limit: 3000/3000 MS (Java/Others)    Memory Limit: 65535/32768 K (Java ...

  5. Codechef2015 May - Chef and Strings (后缀自动机)

    用后缀自动机统计出出现1~n次的串的数量f[i] 对于ans[k]=sigma(f[i]*C(i,k)) i>=k ; mo=; ..maxn] of dword; nt:..maxn,'a'. ...

  6. CF 149E Martian Strings 后缀自动机

    这里给出来一个后缀自动机的题解. 考虑对 $s$ 的正串和反串分别建后缀自动机. 对于正串的每个节点维护 $endpos$ 的最小值. 对于反串的每个节点维护 $endpos$ 的最大值. 这两个东西 ...

  7. Codeforces.700E.Cool Slogans(后缀自动机 线段树合并 DP)

    题目链接 \(Description\) 给定一个字符串\(s[1]\).一个字符串序列\(s[\ ]\)满足\(s[i]\)至少在\(s[i-1]\)中出现过两次(\(i\geq 2\)).求最大的 ...

  8. 字符串(后缀自动机):Codeforces Round #129 (Div. 1) E.Little Elephant and Strings

    E. Little Elephant and Strings time limit per test 3 seconds memory limit per test 256 megabytes inp ...

  9. codeforces 204E. Little Elephant and Strings(广义后缀自动机,Parent树)

    传送门在这里. 大意: 给一堆字符串,询问每个字符串有多少子串在所有字符串中出现K次以上. 解题思路: 这种子串问题一定要见后缀自动机Parent树Dfs序统计出现次数都是套路了吧. 这道题统计子串个 ...

随机推荐

  1. Windows平台dump文件的产生,调试;工程配置pdb文件怎么生成

    http://blog.csdn.net/byxdaz/article/details/25872151

  2. 在vue项目中使用sass

    如果想开发移动端项目,那么适配的时候sass必不可缺,但是 npm  安装sass时候总是报错失败! 研究半天发现可以解决的方法,亲测有效 1.先换成淘宝镜像再安装 npm install -g cn ...

  3. pycharm搭建开发配置,远程调试,数据库配置,git配置等

    1 开发环境搭建 1.1 简介 使用虚拟机作为代码运行环境,本地使用pycharm进行代码编辑,使用远程调试功能进行debug. 1.1 安装centos虚拟机环境: 1.操作系统: 2.网络配置: ...

  4. 【配置】pom.xml的配置

    pom.xml的配置: 地址:https://mvnrepository.com/ 示例:配置log4j 1.在搜索框中搜索log4j 2.在搜索结果页点击log4j 3.选择一个最新的版本,点击 4 ...

  5. 修改placeholder的值---input-placeholder

    input-placeholder .box::input-placeholder 目前所有的浏览器都要加前缀 .box::-webkit-input-placeholder{ color: agba ...

  6. java获得当前系统时间三种方法

    参见: http://blog.csdn.net/cloume/article/details/46624637

  7. 四:(之一和之二) docker架构和底层技术分析(C/S架构)

    1.架构和底层技术 Docker Host提供了RESTUL api,使docker client可以通过这些命令调用dockerd. Registry是一个公用的存储镜像的容器,类似于github. ...

  8. SWAP 简介

    swap 交换分区,是存放在内存当中的临时数据(断电数据丢失) SWAP作用:当内存不足时会导致系统挂了,SWAP就是起到一个临时内存的作用,当内存不足时SWAP充当临时内存,防止系统挂掉

  9. Android开发 ---Fragment片段布局

    前言 Fragment想必大家不陌生吧,在日常开发中,对于Fragment的使用也很频繁,现在主流的APP中,基本的架构也都是一个主页,然后每个Tab项用Fragment做布局,不同选项做切换,使用起 ...

  10. 8.Python爬虫实战一之爬取糗事百科段子

    大家好,前面入门已经说了那么多基础知识了,下面我们做几个实战项目来挑战一下吧.那么这次为大家带来,Python爬取糗事百科的小段子的例子. 首先,糗事百科大家都听说过吧?糗友们发的搞笑的段子一抓一大把 ...