Barney was hanging out with Nora for a while and now he thinks he may have feelings for her. Barney wants to send her a cheesy text message and wants to make her as happy as possible.
![](https://i-blog.csdnimg.cn/blog_migrate/921f03b5ace0a6de1f9ed6765bad6161.png)
Initially, happiness level of Nora is 0. Nora loves some pickup lines like "I'm falling for you" and stuff. Totally, she knows n pickup lines, each consisting only of lowercase English letters, also some of them may be equal (in writing, but different in pronouncing or meaning though). Every time Nora sees i-th pickup line as a consecutive subsequence of Barney's text message her happiness level increases by ai. These substrings may overlap, for example, Nora will see the pickup line aa twice and the pickup line ab once in text message aaab.
Due to texting app limits, Barney's text may have up to l characters.
Barney asked you to help him make Nora as much happy as possible, it's gonna be legen...
The first line of input contains two integers n and l (1 ≤ n ≤ 200, 1 ≤ l ≤ 1014) — the number of pickup lines and the maximum length of Barney's text.
The second line contains n integers a1, a2, ..., an (1 ≤ ai ≤ 100), meaning that Nora's happiness level increases by ai after every time seeing i-th pickup line.
The next n lines contain the pickup lines. i-th of them contains a single string si consisting of only English lowercase letter. Summary length of all pickup lines does not exceed 200.
All strings are not empty.
Print the only integer — the maximum possible value of Nora's happiness level after reading Barney's text.
3 6 3 2 1 heart earth art
6
3 6 3 2 8 heart earth art
16
An optimal answer for the first sample case is hearth containing each pickup line exactly once.
An optimal answer for the second sample case is artart.
大意就是说给定l和n个字符串, 每个字符串有一个分数, 求一个长度为l的字符串能获得的最大分数. 包含了给定字符串就能获得给定分数, 并且字符串之间可以重叠.
给定长度的字符串, 包含xx串就... 这种类型的题已经很常见了... 一看就是要AC自动机上dp啊. 比如说设dp[i][j]表示从root出发走i步到了节点j此时能获得的最大分数. 这个dp很好转移. 到了有end节点就加分即可.
但l太大显然dp不可过... 那么就只能上矩阵快速幂来优化辣... 矩阵a[i][j]表示从i走到j的最大分数. 矩阵乘法每乘一次可以看作走一步(这个可以详见POJ2778. 那么转移n次就可以了, 直接上快速幂. 这里矩阵不再是乘, 而是加和取max. 之所以能这样矩阵乘法是因为仔细想想可以发现从i走到j这样的路径这是可以拆开的... 其实你可以看成floyed的类似感觉.
注意有重复串val要累加. 并且当前点也要累加fail的val(因为是后缀啊).
#include<bits/stdc++.h>
using namespace std;
typedef long long lnt;
const int maxn = 205;
char ss[maxn];
int m, tot;
lnt val[maxn], ans, n;
int w[maxn], c[maxn][26], fail[maxn];
inline void insert(int nw) {
int p = 0;
for (int i = 0; ss[i]; ++ i) {
int idx = ss[i] - 'a';
if (!c[p][idx]) c[p][idx] = ++ tot;
p = c[p][idx];
}
val[p] += w[nw];
}
queue<int> q;
inline void bfs() {
for (int i = 0; i < 26; ++ i)
if (c[0][i]) q.push(c[0][i]);
while (!q.empty()) {
int u = q.front(); q.pop();
for (int i = 0; i < 26; ++ i) {
int &v = c[u][i];
if (!v) {v = c[fail[u]][i]; continue;}
fail[v] = c[fail[u]][i], val[v] += val[fail[v]];
q.push(v);
}
}
}
struct Matrix {
lnt mat[maxn][maxn];
Matrix() {
memset(mat, -1, sizeof (mat));
}
inline friend Matrix operator * (const Matrix &a, const Matrix &b) {
Matrix c;
for (int i = 0; i <= tot; ++ i)
for (int k = 0; k <= tot; ++ k) if (~a.mat[i][k])
for (int j = 0; j <= tot; ++ j) if (~b.mat[k][j])
c.mat[i][j] = max(c.mat[i][j], a.mat[i][k] + b.mat[k][j]);
return c;
}
}a, ret;
int main() {
scanf("%d%I64d", &m, &n);
for (int i = 0; i < m; ++ i) scanf("%d", &w[i]);
for (int i = 0; i < m; ++ i) {
scanf("%s", ss);
insert(i);
}
bfs();
for (int i = 0; i <= tot; ++ i)
for (int p = 0; p < 26; ++ p) {
a.mat[i][c[i][p]] = val[c[i][p]];
}
ret = a, -- n;
while (n) {
if (n & 1) ret = ret * a;
a = a * a, n >>= 1;
}
for (int i = 0; i <= tot; ++ i)
ans = max(ans, ret.mat[0][i]);
printf("%I64d\n", ans);
}