#include<bits/stdc++.h>
using namespace std;
typedef long long int ll;
#define pb push_back
#define fi first
#define se second
#define ld long double
#define PI 3.141592653589793238462643383279
#define endl '\n'
const int md = 1e9 + 7;

const int NN = 205;
int sz;
const ll inf = 1e17;
class matrix{
public:
	ll mat[NN][NN];
	matrix(){
		for(int i = 0; i < NN; i++)
			for(int j = 0; j < NN; j++)
				mat[i][j] = - inf;
	}
	inline matrix operator * (const matrix &a){
		matrix temp;
		for(int i = 0; i < sz; i++)
			for(int j = 0; j < sz; j++){
				temp.mat[i][j] = - inf;
				for(int k = 0; k < sz; k++)
				temp.mat[i][j] = max(temp.mat[i][j], mat[i][k] + a.mat[k][j]);
			}
		return temp;
	}
	inline void operator = (const matrix &b){
		for(int i = 0; i < sz; i++)
			for(int j = 0; j < sz; j++)
				mat[i][j] = b.mat[i][j];
	}
};
matrix matpow(matrix a,int k){
	bool b = 1;
	matrix ans;
	while(k){
		if(k & 1)
		{
			if(b)
				ans = a,b = 0;
			else
				ans = ans * a;
		}
		a = a * a;
		k >>= 1;
	}
	return ans;
}
const int CHAR = 26;
const int N = 205;

int go[N][CHAR];
#define conv(ch) ch - 'a'
class AhoCora {
public:
	struct Node {
		int ch[CHAR];
		int link;
		ll val;
	}nd[N];
	int Q[N]; int qs, qe;
	int size;
	int createNode() {
		int ret = size++;
		for (int i = 0; i<CHAR; i++) {
			nd[ret].ch[i] = -1;
		}
		nd[ret].val = 0;
		return ret;
	}
	void init() {
		size = 0;
		createNode();
	}
	template<typename A>
	void add(A str,const int &sz,const int &val) {
		int cur = 0;
		for (int i = 0; i<sz; i++) {
			int c = conv(str[i]);
			if (nd[cur].ch[c] == -1) {
				int nnd = createNode();
				nd[cur].ch[c] = nnd;
			}
			cur = nd[cur].ch[c];
		}
		nd[cur].val = (nd[cur].val + val) % md;
	}
	inline int next(int u, const int &c) {
		if(go[u][c])
			return go[u][c] - 1;
		if(nd[u].ch[c] != -1)
			return nd[u].ch[c];
		if(!u)
			return 0;
		go[u][c] = 1 + next(nd[u].link, c);
		return go[u][c] - 1;
	}
	void computelink() {
		qs = qe = 0;
		nd[0].link = 0;
		for (int x, i = 0; i<CHAR; i++) {
			if ((x = nd[0].ch[i]) >= 0) {
				Q[qe++] = x;
				nd[x].link = 0;
			}
		}
		while (qs<qe) {
			int u = Q[qs++];
			for (int v, i = 0; i<CHAR; i++) {
				if ((v = nd[u].ch[i]) >= 0) {
					Q[qe++] = v;
					int m = nd[u].link;
					int L = nd[v].link = next(m, i);
					nd[v].val = (nd[v].val + nd[L].val) % md;
				}
			}
		}
	}
}acc;
int a[N];
char s[N];
signed main()
{
	//freopen("inp.txt","r",stdin);
	int n,l;
	acc.init();
	scanf("%d %d",&n,&l);
	for(int i = 1; i <= n; i++)
		scanf("%d ",&a[i]);
	for(int i = 1; i <= n; i++){
		scanf("%s",s);
		acc.add(s,strlen(s),a[i]);
	}
	acc.computelink();
	sz = acc.size;
	matrix p;
	for(int i = 0; i < sz; i++){
		for(int j = 0; j < 26; j++){
			int k = acc.next(i,j);
			p.mat[i][k] = acc.nd[k].val;
		}
	}
	p = matpow(p,l);
	ll maxi = - 1;
	for(int i = 0; i < sz; i++)
		maxi = max(maxi, p.mat[0][i]);
	printf("%lld",maxi);
	return 0;
}


#include<bits/stdc++.h>
using namespace std;
typedef long long int ll;
#define pb push_back
#define fi first
#define se second
#define ld long double
#define PI 3.141592653589793238462643383279
#define endl '\n'

const int NN = 205;
int sz;
const ll inf = 1e17;
class matrix{
public:
	ll mat[NN][NN];
	matrix(){
		for(int i = 0; i < NN; i++)
			for(int j = 0; j < NN; j++)
				mat[i][j] = - inf;
	}
	inline matrix operator * (const matrix &a){
		matrix temp;
		for(int i = 0; i < sz; i++)
			for(int j = 0; j < sz; j++){
				temp.mat[i][j] = - inf;
				for(int k = 0; k < sz; k++)
				temp.mat[i][j] = max(temp.mat[i][j], mat[i][k] + a.mat[k][j]);
			}
		return temp;
	}
	inline void operator = (const matrix &b){
		for(int i = 0; i < sz; i++)
			for(int j = 0; j < sz; j++)
				mat[i][j] = b.mat[i][j];
	}
};
matrix matpow(matrix a,int k){
	bool b = 1;
	matrix ans;
	while(k){
		if(k & 1)
		{
			if(b)
				ans = a,b = 0;
			else
				ans = ans * a;
		}
		a = a * a;
		k >>= 1;
	}
	return ans;
}
const int CHAR = 26;
const int N = 205;

int go[N][CHAR];
#define conv(ch) ch - 'a'
class AhoCora {
public:
	struct Node {
		int ch[CHAR];
		int link;
		ll val;
	}nd[N];
	int Q[N], qs, qe;
	int size;
	int createNode() {
		int ret = size++;
		for (int i = 0; i<CHAR; i++) {
			nd[ret].ch[i] = -1;
		}
		nd[ret].val = 0;
		return ret;
	}
	void init() {
		size = 0;
		createNode();
	}
	template<typename A>
	void add(A str,const int &sz,const int &val) {
		int cur = 0;
		for (int i = 0; i<sz; i++) {
			int c = conv(str[i]);
			if (nd[cur].ch[c] == -1) {
				int nnd = createNode();
				nd[cur].ch[c] = nnd;
			}
			cur = nd[cur].ch[c];
		}
		nd[cur].val = nd[cur].val + val;
	}
	inline int next(int u, const int &c) {
		if(go[u][c])
			return go[u][c] - 1;
		if(nd[u].ch[c] != -1)
			return nd[u].ch[c];
		if(!u)
			return 0;
		go[u][c] = 1 + next(nd[u].link, c);
		return go[u][c] - 1;
	}
	void computelink() {
		qs = qe = 0;
		nd[0].link = 0;
		for (int x, i = 0; i<CHAR; i++) {
			if ((x = nd[0].ch[i]) >= 0) {
				Q[qe++] = x;
				nd[x].link = 0;
			}
		}
		while (qs<qe) {
			int u = Q[qs++];
			for (int v, i = 0; i<CHAR; i++) {
				if ((v = nd[u].ch[i]) >= 0) {
					Q[qe++] = v;
					int m = nd[u].link;
					int L = nd[v].link = next(m, i);
					nd[v].val += nd[L].val;
				}
			}
		}
	}
}acc;
int a[N];
char s[N];
signed main()
{
//	freopen("inp.txt","r",stdin);
	int n;
	ll l;
	acc.init();
	scanf("%d %lld",&n,&l);
	for(int i = 1; i <= n; i++)
		scanf("%d ",&a[i]);
	for(int i = 1; i <= n; i++){
		scanf("%s",s);
		acc.add(s,strlen(s),a[i]);
	}
	acc.computelink();
	sz = acc.size;
	matrix p;
	for(int i = 0; i < sz; i++){
		for(int j = 0; j < 26; j++){
			int k = acc.next(i,j);
			p.mat[i][k] = acc.nd[k].val;
		}
	}
	p = matpow(p,l);
	ll maxi = - 1;
	for(int i = 0; i < sz; i++)
		maxi = max(maxi, p.mat[0][i]);
	printf("%lld",maxi);
	return 0;
}


#include<bits/stdc++.h>
using namespace std;
typedef long long int ll;
#define pb push_back
#define fi first
#define se second
#define ld long double
#define int long long int
#define PI 3.141592653589793238462643383279
#define endl '\n'
const int md = 1e9 + 7;

const int NN = 205;
int sz;
const int inf = 1e17;
class matrix{
public:
	int mat[NN][NN];
	matrix(){
		for(int i = 0; i < NN; i++)
			for(int j = 0; j < NN; j++)
				mat[i][j] = - inf;
	}
	inline matrix operator * (const matrix &a){
		matrix temp;
		for(int i = 0; i < sz; i++)
			for(int j = 0; j < sz; j++){
				temp.mat[i][j] = - inf;
				for(int k = 0; k < sz; k++)
				temp.mat[i][j] = max(temp.mat[i][j], mat[i][k] + a.mat[k][j]);
			}
		return temp;
	}
	inline void operator = (const matrix &b){
		for(int i = 0; i < sz; i++)
			for(int j = 0; j < sz; j++)
				mat[i][j] = b.mat[i][j];
	}
};
const int CHAR = 26;
const int N = 205;

int go[N][CHAR];
#define conv(ch) ch - 'a'
class AhoCora {
public:
	struct Node {
		int ch[CHAR];
		int link;
		int val;
	}nd[N];
	int Q[N]; int qs, qe;
	int size;
	int createNode() {
		int ret = size++;
		for (int i = 0; i<CHAR; i++) {
			nd[ret].ch[i] = -1;
		}
		nd[ret].val = 0;
		return ret;
	}
	void init() {
		size = 0;
		createNode();
	}
	template<typename A>
	void add(A str,const int &sz,const int &val) {
		int cur = 0;
		for (int i = 0; i<sz; i++) {
			int c = conv(str[i]);
			if (nd[cur].ch[c] == -1) {
				int nnd = createNode();
				nd[cur].ch[c] = nnd;
			}
			cur = nd[cur].ch[c];
		}
		nd[cur].val = (nd[cur].val + val) % md;
	}
	inline int next(int u, int c) {
		if(go[u][c])
			return go[u][c] - 1;
		while (u && nd[u].ch[c]<0)
			u = nd[u].link;
		go[u][c] = (nd[u].ch[c] < 0) ? 1 : nd[u].ch[c] + 1;
		return go[u][c] - 1;
	}
	void computelink() {
		qs = qe = 0;
		nd[0].link = 0;
		for (int x, i = 0; i<CHAR; i++) {
			if ((x = nd[0].ch[i]) >= 0) {
				Q[qe++] = x;
				nd[x].link = 0;
			}
		}
		while (qs<qe) {
			int u = Q[qs++];
			for (int v, i = 0; i<CHAR; i++) {
				if ((v = nd[u].ch[i]) >= 0) {
					Q[qe++] = v;
					int m = nd[u].link;
					int L = nd[v].link = next(m, i);
					nd[v].val = (nd[v].val + nd[L].val) % md;
				}
			}
		}
	}
}acc;
int a[N];
char s[N];
matrix matpow(matrix a,int k){
	if(k == 1)
		return a;
	matrix p = matpow(a,k/2);
	p = p * p;
	if(k & 1)
		p = p * a;
	return p;
}
signed main()
{
	//freopen("inp.txt","r",stdin);
	int n,l;
	acc.init();
	scanf("%lld %lld",&n,&l);
	for(int i = 1; i <= n; i++)
		scanf("%lld ",&a[i]);
	for(int i = 1; i <= n; i++){
		scanf("%s",s);
		acc.add(s,strlen(s),a[i]);
	}
	acc.computelink();
	sz = acc.size;
	matrix p;
	for(int i = 0; i < sz; i++){
		for(int j = 0; j < 26; j++){
			int k = acc.next(i,j);
			p.mat[i][k] = acc.nd[k].val;
		}
	}
	p = matpow(p,l);
	int maxi = - inf;
	for(int i = 0; i < sz; i++)
		maxi = max(maxi, p.mat[0][i]);
	printf("%lld",maxi);
	return 0;
}


#include<bits/stdc++.h>
using namespace std;
typedef long long int ll;
#define pb push_back
#define fi first
#define se second
#define ld long double
#define int long long int
#define PI 3.141592653589793238462643383279
#define endl '\n'
const int md = 1e9 + 7;

const int NN = 205;
int sz;
const int inf = 1e17;
class matrix{
public:
	int mat[NN][NN];
	matrix(){
		for(int i = 0; i < NN; i++)
			for(int j = 0; j < NN; j++)
				mat[i][j] = - inf;
	}
	inline matrix operator * (const matrix &a){
		matrix temp;
		for(int i = 0; i < sz; i++)
			for(int j = 0; j < sz; j++){
				temp.mat[i][j] = - inf;
				for(int k = 0; k < sz; k++)
				temp.mat[i][j] = max(temp.mat[i][j], mat[i][k] + a.mat[k][j]);
			}
		return temp;
	}
	inline void operator = (const matrix &b){
		for(int i = 0; i < sz; i++)
			for(int j = 0; j < sz; j++)
				mat[i][j] = b.mat[i][j];
	}
};
const int CHAR = 26;
const int N = 205;

int go[N][CHAR];
#define conv(ch) ch - 'a'
class AhoCora {
public:
	struct Node {
		int ch[CHAR];
		int link;
		int val;
	}nd[N];
	int Q[N]; int qs, qe;
	int size;
	int createNode() {
		int ret = size++;
		for (int i = 0; i<CHAR; i++) {
			nd[ret].ch[i] = -1;
		}
		nd[ret].val = 0;
		return ret;
	}
	void init() {
		size = 0;
		createNode();
	}
	template<typename A>
	void add(A str,const int &sz,const int &val) {
		int cur = 0;
		for (int i = 0; i<sz; i++) {
			int c = conv(str[i]);
			if (nd[cur].ch[c] == -1) {
				int nnd = createNode();
				nd[cur].ch[c] = nnd;
			}
			cur = nd[cur].ch[c];
		}
		nd[cur].val = (nd[cur].val + val) % md;
	}
	inline int next(int u, int c) {
		if(go[u][c])
			return go[u][c] - 1;
		if(nd[u].ch[c] != -1)
			return nd[u].ch[c];
		if(u == 0)
			return 0;
		go[u][c] = 1 + next(nd[u].link, c);
		return go[u][c] - 1;
	}
	void computelink() {
		qs = qe = 0;
		nd[0].link = 0;
		for (int x, i = 0; i<CHAR; i++) {
			if ((x = nd[0].ch[i]) >= 0) {
				Q[qe++] = x;
				nd[x].link = 0;
			}
		}
		while (qs<qe) {
			int u = Q[qs++];
			for (int v, i = 0; i<CHAR; i++) {
				if ((v = nd[u].ch[i]) >= 0) {
					Q[qe++] = v;
					int m = nd[u].link;
					int L = nd[v].link = next(m, i);
					nd[v].val = (nd[v].val + nd[L].val) % md;
				}
			}
		}
	}
}acc;
int a[N];
char s[N];
matrix matpow(matrix a,int k){
	if(k == 1)
		return a;
	matrix p = matpow(a,k/2);
	p = p * p;
	if(k & 1)
		p = p * a;
	return p;
}
signed main()
{
//	freopen("inp.txt","r",stdin);
	int n,l;
	acc.init();
	scanf("%lld %lld",&n,&l);
	for(int i = 1; i <= n; i++)
		scanf("%lld ",&a[i]);
	for(int i = 1; i <= n; i++){
		scanf("%s",s);
		acc.add(s,strlen(s),a[i]);
	}
	acc.computelink();
	sz = acc.size;
	matrix p;
	for(int i = 0; i < sz; i++){
		for(int j = 0; j < 26; j++){
			int k = acc.next(i,j);
			p.mat[i][k] = acc.nd[k].val;
		}
	}
	p = matpow(p,l);
	int maxi = - inf;
	for(int i = 0; i < sz; i++)
		maxi = max(maxi, p.mat[0][i]);
	printf("%lld",maxi);
	return 0;
}


#pragma warning(disable:4996)

#include <stdio.h>
#include <iostream>
#include <iomanip>
#include <vector>
#include <algorithm>
#include <string.h>
#include <stdlib.h>
#include <assert.h>
#include <cmath>
#include <map>
#include <set>
#include <queue>
#include <complex>
#include <iterator>
#include <random>
#include <time.h>
#include <tuple>
#include <functional>
#include <list>
#include <limits.h>
#define mp make_pair
#define ni(x) scanf("%d", &(x))
#define nii(x,y) scanf("%d%d",&(x),&(y))
#define mul(x,y) ((ll)(x)*(y)%mod)
#define mtp make_tuple
#define F(i,n) for(int i = 0; i < (n); i++)
#define FF(i,n) for(int i = 1; i <= (n); i++)

using namespace std;
typedef pair<int, int> pii;
typedef long long ll;
const int mod = 1000000007;
const int inf = 2012345678;
const ll infl = 9012345678901234567;
const double pi = 3.1415926535897932384626433832795;
//----------------------------------------------------------------------------//

const int CHAR = 26;
const int N = 220;

#define CONVERT(c) (c)-'a'
class AhoCora {
public:
	struct Node {
		int ch[CHAR];
		int link;
		vector<int> dic;
	}nd[N];
	int Q[N]; int qs, qe;
	int size;
	int createNode() {
		int ret = size++;
		for (int i = 0; i<CHAR; i++) {
			nd[ret].ch[i] = -1;
		}
		return ret;
	}
	void init() {
		size = 0;
		createNode();
	}
	template<typename A>
	void add(A str, int sz, int id) {
		int cur = 0;
		for (int i = 0; i<sz; i++) {
			int c = CONVERT(str[i]);
			if (nd[cur].ch[c] == -1) {
				int nnd = createNode();
				nd[cur].ch[c] = nnd;
			}
			cur = nd[cur].ch[c];
		}
		nd[cur].dic.push_back(id);
	}
	inline int next(int u, int c) {
		while (u&&nd[u].ch[c]<0)
			u = nd[u].link;
		if (nd[u].ch[c]<0)return 0;
		else return nd[u].ch[c];
	}
	void computelink() {
		qs = qe = 0;
		nd[0].link = 0;
		for (int x, i = 0; i<CHAR; i++) {
			if ((x = nd[0].ch[i]) >= 0) {
				Q[qe++] = x;
				nd[x].link = 0;
			}
		}
		while (qs<qe) {
			int u = Q[qs++];
			for (int v, i = 0; i<CHAR; i++) {
				if ((v = nd[u].ch[i]) >= 0) {
					Q[qe++] = v;
					int m = nd[u].link;
					int L = nd[v].link = next(m, i);
					nd[v].dic.insert(nd[v].dic.end(), nd[L].dic.begin(), nd[L].dic.end());
				}
			}
		}
	}
}acc;

char buf[200][220];
int a[200];

int ndn;
int go[N][26];
int sa[N];
int dp[2001][N];
int dpm[2000];

int main() {
#ifndef __GNUG__
	freopen("input.txt", "r", stdin);
#endif
	int n; ll l;
	ni(n); scanf("%I64d", &l);
	F(i, n) {
		ni(a[i]);
	}
	acc.init();
	F(i, n) {
		scanf("%s", buf[i]);
		acc.add(buf[i], strlen(buf[i]), i);
	}
	acc.computelink();
	int ndn = acc.size;
	F(i, ndn) {
		F(j, CHAR) {
			go[i][j] = acc.next(i, j);
		}
	}
	F(i, ndn) {
		for (auto &x : acc.nd[i].dic) sa[i] += a[x];
	}
	F(t, 2001)F(i, ndn)dp[t][i] = -1;
	dp[0][0] = 0;
	F(t, 2000) {
		F(i, ndn) {
			if (dp[t][i] == -1)continue;
			F(j, CHAR) {
				int ni = go[i][j];
				dp[t + 1][ni] = max(dp[t + 1][ni], dp[t][i] + sa[ni]);
			}
		}
	}
	F(i, 2001)dpm[i] = *max_element(dp[i], dp[i] + ndn);
	if (l <= 2000) {
		printf("%d\n", *max_element(dp[l], dp[l] + ndn));
		return 0;
	}
	int ii = -1;
	for (int i = 1999; i >= 1000; i--) {
		bool bad = false;
		int X = dpm[2000] - dpm[i];
		for (int j = 0; j < n - i; j++) {
			if (dpm[i + j] - dpm[2 * i - 2000 + j] != X) {
				bad = true; break;
			}
		}
		if (!bad) {
			ii = i; break;
		}
	}
	if (ii <= 0) {
		assert(0);
	}
	int T = 2000 - ii;
	int rem = (l-ii)%T;
	ll qq = (l - ii) / T;
	int X = dpm[2000] - dpm[ii];
	printf("%I64d\n", X*qq + dpm[ii + rem]);
}


#pragma warning(disable:4996)

#include <stdio.h>
#include <iostream>
#include <iomanip>
#include <vector>
#include <algorithm>
#include <string.h>
#include <stdlib.h>
#include <assert.h>
#include <cmath>
#include <map>
#include <set>
#include <queue>
#include <complex>
#include <iterator>
#include <random>
#include <time.h>
#include <tuple>
#include <functional>
#include <list>
#include <limits.h>
#define mp make_pair
#define ni(x) scanf("%d", &(x))
#define nii(x,y) scanf("%d%d",&(x),&(y))
#define mul(x,y) ((ll)(x)*(y)%mod)
#define mtp make_tuple
#define F(i,n) for(int i = 0; i < (n); i++)
#define FF(i,n) for(int i = 1; i <= (n); i++)

using namespace std;
typedef pair<int, int> pii;
typedef long long ll;
const int mod = 1000000007;
const int inf = 2012345678;
const ll infl = 9012345678901234567;
const double pi = 3.1415926535897932384626433832795;
//----------------------------------------------------------------------------//

const int CHAR = 26;
const int N = 220;

#define CONVERT(c) (c)-'a'
class AhoCora {
public:
	struct Node {
		int ch[CHAR];
		int link;
		vector<int> dic;
	}nd[N];
	int Q[N]; int qs, qe;
	int size;
	int createNode() {
		int ret = size++;
		for (int i = 0; i<CHAR; i++) {
			nd[ret].ch[i] = -1;
		}
		return ret;
	}
	void init() {
		size = 0;
		createNode();
	}
	template<typename A>
	void add(A str, int sz, int id) {
		int cur = 0;
		for (int i = 0; i<sz; i++) {
			int c = CONVERT(str[i]);
			if (nd[cur].ch[c] == -1) {
				int nnd = createNode();
				nd[cur].ch[c] = nnd;
			}
			cur = nd[cur].ch[c];
		}
		nd[cur].dic.push_back(id);
	}
	inline int next(int u, int c) {
		while (u&&nd[u].ch[c]<0)
			u = nd[u].link;
		if (nd[u].ch[c]<0)return 0;
		else return nd[u].ch[c];
	}
	void computelink() {
		qs = qe = 0;
		nd[0].link = 0;
		for (int x, i = 0; i<CHAR; i++) {
			if ((x = nd[0].ch[i]) >= 0) {
				Q[qe++] = x;
				nd[x].link = 0;
			}
		}
		while (qs<qe) {
			int u = Q[qs++];
			for (int v, i = 0; i<CHAR; i++) {
				if ((v = nd[u].ch[i]) >= 0) {
					Q[qe++] = v;
					int m = nd[u].link;
					int L = nd[v].link = next(m, i);
					nd[v].dic.insert(nd[v].dic.end(), nd[L].dic.begin(), nd[L].dic.end());
				}
			}
		}
	}
}acc;

char buf[200][220];
int a[200];

const int M =5000;

int ndn;
int go[N][26];
int sa[N];
int dp[M+1][N];
int dpm[M+1];

int main() {
#ifndef __GNUG__
	freopen("input.txt", "r", stdin);
#endif
	int n; ll l;
	ni(n); scanf("%I64d", &l);
	F(i, n) {
		ni(a[i]);
	}
	acc.init();
	F(i, n) {
		scanf("%s", buf[i]);
		acc.add(buf[i], strlen(buf[i]), i);
	}
	acc.computelink();
	int ndn = acc.size;
	F(i, ndn) {
		F(j, CHAR) {
			go[i][j] = acc.next(i, j);
		}
	}
	F(i, ndn) {
		for (auto &x : acc.nd[i].dic) sa[i] += a[x];
	}
	F(t, M+1)F(i, ndn)dp[t][i] = -1;
	dp[0][0] = 0;
	F(t, M) {
		F(i, ndn) {
			if (dp[t][i] == -1)continue;
			F(j, CHAR) {
				int ni = go[i][j];
				dp[t + 1][ni] = max(dp[t + 1][ni], dp[t][i] + sa[ni]);
			}
		}
	}
	F(i, M+1)dpm[i] = *max_element(dp[i], dp[i] + ndn);
	if (l <= M) {
		printf("%d\n", dpm[l]);
		return 0;
	}
	int ii = -1;
	for (int i = (M+1)/2; i < M; i++) {
		bool bad = false;
		int X = dpm[M] - dpm[i];
		for (int j = 0; j < n - i; j++) {
			if (dpm[i + j] - dpm[2 * i - M + j] != X) {
				bad = true; break;
			}
		}
		if (!bad) {
			ii = i; break;
		}
	}
	if (ii == -1) {
		assert(0);
	}
	int T = M - ii;
	int rem = (l-ii)%T;
	ll qq = (l - ii) / T;
	int X = dpm[M] - dpm[ii];
	printf("%I64d\n", X*qq + dpm[ii + rem]);
}


#pragma warning(disable:4996)

#include <stdio.h>
#include <iostream>
#include <iomanip>
#include <vector>
#include <algorithm>
#include <string.h>
#include <stdlib.h>
#include <assert.h>
#include <cmath>
#include <map>
#include <set>
#include <queue>
#include <complex>
#include <iterator>
#include <random>
#include <time.h>
#include <tuple>
#include <functional>
#include <list>
#include <limits.h>
#define mp make_pair
#define ni(x) scanf("%d", &(x))
#define nii(x,y) scanf("%d%d",&(x),&(y))
#define mul(x,y) ((ll)(x)*(y)%mod)
#define mtp make_tuple
#define F(i,n) for(int i = 0; i < (n); i++)
#define FF(i,n) for(int i = 1; i <= (n); i++)

using namespace std;
typedef pair<int, int> pii;
typedef long long ll;
const int mod = 1000000007;
const int inf = 2012345678;
const ll infl = 9012345678901234567;
const double pi = 3.1415926535897932384626433832795;
//----------------------------------------------------------------------------//

const int CHAR = 26;
const int N = 220;

#define CONVERT(c) (c)-'a'
class AhoCora {
public:
	struct Node {
		int ch[CHAR];
		int link;
		vector<int> dic;
	}nd[N];
	int Q[N]; int qs, qe;
	int size;
	int createNode() {
		int ret = size++;
		for (int i = 0; i<CHAR; i++) {
			nd[ret].ch[i] = -1;
		}
		return ret;
	}
	void init() {
		size = 0;
		createNode();
	}
	template<typename A>
	void add(A str, int sz, int id) {
		int cur = 0;
		for (int i = 0; i<sz; i++) {
			int c = CONVERT(str[i]);
			if (nd[cur].ch[c] == -1) {
				int nnd = createNode();
				nd[cur].ch[c] = nnd;
			}
			cur = nd[cur].ch[c];
		}
		nd[cur].dic.push_back(id);
	}
	inline int next(int u, int c) {
		while (u&&nd[u].ch[c]<0)
			u = nd[u].link;
		if (nd[u].ch[c]<0)return 0;
		else return nd[u].ch[c];
	}
	void computelink() {
		qs = qe = 0;
		nd[0].link = 0;
		for (int x, i = 0; i<CHAR; i++) {
			if ((x = nd[0].ch[i]) >= 0) {
				Q[qe++] = x;
				nd[x].link = 0;
			}
		}
		while (qs<qe) {
			int u = Q[qs++];
			for (int v, i = 0; i<CHAR; i++) {
				if ((v = nd[u].ch[i]) >= 0) {
					Q[qe++] = v;
					int m = nd[u].link;
					int L = nd[v].link = next(m, i);
					nd[v].dic.insert(nd[v].dic.end(), nd[L].dic.begin(), nd[L].dic.end());
				}
			}
		}
	}
}acc;

char buf[200][220];
int a[200];

int ndn;
int go[N][26];
int sa[N];
int dp[2001][N];
int dpm[2000];

int main() {
#ifndef __GNUG__
	freopen("input.txt", "r", stdin);
#endif
	int n; ll l;
	ni(n); scanf("%I64d", &l);
	F(i, n) {
		ni(a[i]);
	}
	acc.init();
	F(i, n) {
		scanf("%s", buf[i]);
		acc.add(buf[i], strlen(buf[i]), i);
	}
	acc.computelink();
	int ndn = acc.size;
	F(i, ndn) {
		F(j, CHAR) {
			go[i][j] = acc.next(i, j);
		}
	}
	F(i, ndn) {
		for (auto &x : acc.nd[i].dic) sa[i] += a[x];
	}
	F(t, 2001)F(i, ndn)dp[t][i] = -1;
	dp[0][0] = 0;
	F(t, 2000) {
		F(i, ndn) {
			if (dp[t][i] == -1)continue;
			F(j, CHAR) {
				int ni = go[i][j];
				dp[t + 1][ni] = max(dp[t + 1][ni], dp[t][i] + sa[ni]);
			}
		}
	}
	F(i, 2001)dpm[i] = *max_element(dp[i], dp[i] + ndn);
	if (l <= 2000) {
		printf("%d\n", dpm[l]);
		return 0;
	}
	int ii = -1;
	for (int i = 1000; i < 2000; i++) {
		bool bad = false;
		int X = dpm[2000] - dpm[i];
		for (int j = 0; j < n - i; j++) {
			if (dpm[i + j] - dpm[2 * i - 2000 + j] != X) {
				bad = true; break;
			}
		}
		if (!bad) {
			ii = i; break;
		}
	}
	if (ii == -1) {
		assert(0);
	}
	int T = 2000 - ii;
	int rem = (l-ii)%T;
	ll qq = (l - ii) / T;
	int X = dpm[2000] - dpm[ii];
	printf("%I64d\n", X*qq + dpm[ii + rem]);
}


#pragma warning(disable:4996)

#include <stdio.h>
#include <iostream>
#include <iomanip>
#include <vector>
#include <algorithm>
#include <string.h>
#include <stdlib.h>
#include <assert.h>
#include <cmath>
#include <map>
#include <set>
#include <queue>
#include <complex>
#include <iterator>
#include <random>
#include <time.h>
#include <tuple>
#include <functional>
#include <list>
#include <limits.h>
#define mp make_pair
#define ni(x) scanf("%d", &(x))
#define nii(x,y) scanf("%d%d",&(x),&(y))
#define mul(x,y) ((ll)(x)*(y)%mod)
#define mtp make_tuple
#define F(i,n) for(int i = 0; i < (n); i++)
#define FF(i,n) for(int i = 1; i <= (n); i++)

using namespace std;
typedef pair<int, int> pii;
typedef long long ll;
const int mod = 1000000007;
const int inf = 2012345678;
const ll infl = 9012345678901234567;
const double pi = 3.1415926535897932384626433832795;
//----------------------------------------------------------------------------//

const int CHAR = 26;
const int N = 220;

#define CONVERT(c) (c)-'a'
class AhoCora {
public:
	struct Node {
		int ch[CHAR];
		int link;
		vector<int> dic;
	}nd[N];
	int Q[N]; int qs, qe;
	int size;
	int createNode() {
		int ret = size++;
		for (int i = 0; i<CHAR; i++) {
			nd[ret].ch[i] = -1;
		}
		return ret;
	}
	void init() {
		size = 0;
		createNode();
	}
	template<typename A>
	void add(A str, int sz, int id) {
		int cur = 0;
		for (int i = 0; i<sz; i++) {
			int c = CONVERT(str[i]);
			if (nd[cur].ch[c] == -1) {
				int nnd = createNode();
				nd[cur].ch[c] = nnd;
			}
			cur = nd[cur].ch[c];
		}
		nd[cur].dic.push_back(id);
	}
	inline int next(int u, int c) {
		while (u&&nd[u].ch[c]<0)
			u = nd[u].link;
		if (nd[u].ch[c]<0)return 0;
		else return nd[u].ch[c];
	}
	void computelink() {
		qs = qe = 0;
		nd[0].link = 0;
		for (int x, i = 0; i<CHAR; i++) {
			if ((x = nd[0].ch[i]) >= 0) {
				Q[qe++] = x;
				nd[x].link = 0;
			}
		}
		while (qs<qe) {
			int u = Q[qs++];
			for (int v, i = 0; i<CHAR; i++) {
				if ((v = nd[u].ch[i]) >= 0) {
					Q[qe++] = v;
					int m = nd[u].link;
					int L = nd[v].link = next(m, i);
					nd[v].dic.insert(nd[v].dic.end(), nd[L].dic.begin(), nd[L].dic.end());
				}
			}
		}
	}
}acc;

char buf[200][220];
int a[200];

const int M =5000;

int ndn;
int go[N][26];
int sa[N];
int dp[M+1][N];
int dpm[M+1];

int main() {
#ifndef __GNUG__
	freopen("input.txt", "r", stdin);
#endif
	int n; ll l;
	ni(n); scanf("%I64d", &l);
	F(i, n) {
		ni(a[i]);
	}
	acc.init();
	F(i, n) {
		scanf("%s", buf[i]);
		acc.add(buf[i], strlen(buf[i]), i);
	}
	acc.computelink();
	int ndn = acc.size;
	F(i, ndn) {
		F(j, CHAR) {
			go[i][j] = acc.next(i, j);
		}
	}
	F(i, ndn) {
		for (auto &x : acc.nd[i].dic) sa[i] += a[x];
	}
	F(t, M+1)F(i, ndn)dp[t][i] = -1;
	dp[0][0] = 0;
	F(t, M) {
		F(i, ndn) {
			if (dp[t][i] == -1)continue;
			F(j, CHAR) {
				int ni = go[i][j];
				dp[t + 1][ni] = max(dp[t + 1][ni], dp[t][i] + sa[ni]);
			}
		}
	}
	F(i, M+1)dpm[i] = *max_element(dp[i], dp[i] + ndn);
	if (l <= M) {
		printf("%d\n", dpm[l]);
		return 0;
	}
	int ii = -1;
	for (int i = (M+1)/2; i < M; i++) {
		bool bad = false;
		int X = dpm[M] - dpm[i];
		for (int j = 0; j < M - i; j++) {
			if (dpm[i + j] - dpm[2 * i - M + j] != X) {
				bad = true; break;
			}
		}
		if (!bad) {
			ii = i; break;
		}
	}
	if (ii == -1) {
		assert(0);
	}
	int T = M - ii;
	int rem = (l-ii)%T;
	ll qq = (l - ii) / T;
	int X = dpm[M] - dpm[ii];
	printf("%I64d\n", X*qq + dpm[ii + rem]);
}


#include<bits/stdc++.h>
#define ll long long
#define maxn 205
#define pb push_back
#define clr(x,y) memset(x,y,sizeof(x))
#define rep(i,l,r) for (int i=l;i<=r;i++)
#define down(i,l,r) for (int i=l;i>=r;i--)
#define mm 100000
using namespace std;
const double eps=1e-9;
const ll inf=1e18;
const double pi=acos(-1.0);
ll read(){
	ll x=0,f=1; char ch=getchar();
	while (!isdigit(ch)){if (ch=='-') f=-1; ch=getchar();}
	while (isdigit(ch)){x=x*10+ch-'0'; ch=getchar();}
	return x*f;
}
char s[maxn];
int n,tot,l;
int c[maxn][26],fail[maxn];
ll a[maxn],d[maxn][maxn],ans[maxn][maxn],b[maxn][maxn],vis[maxn][maxn],f[maxn];
void mulans(){
	rep(i,1,tot) rep(j,1,tot) d[i][j]=-inf;
	rep(i,1,tot) rep(j,1,tot) rep(k,1,tot) d[i][j]=max(d[i][j],ans[i][k]+b[k][j]);
	rep(i,1,tot) rep(j,1,tot) ans[i][j]=d[i][j];
}
void mulb(){
	rep(i,1,tot) rep(j,1,tot) d[i][j]=-inf;
	rep(i,1,tot) rep(j,1,tot) rep(k,1,tot) d[i][j]=max(d[i][j],b[i][k]+b[k][j]);
	rep(i,1,tot) rep(j,1,tot) b[i][j]=d[i][j];
}
int main(){
	n=read(); l=read();
	rep(i,1,n) a[i]=read();
	tot=1;
	rep(i,1,n) {
		scanf("%s",s+1);
		int len=strlen(s+1),now=1;
		rep(j,1,len) {
			int x=s[j]-'a';
			if (!c[now][x]) c[now][x]=++tot;
			now=c[now][x];
		}
		f[now]+=a[i];
	}
	queue<int> q;
	rep(i,0,25) if (c[1][i]) {
		q.push(c[1][i]); fail[c[1][i]]=1;
	} else c[1][i]=1;
	while (!q.empty()){
		int u=q.front(),k=fail[u]; q.pop();
		f[u]+=f[k];
		rep(j,0,25) if (c[u][j]){
			q.push(c[u][j]); fail[c[u][j]]=c[k][j];
		}else c[u][j]=c[k][j];
	}
//	rep(i,1,tot) printf("%lld ",fail[i]); puts("");
//	rep(i,1,tot) printf("%lld ",f[i]);
	rep(i,1,tot) rep(j,1,tot) b[i][j]=-inf;
	rep(i,1,tot) rep(j,0,25) if (c[i][j]) {
		if (!vis[i][c[i][j]]) vis[i][c[i][j]]=1,b[i][c[i][j]]=0;
		b[i][c[i][j]]+=f[c[i][j]];
	}
	rep(i,1,tot) rep(j,1,tot) ans[i][j]=b[i][j];
	l--;
	while (l){
		if (l&1) mulans();
		mulb();
		l/=2;
	}
	ll mx=0;
	rep(i,1,tot) mx=max(mx,ans[1][i]);
	printf("%lld\n",mx);
	return 0;

}


#include<bits/stdc++.h>
#define ll long long
#define maxn 205
#define pb push_back
#define clr(x,y) memset(x,y,sizeof(x))
#define rep(i,l,r) for (int i=l;i<=r;i++)
#define down(i,l,r) for (int i=l;i>=r;i--)
#define mm 100000
using namespace std;
const double eps=1e-9;
const ll inf=1e18;
const double pi=acos(-1.0);
ll read(){
	ll x=0,f=1; char ch=getchar();
	while (!isdigit(ch)){if (ch=='-') f=-1; ch=getchar();}
	while (isdigit(ch)){x=x*10+ch-'0'; ch=getchar();}
	return x*f;
}
char s[maxn];
int n,tot,l;
int c[maxn][26],fail[maxn];
ll a[maxn],d[maxn][maxn],ans[maxn][maxn],b[maxn][maxn],f[maxn];
void mulans(){
	rep(i,1,tot) rep(j,1,tot) d[i][j]=-inf;
	rep(i,1,tot) rep(j,1,tot) rep(k,1,tot) d[i][j]=max(d[i][j],ans[i][k]+b[k][j]);
	rep(i,1,tot) rep(j,1,tot) ans[i][j]=d[i][j];
}
void mulb(){
	rep(i,1,tot) rep(j,1,tot) d[i][j]=-inf;
	rep(i,1,tot) rep(j,1,tot) rep(k,1,tot) d[i][j]=max(d[i][j],b[i][k]+b[k][j]);
	rep(i,1,tot) rep(j,1,tot) b[i][j]=d[i][j];
}
int main(){
	n=read(); l=read();
	rep(i,1,n) a[i]=read();
	tot=1;
	rep(i,1,n) {
		scanf("%s",s+1);
		int len=strlen(s+1),now=1;
		rep(j,1,len) {
			int x=s[j]-'a';
			if (!c[now][x]) c[now][x]=++tot;
			now=c[now][x];
		}
		f[now]+=a[i];
	}
	queue<int> q;
	rep(i,0,25) if (c[1][i]) {
		q.push(c[1][i]); fail[c[1][i]]=1;
	} else c[1][i]=1;
	while (!q.empty()){
		int u=q.front(),k=fail[u]; q.pop();
		f[u]+=f[k];
		rep(j,0,25) if (c[u][j]){
			q.push(c[u][j]); fail[c[u][j]]=c[k][j];
		}else c[u][j]=c[k][j];
	}
//	rep(i,1,tot) printf("%lld ",fail[i]); puts("");
//	rep(i,1,tot) printf("%lld ",f[i]);
	rep(i,1,tot) rep(j,1,tot) b[i][j]=-inf;
	rep(i,1,tot) rep(j,0,25) if (c[i][j]) b[i][c[i][j]]=f[c[i][j]];
	rep(i,1,tot) rep(j,1,tot) ans[i][j]=b[i][j];
	l--;
	while (l){
		if (l&1) mulans();
		mulb();
		l/=2;
	}
	ll mx=0;
	rep(i,1,tot) mx=max(mx,ans[1][i]);
	printf("%lld\n",mx);
	return 0;

}


#include<bits/stdc++.h>
#define ll long long
#define maxn 205
#define pb push_back
#define clr(x,y) memset(x,y,sizeof(x))
#define rep(i,l,r) for (int i=l;i<=r;i++)
#define down(i,l,r) for (int i=l;i>=r;i--)
#define mm 100000
using namespace std;
const double eps=1e-9;
const ll inf=1e18;
const double pi=acos(-1.0);
ll read(){
	ll x=0,f=1; char ch=getchar();
	while (!isdigit(ch)){if (ch=='-') f=-1; ch=getchar();}
	while (isdigit(ch)){x=x*10+ch-'0'; ch=getchar();}
	return x*f;
}
char s[maxn];
int n,tot;
ll l;
int c[maxn][26],fail[maxn];
ll a[maxn],d[maxn][maxn],ans[maxn][maxn],b[maxn][maxn],vis[maxn][maxn],f[maxn];
void mulans(){
	rep(i,1,tot) rep(j,1,tot) d[i][j]=-inf;
	rep(i,1,tot) rep(j,1,tot) rep(k,1,tot) d[i][j]=max(d[i][j],ans[i][k]+b[k][j]);
	rep(i,1,tot) rep(j,1,tot) ans[i][j]=d[i][j];
}
void mulb(){
	rep(i,1,tot) rep(j,1,tot) d[i][j]=-inf;
	rep(i,1,tot) rep(j,1,tot) rep(k,1,tot) d[i][j]=max(d[i][j],b[i][k]+b[k][j]);
	rep(i,1,tot) rep(j,1,tot) b[i][j]=d[i][j];
}
int main(){
	n=read(); l=read();
	rep(i,1,n) a[i]=read();
	tot=1;
	rep(i,1,n) {
		scanf("%s",s+1);
		int len=strlen(s+1),now=1;
		rep(j,1,len) {
			int x=s[j]-'a';
			if (!c[now][x]) c[now][x]=++tot;
			now=c[now][x];
		}
		f[now]+=a[i];
	}
	queue<int> q;
	rep(i,0,25) if (c[1][i]) {
		q.push(c[1][i]); fail[c[1][i]]=1;
	} else c[1][i]=1;
	while (!q.empty()){
		int u=q.front(),k=fail[u]; q.pop();
		f[u]+=f[k];
		rep(j,0,25) if (c[u][j]){
			q.push(c[u][j]); fail[c[u][j]]=c[k][j];
		}else c[u][j]=c[k][j];
	}
//	rep(i,1,tot) printf("%lld ",fail[i]); puts("");
//	rep(i,1,tot) printf("%lld ",f[i]);
	rep(i,1,tot) rep(j,1,tot) b[i][j]=-inf;
	rep(i,1,tot) rep(j,0,25) if (c[i][j]) {
		if (!vis[i][c[i][j]]) vis[i][c[i][j]]=1,b[i][c[i][j]]=0;
		b[i][c[i][j]]+=f[c[i][j]];
	}
	rep(i,1,tot) rep(j,1,tot) ans[i][j]=b[i][j];
	l--;
	while (l){
		if (l&1) mulans();
		mulb();
		l/=2;
	}
	ll mx=0;
	rep(i,1,tot) mx=max(mx,ans[1][i]);
	printf("%lld\n",mx);
	return 0;

}


#include<bits/stdc++.h>
#define ll long long
#define maxn 205
#define pb push_back
#define clr(x,y) memset(x,y,sizeof(x))
#define rep(i,l,r) for (int i=l;i<=r;i++)
#define down(i,l,r) for (int i=l;i>=r;i--)
#define mm 100000
using namespace std;
const double eps=1e-9;
const ll inf=1e18;
const double pi=acos(-1.0);
ll read(){
	ll x=0,f=1; char ch=getchar();
	while (!isdigit(ch)){if (ch=='-') f=-1; ch=getchar();}
	while (isdigit(ch)){x=x*10+ch-'0'; ch=getchar();}
	return x*f;
}
char s[maxn];
int n,tot;
ll l;
int c[maxn][26],fail[maxn];
ll a[maxn],d[maxn][maxn],ans[maxn][maxn],b[maxn][maxn],f[maxn];
void mulans(){
	rep(i,1,tot) rep(j,1,tot) d[i][j]=-inf;
	rep(i,1,tot) rep(j,1,tot) rep(k,1,tot) d[i][j]=max(d[i][j],ans[i][k]+b[k][j]);
	rep(i,1,tot) rep(j,1,tot) ans[i][j]=d[i][j];
}
void mulb(){
	rep(i,1,tot) rep(j,1,tot) d[i][j]=-inf;
	rep(i,1,tot) rep(j,1,tot) rep(k,1,tot) d[i][j]=max(d[i][j],b[i][k]+b[k][j]);
	rep(i,1,tot) rep(j,1,tot) b[i][j]=d[i][j];
}
int main(){
	n=read(); l=read();
	rep(i,1,n) a[i]=read();
	tot=1;
	rep(i,1,n) {
		scanf("%s",s+1);
		int len=strlen(s+1),now=1;
		rep(j,1,len) {
			int x=s[j]-'a';
			if (!c[now][x]) c[now][x]=++tot;
			now=c[now][x];
		}
		f[now]+=a[i];
	}
	queue<int> q;
	rep(i,0,25) if (c[1][i]) {
		q.push(c[1][i]); fail[c[1][i]]=1;
	} else c[1][i]=1;
	while (!q.empty()){
		int u=q.front(),k=fail[u]; q.pop();
		f[u]+=f[k];
		rep(j,0,25) if (c[u][j]){
			q.push(c[u][j]); fail[c[u][j]]=c[k][j];
		}else c[u][j]=c[k][j];
	}
//	rep(i,1,tot) printf("%lld ",fail[i]); puts("");
//	rep(i,1,tot) printf("%lld ",f[i]);
	rep(i,1,tot) rep(j,1,tot) b[i][j]=-inf;
	rep(i,1,tot) rep(j,0,25) if (c[i][j]) b[i][c[i][j]]=f[c[i][j]];
	rep(i,1,tot) rep(j,1,tot) ans[i][j]=b[i][j];
	l--;
	while (l){
		if (l&1) mulans();
		mulb();
		l/=2;
	}
	ll mx=0;
	rep(i,1,tot) mx=max(mx,ans[1][i]);
	printf("%lld\n",mx);
	return 0;

}


#include <iostream>
#include <iomanip>
#include <fstream>
#include <sstream>
#include <cstdio>
#include <cstdlib>
#include <ctime>
#include <cmath>
#include <cstring>
#include <string>
#include <algorithm>
#include <vector>
#include <set>
#include <queue>
#include <map>
#include <stack>
#include <complex>
#include <random>
using namespace std;
typedef long long ll;
typedef unsigned long long ull;

const int MAXN = 205;
int N, M;
ll L;
ll A[MAXN];
string S[MAXN];

map<string, ll> score;
map<string, int> prefix;
string idx[MAXN];
ll P[MAXN];

ll mat[MAXN][MAXN];
ll temp[MAXN][MAXN];
ll dp[MAXN];
ll temp2[MAXN];

void mult()
{
    memcpy(temp, mat, sizeof(temp));
    for (int i = 1; i <= M; i++)
        for (int j = 1; j <= M; j++)
            for (int k = 1; k <= M; k++)
                mat[i][j] = max(mat[i][j], temp[i][k] + temp[k][j]);
}

void mult2()
{
    memcpy(temp2, dp, sizeof(dp));
    for (int i = 1; i <= M; i++)
        for (int j = 1; j <= M; j++)
            dp[i] = max(dp[i], mat[i][j] + temp2[j]);
}

int longest(string s)
{
    for (int i = 0; i < s.size(); i++)
    {
        string t = s.substr(i, s.size() - i);
        if (prefix.count(t))
            return prefix[t];
    }
    return prefix[""];
}

int main()
{
    ios::sync_with_stdio(0);

    cin >> N >> L;
    for (int i = 1; i <= N; i++)
        cin >> A[i];
    for (int i = 1; i <= N; i++)
    {
        cin >> S[i];
        for (int j = 1; j <= S[i].size(); j++)
            prefix[S[i].substr(0, j)] = 1;
        score[S[i]] += A[i];
    }

    M = 0;
    prefix[""] = 1;
    for (map<string, int>::iterator it = prefix.begin(); it != prefix.end(); it++)
        it->second = ++M;

    for (map<string, int>::iterator it = prefix.begin(); it != prefix.end(); it++)
    {
        string s = it->first;
        int i = it->second;
        idx[i] = s;
        for (int j = 0; j < s.size(); j++)
        {
            string cur = s.substr(j, s.size() - j);
            if (score.count(cur))
                P[i] += score[cur];
        }
    }

    for (int i = 1; i <= M; i++)
        for (int j = 1; j <= M; j++)
            mat[i][j] = -(1LL<<60);

    for (int i = 1; i <= M; i++)
        for (char c = 'a'; c <= 'z'; c++)
        {
            string s = idx[i];
            s += c;
            int j = longest(s);
            mat[j][i] = P[j];
        }

    for (int i = 2; i <= M; i++)
        dp[i] = -(1LL<<60);

    for (; L > 0; L >>= 1)
    {
        if (L & 1)
            mult2();
        mult();
    }

    ll best = 0;
    for (int i = 1; i <= M; i++)
        best = max(best, dp[i]);

    cout << best << "\n";

    return 0;
}


#include <iostream>
#include <iomanip>
#include <fstream>
#include <sstream>
#include <cstdio>
#include <cstdlib>
#include <ctime>
#include <cmath>
#include <cstring>
#include <string>
#include <algorithm>
#include <vector>
#include <set>
#include <queue>
#include <map>
#include <stack>
#include <complex>
#include <random>
using namespace std;
typedef long long ll;
typedef unsigned long long ull;

const int MAXN = 205;
int N, M;
ll L;
ll A[MAXN];
string S[MAXN];

map<string, ll> score;
map<string, int> prefix;
string idx[MAXN];
ll P[MAXN];

ll mat[MAXN][MAXN];
ll temp[MAXN][MAXN];
ll dp[MAXN];
ll temp2[MAXN];

void mult()
{
    memcpy(temp, mat, sizeof(temp));
    for (int i = 1; i <= M; i++)
        for (int j = 1; j <= M; j++)
            for (int k = 1; k <= M; k++)
                mat[i][j] = max(mat[i][j], temp[i][k] + temp[k][j]);
}

void mult2()
{
    memcpy(temp2, dp, sizeof(dp));
    for (int i = 1; i <= M; i++)
        for (int j = 1; j <= M; j++)
            dp[i] = max(dp[i], mat[i][j] + temp2[j]);
}

int longest(string s)
{
    for (int i = 0; i < s.size(); i++)
    {
        string t = s.substr(i, s.size() - i);
        if (prefix.count(t))
            return prefix[t];
    }
    return prefix[""];
}

int main()
{
    ios::sync_with_stdio(0);

    cin >> N >> L;
    for (int i = 1; i <= N; i++)
        cin >> A[i];
    for (int i = 1; i <= N; i++)
    {
        cin >> S[i];
        for (int j = 1; j <= S[i].size(); j++)
            prefix[S[i].substr(0, j)] = 1;
        score[S[i]] += A[i];
    }

    M = 0;
    prefix[""] = 1;
    for (map<string, int>::iterator it = prefix.begin(); it != prefix.end(); it++)
        it->second = ++M;

    //cout << M << "\n";

    for (map<string, int>::iterator it = prefix.begin(); it != prefix.end(); it++)
    {
        string s = it->first;
        int i = it->second;
        idx[i] = s;
        for (int j = 0; j < s.size(); j++)
        {
            string cur = s.substr(j, s.size() - j);
            if (score.count(cur))
                P[i] += score[cur];
        }
    }

    /*for (int i = 1; i <= M; i++)
        cout << idx[i] << " " << P[i] << "\n";*/

    for (int i = 1; i <= M; i++)
        for (int j = 1; j <= M; j++)
            mat[i][j] = -(1LL<<60);

    for (int i = 1; i <= M; i++)
        for (char c = 'a'; c <= 'z'; c++)
        {
            string s = idx[i];
            s += c;
            int j = longest(s);
            mat[j][i] = P[j];
        }

    /*for (int i = 1; i <= M; i++)
    {
        for (int j = 1; j <= M; j++)
            cout << mat[i][j] << " ";
        cout << "\n";
    }
    cout << "\n";*/

    memset(dp, -1, sizeof(dp));
    dp[1] = 0;
    for (int i = 1; i < MAXN && L > 0; i++, L--)
    {
        memcpy(temp2, dp, sizeof(dp));
        for (int j = 1; j <= M; j++)
            if (idx[j].size() <= i)
                for (int k = 1; k <= M; k++)
                    if (temp2[k] != -1 && mat[j][k] != -(1LL<<60))
                        dp[j] = max(dp[j], mat[j][k] + temp2[k]);

        /*for (int j = 1; j <= M; j++)
            cout << dp[j] << " ";
        cout << "\n";*/
    }

    for (; L > 0; L >>= 1)
    {
        if (L & 1)
            mult2();
        mult();
        /*for (int i = 1; i <= M; i++)
        {
            for (int j = 1; j <= M; j++)
                cout << mat[i][j] << " ";
            cout << "\n";
        }
        cout << "\n";*/
    }

    ll best = 0;
    for (int i = 1; i <= M; i++)
        best = max(best, dp[i]);

    cout << best << "\n";

    return 0;
}


#include <iostream>
#include <iomanip>
#include <fstream>
#include <sstream>
#include <cstdio>
#include <cstdlib>
#include <ctime>
#include <cmath>
#include <cstring>
#include <string>
#include <algorithm>
#include <vector>
#include <set>
#include <queue>
#include <map>
#include <stack>
#include <complex>
#include <random>
using namespace std;
typedef long long ll;
typedef unsigned long long ull;

const int MAXN = 205;
const int MOD = 1000000007;
ll HASH[26];
ll power[MAXN];
int N, M;
ll L;
ll A[MAXN];
string S[MAXN];

ll pre[MAXN][MAXN];
map<ll, int> score;
map<ll, int> prefix;
int idx[MAXN];
int len[MAXN];
ll P[MAXN];

ll mat[MAXN][MAXN];
ll temp[MAXN][MAXN];
ll dp[MAXN];
ll temp2[MAXN];

void mult()
{
    memcpy(temp, mat, sizeof(temp));
    for (int i = 1; i <= M; i++)
        for (int j = 1; j <= M; j++)
            for (int k = 1; k <= M; k++)
                mat[i][j] = max(mat[i][j], temp[i][k] + temp[k][j]);
}

void mult2()
{
    memcpy(temp2, dp, sizeof(dp));
    for (int i = 1; i <= M; i++)
        for (int j = 1; j <= M; j++)
            dp[i] = max(dp[i], mat[i][j] + temp2[j]);
}

int main()
{
    ios::sync_with_stdio(0);

    for (int i = 0; i < 26; i++)
        HASH[i] = ((ll)rand()*MAXN + rand()) % MOD;
    power[0] = 1;
    for (int i = 1; i < MAXN; i++)
        power[i] = 2*power[i - 1] % MOD;

    cin >> N >> L;
    for (int i = 1; i <= N; i++)
        cin >> A[i];
    for (int i = 1; i <= N; i++)
    {
        cin >> S[i];
        for (int j = 1; j <= S[i].size(); j++)
        {
            pre[i][j] = (pre[i][j-1] + HASH[S[i][j-1]-'a']*power[j] % MOD) % MOD;
            prefix[pre[i][j]] = 1;
        }
        score[pre[i][S[i].size()]] = A[i];
    }

    M = 0;
    prefix[0] = 1;
    for (map<ll, int>::iterator it = prefix.begin(); it != prefix.end(); it++)
        it->second = ++M;

    for (int i = 1; i <= N; i++)
        for (int j = 1; j <= S[i].size(); j++)
            for (int k = 1; k <= j; k++)
            {
                ll cur = (pre[i][j] - pre[i][k-1]) % MOD;
                if (cur < 0)
                    cur += MOD;

                int x = prefix[pre[i][j]];
                idx[x] = pre[i][j];
                len[x] = j;

                if (score.count(cur))
                    P[x] += score[cur];
            }

    for (int i = 1; i <= M; i++)
        for (int j = 1; j <= M; j++)
            for (int c = 0; c < 26; c++)
                if ((idx[i] + HASH[c]*power[len[i] + 1] % MOD) % MOD == idx[j])
                    mat[j][i] = P[j];

    for (; L > 1; L >>= 1)
    {
        if (L & 1)
            mult2();
        else
            mult();
    }

    mult2();

    ll best = 0;
    for (int i = 1; i <= M; i++)
        best = max(best, dp[i]);

    cout << best << "\n";

    return 0;
}


#include <iostream>
#include <iomanip>
#include <fstream>
#include <sstream>
#include <cstdio>
#include <cstdlib>
#include <ctime>
#include <cmath>
#include <cstring>
#include <string>
#include <algorithm>
#include <vector>
#include <set>
#include <queue>
#include <map>
#include <stack>
#include <complex>
#include <random>
using namespace std;
typedef long long ll;
typedef unsigned long long ull;

const int MAXN = 205;
int N, M;
ll L;
ll A[MAXN];
string S[MAXN];

map<string, int> score;
map<string, int> prefix;
string idx[MAXN];
ll P[MAXN];

ll mat[MAXN][MAXN];
ll temp[MAXN][MAXN];
ll dp[MAXN];
ll temp2[MAXN];

void mult()
{
    memcpy(temp, mat, sizeof(temp));
    for (int i = 1; i <= M; i++)
        for (int j = 1; j <= M; j++)
            for (int k = 1; k <= M; k++)
                mat[i][j] = max(mat[i][j], temp[i][k] + temp[k][j]);
}

void mult2()
{
    memcpy(temp2, dp, sizeof(dp));
    for (int i = 1; i <= M; i++)
        for (int j = 1; j <= M; j++)
            dp[i] = max(dp[i], mat[i][j] + temp2[j]);
}

int longest(string s)
{
    for (int i = 0; i < s.size(); i++)
    {
        string t = s.substr(i, s.size() - i);
        if (prefix.count(t))
            return prefix[t];
    }
    return prefix[""];
}

int main()
{
    ios::sync_with_stdio(0);

    cin >> N >> L;
    for (int i = 1; i <= N; i++)
        cin >> A[i];
    for (int i = 1; i <= N; i++)
    {
        cin >> S[i];
        for (int j = 1; j <= S[i].size(); j++)
            prefix[S[i].substr(0, j)] = 1;
        score[S[i]] = A[i];
    }

    M = 0;
    prefix[""] = 1;
    for (map<string, int>::iterator it = prefix.begin(); it != prefix.end(); it++)
        it->second = ++M;

    //cout << M << "\n";

    for (map<string, int>::iterator it = prefix.begin(); it != prefix.end(); it++)
    {
        string s = it->first;
        int i = it->second;
        idx[i] = s;
        for (int j = 0; j < s.size(); j++)
        {
            string cur = s.substr(j, s.size() - j);
            if (score.count(cur))
                P[i] += score[cur];
        }
    }

    /*for (int i = 1; i <= M; i++)
        cout << idx[i] << " " << P[i] << "\n";*/

    for (int i = 1; i <= M; i++)
        for (int j = 1; j <= M; j++)
            mat[i][j] = -(1LL<<60);

    for (int i = 1; i <= M; i++)
        for (char c = 'a'; c <= 'z'; c++)
        {
            string s = idx[i];
            s += c;
            int j = longest(s);
            mat[j][i] = P[j];
        }

    /*for (int i = 1; i <= M; i++)
    {
        for (int j = 1; j <= M; j++)
            cout << mat[i][j] << " ";
        cout << "\n";
    }
    cout << "\n";*/

    memset(dp, -1, sizeof(dp));
    dp[1] = 0;
    for (int i = 1; i < MAXN && L > 0; i++, L--)
    {
        memcpy(temp2, dp, sizeof(dp));
        for (int j = 1; j <= M; j++)
            if (idx[j].size() <= i)
                for (int k = 1; k <= M; k++)
                    if (temp2[k] != -1)
                        dp[j] = max(dp[j], mat[j][k] + temp2[k]);

        /*for (int j = 1; j <= M; j++)
            cout << dp[j] << " ";
        cout << "\n";*/
    }

    for (; L > 0; L >>= 1)
    {
        if (L & 1)
            mult2();
        mult();
        /*for (int i = 1; i <= M; i++)
        {
            for (int j = 1; j <= M; j++)
                cout << mat[i][j] << " ";
            cout << "\n";
        }
        cout << "\n";*/
    }

    ll best = 0;
    for (int i = 1; i <= M; i++)
        best = max(best, dp[i]);

    cout << best << "\n";

    return 0;
}


#include <bits/stdc++.h>

using namespace std;

#define endl '\n'

typedef long long int64;
typedef pair<int,int> pii;
typedef vector<int> vi;

const int64 oo = 0x3f3f3f3f3f3f3f3f;
const double eps = 1e-9;
const int maxn = 100010;

struct matrix{
    int n;
    int64 m[210][210];

    matrix(int n) : n(n){
        for (int i = 0; i < n; ++i)
            for (int j = 0; j < n; ++j)
                m[i][j] = -oo;
    }

    matrix operator *(const matrix &M) const{
        matrix a(n);

        for (int i = 0; i < n; ++i)
            for (int j = 0; j < n; ++j)
                for (int k = 0; k < n; ++k)
                    if (m[i][k] >= 0 && M.m[k][j] >= 0)
                        a.m[i][j] = max(a.m[i][j], m[i][k] + M.m[k][j]);

        return a;
    }

    void print() const{
        for (int i = 0; i < n; ++i)
            for (int j = 0; j < n; ++j)
                cout << (m[i][j] < 0 ? -1 : m[i][j]) << " \n"[j + 1 == n];
        cout << endl;
    }
};

matrix identity(int n)
{
    matrix I(n);

    for (int i = 0; i < n; ++i)
        I.m[i][i] = 0;

    return I;
}

matrix fpow(matrix m, int64 n)
{
    matrix a = identity(m.n);

    for (int i = 0; i < 63; ++i){
        if (n >> i & 1) a = a * m;
        m = m * m;
    }

    return a;
}

const int alpha = 26;

int main()
{
    ios_base::sync_with_stdio(0);
    cin.tie(0);

    int n;
    cin >> n;

    int64 l;
    cin >> l;

    vector<int64> a(n);
    for (int64 &x : a)
        cin >> x;

    vector<vector<int>> go(2, vector<int>(alpha));
    vector<int> fail(2);
    vector<int64> cost(2, 0);

    for (int i = 0; i < n; ++i)
    {
        string s;
        cin >> s;

        int st = 1;

        for (char c : s)
        {
            if (!go[st][c - 'a'])
            {
                go[st][c - 'a'] = go.size();
                go.emplace_back(vector<int>(alpha));
                cost.push_back(0);
                fail.push_back(0);
            }

            st = go[st][c - 'a'];
        }

        cost[st] += a[i];
    }

    queue<int> q;

    for (int c = 0; c < alpha; ++c)
        if (!go[1][c]) go[1][c] = 1;
        else fail[go[1][c]] = 1, q.push(go[1][c]);

    for (; !q.empty(); q.pop())
    {
        int st = q.front(), f = fail[st];

        for (int c = 0; c < 26; ++c)
            if (!go[st][c]) go[st][c] = go[f][c];
            else
            {
                q.push(go[st][c]);
                fail[go[st][c]] = go[f][c];
                cost[go[st][c]] += cost[go[f][c]];
            }
    }

    matrix A(go.size());

    for (int st = 1; st < go.size(); ++st)
        for (int c = 0; c < alpha; ++c)
            A.m[st][go[st][c]] = cost[go[st][c]];

    A = fpow(A, l);

    int64 best = -oo;

    for (int i = 0; i < go.size(); ++i)
        best = max(best, A.m[1][i]);

    cout << best << endl;

    return 0;
}


#include <bits/stdc++.h>

using namespace std;

// #define endl '\n'

typedef long long int64;
typedef pair<int,int> pii;
typedef vector<int> vi;

const int64 oo = 0x3f3f3f3f;
const double eps = 1e-9;
const int maxn = 100010;

struct matrix{
    int n;
    int64 m[210][210];

    matrix(int n) : n(n){
        for (int i = 0; i < n; ++i)
            for (int j = 0; j < n; ++j)
                m[i][j] = -oo;
    }

    matrix operator *(const matrix &M) const{
        matrix a(n);

        for (int i = 0; i < n; ++i)
            for (int j = 0; j < n; ++j)
                for (int k = 0; k < n; ++k)
                    a.m[i][j] = max(a.m[i][j], m[i][k] + M.m[k][j]);

        return a;
    }

    void print() const{
        for (int i = 0; i < n; ++i)
            for (int j = 0; j < n; ++j)
                cout << (m[i][j] < 0 ? -1 : m[i][j]) << " \n"[j + 1 == n];
        cout << endl;
    }
};

matrix identity(int n)
{
    matrix I(n);

    for (int i = 0; i < n; ++i)
        I.m[i][i] = 0;

    return I;
}

matrix fpow(matrix m, int64 n)
{
    matrix a = identity(m.n);

    for (int i = 0; i < 63; ++i){
        if (n >> i & 1) a = a * m;
        m = m * m;
    }

    return a;
}

const int alpha = 26;

int main()
{
    ios_base::sync_with_stdio(0);
    cin.tie(0);

    int n;
    cin >> n;

    int64 l;
    cin >> l;

    vector<int64> a(n);
    for (int64 &x : a)
        cin >> x;

    vector<vector<int>> go(2, vector<int>(alpha));
    vector<int> fail(2);
    vector<int64> cost(2, 0);

    for (int i = 0; i < n; ++i)
    {
        string s;
        cin >> s;

        int st = 1;

        for (char c : s)
        {
            if (!go[st][c - 'a'])
            {
                go[st][c - 'a'] = go.size();
                go.emplace_back(vector<int>(alpha));
                cost.push_back(0);
                fail.push_back(0);
            }

            st = go[st][c - 'a'];
        }

        cost[st] = a[i];
    }

    queue<int> q;

    for (int c = 0; c < alpha; ++c)
        if (!go[1][c]) go[1][c] = 1;
        else fail[go[1][c]] = 1, q.push(go[1][c]);

    for (; !q.empty(); q.pop())
    {
        int st = q.front(), f = fail[st];

        for (int c = 0; c < 26; ++c)
            if (!go[st][c]) go[st][c] = go[f][c];
            else
            {
                q.push(go[st][c]);
                fail[go[st][c]] = go[f][c];
                cost[go[st][c]] += cost[go[f][c]];
            }
    }

    matrix A(go.size());

    for (int st = 1; st < go.size(); ++st)
        for (int c = 0; c < alpha; ++c)
            A.m[st][go[st][c]] = cost[go[st][c]];

    A = fpow(A, l);

    int64 best = -oo;

    for (int i = 0; i < go.size(); ++i)
        best = max(best, A.m[1][i]);

    cout << best << endl;

    return 0;
}


#include <bits/stdc++.h>

using namespace std;

#define endl '\n'

typedef long long int64;
typedef pair<int,int> pii;
typedef vector<int> vi;

const int64 oo = 0x3f3f3f3f3f3f3f3f;
const double eps = 1e-9;
const int maxn = 100010;

struct matrix{
    int n;
    int64 m[210][210];

    matrix(int n) : n(n){
        for (int i = 0; i < n; ++i)
            for (int j = 0; j < n; ++j)
                m[i][j] = -oo;
    }

    matrix operator *(const matrix &M) const{
        matrix a(n);

        for (int i = 0; i < n; ++i)
            for (int j = 0; j < n; ++j)
                for (int k = 0; k < n; ++k)
                    a.m[i][j] = max(a.m[i][j], m[i][k] + M.m[k][j]);

        return a;
    }

    void print() const{
        for (int i = 0; i < n; ++i)
            for (int j = 0; j < n; ++j)
                cout << (m[i][j] < 0 ? -1 : m[i][j]) << " \n"[j + 1 == n];
        cout << endl;
    }
};

matrix identity(int n)
{
    matrix I(n);

    for (int i = 0; i < n; ++i)
        I.m[i][i] = 0;

    return I;
}

matrix fpow(matrix m, int64 n)
{
    matrix a = identity(m.n);

    for (int i = 0; i < 63; ++i){
        if (n >> i & 1) a = a * m;
        m = m * m;
    }

    return a;
}

const int alpha = 26;

int main()
{
    ios_base::sync_with_stdio(0);
    cin.tie(0);

    int n;
    cin >> n;

    int64 l;
    cin >> l;

    vector<int64> a(n);
    for (int64 &x : a)
        cin >> x;

    vector<vector<int>> go(2, vector<int>(alpha));
    vector<int> fail(2);
    vector<int64> cost(2, 0);

    for (int i = 0; i < n; ++i)
    {
        string s;
        cin >> s;

        int st = 1;

        for (char c : s)
        {
            if (!go[st][c - 'a'])
            {
                go[st][c - 'a'] = go.size();
                go.emplace_back(vector<int>(alpha));
                cost.push_back(0);
                fail.push_back(0);
            }

            st = go[st][c - 'a'];
        }

        cost[st] = a[i];
    }

    queue<int> q;

    for (int c = 0; c < alpha; ++c)
        if (!go[1][c]) go[1][c] = 1;
        else fail[go[1][c]] = 1, q.push(go[1][c]);

    for (; !q.empty(); q.pop())
    {
        int st = q.front(), f = fail[st];

        for (int c = 0; c < 26; ++c)
            if (!go[st][c]) go[st][c] = go[f][c];
            else
            {
                q.push(go[st][c]);
                fail[go[st][c]] = go[f][c];
                cost[go[st][c]] += cost[go[f][c]];
            }
    }

    matrix A(go.size());

    for (int st = 1; st < go.size(); ++st)
        for (int c = 0; c < alpha; ++c)
            A.m[st][go[st][c]] = cost[go[st][c]];

    A = fpow(A, l);

    int64 best = -oo;

    for (int i = 0; i < go.size(); ++i)
        best = max(best, A.m[1][i]);

    cout << best << endl;

    return 0;
}


#include <bits/stdc++.h>
using namespace std;

typedef long long ll;

const int N = 205;
const int MAXS = 26;
const int INF = 0x3f3f3f3f;
const ll MOD = 1e9 + 9;

struct Aho {
    int fail[N], val[N];
    int ch[N][MAXS];
    int sz;

    void clear() {
        sz = 0;
        memset(ch[0], 0, sizeof(ch[0]));
    }

    void insert(char *s, int v) {
        int len = strlen(s);
        int u = 0;
        for(int i = 0; i < len; i++) {
            int ind = s[i] - 'a';
            if(!ch[u][ind]) {
                ch[u][ind] = ++sz;
                memset(ch[sz], 0, sizeof(ch[sz]));
                val[sz] = 0;
            }
            u = ch[u][ind];
        }
        val[u] = v;
    }

    void build() {
        queue<int> q;
        for(int i = 0; i < MAXS; i++) {
            int u = ch[0][i];
            if(u) {
                fail[u] = 0;
                q.push(u);
            }
        }
        while(!q.empty()) {
            int r = q.front();
            q.pop();
            for(int i = 0; i < MAXS; i++) {
                int u = ch[r][i];
                if(!u) { ch[r][i] = ch[ fail[r] ][i]; continue; }
                q.push(u);
                int v = fail[r];
                while(v && !ch[v][i]) v = fail[v];
                fail[u] = ch[v][i];
                val[u] += val[ fail[u] ];
            }
        }
    }

    ll mt[N][N];
    ll res_mt[N][N];
    ll temp_mt[N][N];

    void cal(ll m1[][N], ll m2[][N]) {
        memset(temp_mt, -1, sizeof(temp_mt));
        for(int i = 0; i <= sz; i++) {
            for(int j = 0; j <= sz; j++) {
                if(m1[i][j] == -1) continue;
                for(int k = 0; k <= sz; k++) {
                    if(m2[j][k] == -1) continue;
                    temp_mt[i][k] = max(temp_mt[i][k], m1[i][j] + m2[j][k]);
                }
            }
        }

        for(int i = 0; i <= sz; i++) {
            for(int j = 0; j <= sz; j++) {
                m1[i][j] = temp_mt[i][j];
            }
        }
    }

    ll to_dp(ll n) {
        memset(res_mt, -1, sizeof(res_mt));
        memset(mt, -1, sizeof(mt));
        for(int i = 0; i <= sz; i++) {
            for(int j = 0; j < MAXS; j++) {
                int to = ch[i][j];
                res_mt[i][to] = mt[i][to] = val[to];
            }
        }

        n--;
        while(n) {
            if(n & 1) cal(res_mt, mt);
            cal(mt, mt);
            n >>= 1;
        }

        ll ans = 0;
        for(int i = 0; i <= sz; i++) {
            ans = max(ans, res_mt[0][i]);
        }
        return ans;
    }
};

Aho aho;
char word[N];
int a[N];

int main() {
    int n;
    ll m;
    aho.clear();
    scanf("%d%I64d", &n, &m);

    for(int i = 1; i <= n; i++) scanf("%d", &a[i]);
    for(int i = 1; i <= n; i++) {
        scanf("%s", word);
        aho.insert(word, a[i]);
    }
    aho.build();

    printf("%I64d\n", aho.to_dp(m));

	return 0;
}

/*
3 11
1 1 1
aa
ba
a
*/


#include <bits/stdc++.h>
using namespace std;

typedef long long ll;

const int N = 205;
const int MAXS = 26;
const int INF = 0x3f3f3f3f;
const ll MOD = 1e9 + 9;

struct Aho {
    int fail[N], val[N];
    int ch[N][MAXS];
    int sz;

    void clear() {
        sz = 0;
        memset(ch[0], 0, sizeof(ch[0]));
    }

    void insert(char *s, int v) {
        int len = strlen(s);
        int u = 0;
        for(int i = 0; i < len; i++) {
            int ind = s[i] - 'a';
            if(!ch[u][ind]) {
                ch[u][ind] = ++sz;
                memset(ch[sz], 0, sizeof(ch[sz]));
                val[sz] = 0;
            }
            u = ch[u][ind];
        }
        val[u] += v;
    }

    void build() {
        queue<int> q;
        for(int i = 0; i < MAXS; i++) {
            int u = ch[0][i];
            if(u) {
                fail[u] = 0;
                q.push(u);
            }
        }
        while(!q.empty()) {
            int r = q.front();
            q.pop();
            for(int i = 0; i < MAXS; i++) {
                int u = ch[r][i];
                if(!u) { ch[r][i] = ch[ fail[r] ][i]; continue; }
                q.push(u);
                int v = fail[r];
                while(v && !ch[v][i]) v = fail[v];
                fail[u] = ch[v][i];
                val[u] += val[ fail[u] ];
            }
        }
    }

    ll mt[N][N];
    ll res_mt[N][N];
    ll temp_mt[N][N];

    void cal(ll m1[][N], ll m2[][N]) {
        memset(temp_mt, -1, sizeof(temp_mt));
        for(int i = 0; i <= sz; i++) {
            for(int j = 0; j <= sz; j++) {
                if(m1[i][j] == -1) continue;
                for(int k = 0; k <= sz; k++) {
                    if(m2[j][k] == -1) continue;
                    temp_mt[i][k] = max(temp_mt[i][k], m1[i][j] + m2[j][k]);
                }
            }
        }

        for(int i = 0; i <= sz; i++) {
            for(int j = 0; j <= sz; j++) {
                m1[i][j] = temp_mt[i][j];
            }
        }
    }

    ll to_dp(ll n) {
        memset(res_mt, -1, sizeof(res_mt));
        memset(mt, -1, sizeof(mt));
        for(int i = 0; i <= sz; i++) {
            for(int j = 0; j < MAXS; j++) {
                int to = ch[i][j];
                res_mt[i][to] = mt[i][to] = val[to];
            }
        }

        n--;
        while(n) {
            if(n & 1) cal(res_mt, mt);
            cal(mt, mt);
            n >>= 1;
        }

        ll ans = 0;
        for(int i = 0; i <= sz; i++) {
            ans = max(ans, res_mt[0][i]);
        }
        return ans;
    }
};

Aho aho;
char word[N];
int a[N];

int main() {
    int n;
    ll m;
    aho.clear();
    scanf("%d%I64d", &n, &m);

    for(int i = 1; i <= n; i++) scanf("%d", &a[i]);
    for(int i = 1; i <= n; i++) {
        scanf("%s", word);
        aho.insert(word, a[i]);
    }
    aho.build();

    printf("%I64d\n", aho.to_dp(m));

	return 0;
}

/*
3 11
1 1 1
aa
ba
a
*/


#include <bits/stdc++.h>
using namespace std;

typedef long long ll;

const int N = 205;
const int MAXS = 26;
const int INF = 0x3f3f3f3f;
const ll MOD = 1e9 + 9;

struct Aho {
    int fail[N], val[N];
    int ch[N][MAXS];
    int sz;

    void clear() {
        sz = 0;
        memset(ch[0], 0, sizeof(ch[0]));
    }

    void insert(char *s, int v) {
        int len = strlen(s);
        int u = 0;
        for(int i = 0; i < len; i++) {
            int ind = s[i] - 'a';
            if(!ch[u][ind]) {
                ch[u][ind] = ++sz;
                memset(ch[sz], 0, sizeof(ch[sz]));
                val[sz] = 0;
            }
            u = ch[u][ind];
            cur++;
        }
        val[u] = v;
    }

    void build() {
        queue<int> q;
        for(int i = 0; i < MAXS; i++) {
            int u = ch[0][i];
            if(u) {
                fail[u] = 0;
                q.push(u);
            }
        }
        while(!q.empty()) {
            int r = q.front();
            q.pop();
            for(int i = 0; i < MAXS; i++) {
                int u = ch[r][i];
                if(!u) { ch[r][i] = ch[ fail[r] ][i]; continue; }
                q.push(u);
                int v = fail[r];
                while(v && !ch[v][i]) v = fail[v];
                fail[u] = ch[v][i];
                val[u] += val[ fail[u] ];
            }
        }
    }

    ll mt[N][N];
    ll res_mt[N][N];
    ll temp_mt[N][N];

    void cal(ll m1[][N], ll m2[][N]) {
        memset(temp_mt, -1, sizeof(temp_mt));
        for(int i = 0; i <= sz; i++) {
            for(int j = 0; j <= sz; j++) {
                if(m1[i][j] == -1) continue;
                for(int k = 0; k <= sz; k++) {
                    if(m2[j][k] == -1) continue;
                    temp_mt[i][k] = max(temp_mt[i][k], m1[i][j] + m2[j][k]);
                }
            }
        }

        for(int i = 0; i <= sz; i++) {
            for(int j = 0; j <= sz; j++) {
                m1[i][j] = temp_mt[i][j];
            }
        }
    }

    ll to_dp(ll n) {
        memset(res_mt, -1, sizeof(res_mt));
        memset(mt, -1, sizeof(mt));
        for(int i = 0; i <= sz; i++) {
            for(int j = 0; j < MAXS; j++) {
                int to = ch[i][j];
                res_mt[i][to] = mt[i][to] = val[to];
            }
        }

        n--;
        while(n) {
            if(n & 1) cal(res_mt, mt);
            cal(mt, mt);
            n >>= 1;
        }

        ll ans = 0;
        for(int i = 0; i <= sz; i++) {
            ans = max(ans, res_mt[0][i]);
        }
        return ans;
    }
};

Aho aho;
char word[N];
int a[N];

int main() {
    int n;
    ll m;
    aho.clear();
    scanf("%d%I64d", &n, &m);

    for(int i = 1; i <= n; i++) scanf("%d", &a[i]);
    for(int i = 1; i <= n; i++) {
        scanf("%s", word);
        aho.insert(word, a[i]);
    }
    aho.build();

    printf("%I64d\n", aho.to_dp(m));

	return 0;
}

/*
3 11
1 1 1
aa
ba
a
*/


#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const int N = 210;
struct matrix{
	int A[N][N];
	matrix() {
		memset(A, 0xcf, sizeof(A));
	}
}G, dp;
int n;
ll m;
int a[N], sum[N], ans;
int ch[N][26], fail[N], tot = 1;
char s[N];
queue<int> q;
matrix Mul(matrix X, matrix Y) {
	matrix Z;
	for (int i = 1; i <= tot; i++) {
		for (int j = 1; j <= tot; j++) {
			for (int k = 1; k <= tot; k++) {
				Z.A[i][j] = max(Z.A[i][j], X.A[i][k] + Y.A[k][j]);
			}
		}
	}
	return Z;
}
matrix ksm(matrix X, ll y) {
	matrix Z;
	for (int i = 1; i <= tot; i++) Z.A[i][i] = 0;
	while (y) {
		if (y & 1) Z = Mul(Z, X);
		X = Mul(X, X);
		y >>= 1;
	}
	return Z;
}
int main() {
	scanf("%d%lld", &n, &m);
	for (int i = 1; i <= n; i++) scanf("%d", &a[i]);
	for (int i = 1; i <= n; i++) {
		scanf("%s", s + 1);
		int p = 1;
		int len = strlen(s + 1);
		for (int j = 1; j <= len; j++) {
			if (!ch[p][s[j] - 'a']) ch[p][s[j] - 'a'] = ++tot;
			p = ch[p][s[j] - 'a'];
		}
		sum[p] += a[i];
	}
	for (int i = 0; i < 26; i++) ch[0][i] = 1;
	fail[1] = 0;
	q.push(1);
	while (!q.empty()) {
		int p = q.front();
		q.pop();
		sum[p] += sum[fail[p]];
		for (int i = 0; i < 26; i++) {
			if (ch[p][i]) {
				fail[ch[p][i]] = ch[fail[p]][i];
				q.push(ch[p][i]);
			} else {
				ch[p][i] = ch[fail[p]][i];
			}
		}
	}

	for (int i = 1; i <= tot; i++) {
		for (int c = 0; c < 26; c++) {
			int j = ch[i][c];
			G.A[i][j] = sum[j];
		}
	}
	G = ksm(G, m);
	dp.A[1][1] = 0;
	dp = Mul(dp, G);
	for (int i = 1; i <= tot; i++) ans = max(ans, dp.A[1][i]);
	cout << ans;
	return 0;
}


#include <bits/stdc++.h>
#define int long long
using namespace std;
const int N = 210;
struct matrix{
	int A[N][N];
	matrix() {
		memset(A, 0xcf, sizeof(A));
	}
}G, dp;
int n;
int m;
int a[N], sum[N], ans;
int ch[N][26], fail[N], tot = 1;
char s[N];
queue<int> q;
matrix Mul(matrix X, matrix Y) {
	matrix Z;
	for (int i = 1; i <= tot; i++) {
		for (int j = 1; j <= tot; j++) {
			for (int k = 1; k <= tot; k++) {
				Z.A[i][j] = max(Z.A[i][j], X.A[i][k] + Y.A[k][j]);
			}
		}
	}
	return Z;
}
matrix ksm(matrix X, int y) {
	matrix Z;
	for (int i = 1; i <= tot; i++) Z.A[i][i] = 0;
	while (y) {
		if (y & 1) Z = Mul(Z, X);
		X = Mul(X, X);
		y >>= 1;
	}
	return Z;
}
#undef int
int main() {
	#define int long long
	scanf("%lld%lld", &n, &m);
	for (int i = 1; i <= n; i++) scanf("%lld", &a[i]);
	for (int i = 1; i <= n; i++) {
		scanf("%s", s + 1);
		int p = 1;
		int len = strlen(s + 1);
		for (int j = 1; j <= len; j++) {
			if (!ch[p][s[j] - 'a']) ch[p][s[j] - 'a'] = ++tot;
			p = ch[p][s[j] - 'a'];
		}
		sum[p] += a[i];
	}
	for (int i = 0; i < 26; i++) ch[0][i] = 1;
	fail[1] = 0;
	q.push(1);
	while (!q.empty()) {
		int p = q.front();
		q.pop();
		sum[p] += sum[fail[p]];
		for (int i = 0; i < 26; i++) {
			if (ch[p][i]) {
				fail[ch[p][i]] = ch[fail[p]][i];
				q.push(ch[p][i]);
			} else {
				ch[p][i] = ch[fail[p]][i];
			}
		}
	}

	for (int i = 1; i <= tot; i++) {
		for (int c = 0; c < 26; c++) {
			int j = ch[i][c];
			G.A[i][j] = sum[j];
		}
	}
	G = ksm(G, m);
	dp.A[1][1] = 0;
	dp = Mul(dp, G);
	for (int i = 1; i <= tot; i++) ans = max(ans, dp.A[1][i]);
	cout << ans;
	return 0;
}


#include <algorithm>
#include <vector>
#include <iostream>
#include <cstdio>
#include <map>
#include <cstring>
#include <queue>
#define mem(a,b) memset(a,b,sizeof(a))
#define LL __int64
#define MAX 100010
#define INF 1000000000000000000LL
using namespace std;

int n, cnt;
LL m;
int num[110];
char str[210];
const int alpha = 26;

struct MATRIX
{
	LL num[210][210];
}matrix, ans, temp;

class ac_automation{
private:
	int to[MAX][alpha], fail[MAX];
	LL end[MAX];
	int root;
public:
	int newnode(){
		for(int i = 0; i < alpha; i ++)
			to[cnt][i] = -1;
		end[cnt] = 0;
		return cnt ++;
	}
	void init(){
		cnt = 0;
		root = newnode();
	}
	void insert(char *str, int val){
		int temp = root;
		for(int i = 0; str[i]; i ++){
			int re = str[i] - 'a';
			if(to[temp][re] == -1)
				to[temp][re] = newnode();
			temp = to[temp][re];
		}
		end[temp] += val;
	}
	void build(){
		queue<int> q;
		int temp;
		fail[root] = root;
		for(int i = 0; i < alpha; i ++){
			if(to[root][i] == -1)
				to[root][i] = root;
			else{
				fail[to[root][i]] = root;
				q.push(to[root][i]);
			}
		}
		while(!q.empty()){
			temp = q.front();
			q.pop();
			end[temp] += end[fail[temp]];
			for(int i = 0; i < alpha; i ++){
				if(to[temp][i] == -1)
					to[temp][i] = to[fail[temp]][i];
				else{
					if(temp == 0)
					{
						fail[to[temp][i]] = 0;
					}
					else
					{
						fail[to[temp][i]] = to[fail[temp]][i];
					}
					q.push(to[temp][i]);
				}
			}
		}
	}

	void get_matrix(){
		for(int i = 0; i < cnt; i ++){
			for(int j = 0; j < cnt; j ++){
				matrix.num[i][j] = -INF;
			}
		}
		for(int i = 0; i < cnt; i ++){
			for(int j = 0; j < alpha; j ++){
				matrix.num[i][to[i][j]] = end[to[i][j]];
			}
		}
	}
}ac;

void solve(struct MATRIX x, LL y){
	for(int i = 0; i < cnt; i ++)
	{
		for(int j = 0; j < cnt; j ++)
		{
			ans.num[i][j] = -INF;
		}
		ans.num[i][i] = 0;
	}
	while(y)
	{
		if(y&1)
		{
			for(int i = 0; i < cnt; i ++)
			{
				for(int j = 0; j < cnt; j ++)
				{
					temp.num[i][j] = -INF;
				}
			}
			for(int i = 0; i < cnt; i ++)
			{
				for(int j = 0; j < cnt; j ++)
				{
					temp.num[i][j] = -INF;
					for(int k = 0; k < cnt; k ++)
					{
						temp.num[i][j] = max(temp.num[i][j], ans.num[i][k] + x.num[k][j]);
					}
				}
			}
			for(int i = 0; i < cnt; i ++)
			{
				for(int j = 0; j < cnt; j ++)
				{
					ans.num[i][j] = temp.num[i][j];
				}
			}
		}
		for(int i = 0; i < cnt; i ++)
		{
			for(int j = 0; j < cnt; j ++)
			{
				temp.num[i][j] = -INF;
			}
		}
		for(int i = 0; i < cnt; i ++)
		{
			for(int j = 0; j < cnt; j ++)
			{
				temp.num[i][j] = -INF;
				for(int k = 0; k < cnt; k ++)
				{
					temp.num[i][j] = max(temp.num[i][j], x.num[i][k] + x.num[k][j]);
				}
			}
		}
		for(int i = 0; i < cnt; i ++)
		{
			for(int j = 0; j < cnt; j ++)
			{
				x.num[i][j] = temp.num[i][j];
			}
		}
		y >>= 1;
	}
	LL sum = 0;
	for(int i = 0; i < cnt; i ++)
	{
		sum = max(sum, ans.num[0][i]);
	}
	printf("%I64d\n",sum);
}

int main()
{
	scanf("%d%I64d",&n,&m);
	for(int i = 0; i < n; i ++)
	{
		scanf("%d",&num[i]);
	}
	ac.init();
	for(int i = 0; i < n; i ++)
	{
		scanf("%s",str);
		ac.insert(str,num[i]);
	}
	ac.build();
	ac.get_matrix();
	solve(matrix, m);
	return 0;
}


/* ===================================*/
/* Author :   Moshiur Rahman Mohim    */
/* Email  :   mtmohim74@gmail.com     */
/* Institute: HSTU                    */
/* ===================================*/

#include <cstdio>
#include <iostream>
#include <cmath>
#include <cstring>
#include <cstdlib>
#include <cctype>

#include <algorithm>
#include <string>
#include <sstream>
#include <vector>
#include <queue>
#include <stack>
#include <list>
#include <map>
#include <set>
using  namespace  std;

const int INF = 0x7f7f7f7f;

#define pi              acos(-1.0)
#define mod             1000000007
#define MAX             10000
#define FOR(i, b, n)    for(LL i=b; i<=n; i++)
#define FORR(i, n, b)   for(LL i=n; i>=b; i--)
// Library.
#define mem(a,b)        memset(a,b,sizeof(a))
#define Sort(x)         sort(x.begin(),x.end())
#define Reverse(x)      reverse(x.begin(),x.end())
#define gcd(a,b)        __gcd(a,b)
#define lcm(a,b)        (a * (b / gcd(a,b)))
#define sq(x)           (x)*(x)
// File.
#define READ(f)         freopen(f, "r", stdin)
#define WRITE(f)        freopen(f, "w", stdout)
// i/o.
#define sf              scanf
#define pf              printf
#define pr1(x)          cout<<x<<"\n"
#define pr2(x,y)        cout<<x<<" "<<y<<"\n"
#define pr3(x,y,z)      cout<<x<<" "<<y<<" "<<z<<"\n"
// Test Case.
#define TEST(i,t)       LL i,t;scanf("%I64d",&t);for(i=1;i<=t;i++)

typedef unsigned int            U;
typedef long int                L;
typedef unsigned long int       LU;
typedef long long int           LL;
typedef unsigned long long int  LLU;
typedef float                   F;
typedef double                  LF;
typedef char                    C;

//STL
typedef vector<LL>vl;
typedef pair<int,int>pii;
typedef pair<string,int>psi;
typedef pair<LL,LL>pll;
typedef pair<LF,LF>pdd;
typedef vector<pii> vpii;

// Compute b^p%m
int BigMod(LL B,LL P,LL M)
{
    LL R=1;
    while(P>0)
    {
        if(P%2==1)
            R=(R*B)%M;
        P/=2;
        B=(B*B)%M;
    }
    return (int)R;
}
//Ugly Numbers
LL maxDivide(LL a,LL b)
{
    while(a%b==0)
        a=a/b;
    return a;
}
LL isUgly(LL no)
{
    no=maxDivide(no,2);
    no=maxDivide(no,3);
    no=maxDivide(no,5);
    return (no==1)?1:0;
}
//XOR of 2 Numbers
LL myXOR(LL x,LL y)
{
    LL res=0;
    FORR(i,31,0)
    {
        bool b1=x&(1<<i);
        bool b2=y&(1<<i);
        bool xoredBit=(b1&b2)?0:(b1|b2);
        res<<=1;
        res|=xoredBit;
    }
    return res;
}
/*............End............*/
/*.......Global Declaration.......*/
LL ar[505];
int main()
{
    //READ("input.txt");
    //WRITE("output.txt");
    LL n,m,a,b,c,x,y,mn,mx,k;
    sf("%lld%lld",&n,&k);
    FOR(i,1,n)
    {
        sf("%lld",&ar[i]);
    }
    x=0;
    if(n==1)
    {
        if(ar[1]>=k)
        {
            pf("0\n%lld\n",ar[1]);
        }
        else
        {
            pf("%lld\n%lld\n",k-ar[1],k);
        }
    }
    else
    {
        FOR(i,2,n)
        {
            if(ar[i]+ar[i-1]<k)
            {
                x+=(k-(ar[i]+ar[i-1]));
                ar[i]+=(k-(ar[i]+ar[i-1]));
            }
        }
        pf("%lld\n",x);
        FOR(i,1,n)
        {
            pf("%lld ",ar[i]);
        }
        pf("\n");
    }
    return 0;
}


/* ===================================*/
/* Author :   Moshiur Rahman Mohim    */
/* Email  :   mtmohim74@gmail.com     */
/* Institute: HSTU                    */
/* ===================================*/

#include <cstdio>
#include <iostream>
#include <cmath>
#include <cstring>
#include <cstdlib>
#include <cctype>

#include <algorithm>
#include <string>
#include <sstream>
#include <vector>
#include <queue>
#include <stack>
#include <list>
#include <map>
#include <set>
using  namespace  std;

const int INF = 0x7f7f7f7f;

#define pi              acos(-1.0)
#define mod             1000000007
#define MAX             10000
#define FOR(i, b, n)    for(LL i=b; i<=n; i++)
#define FORR(i, n, b)   for(LL i=n; i>=b; i--)
// Library.
#define mem(a,b)        memset(a,b,sizeof(a))
#define Sort(x)         sort(x.begin(),x.end())
#define Reverse(x)      reverse(x.begin(),x.end())
#define gcd(a,b)        __gcd(a,b)
#define lcm(a,b)        (a * (b / gcd(a,b)))
#define sq(x)           (x)*(x)
// File.
#define READ(f)         freopen(f, "r", stdin)
#define WRITE(f)        freopen(f, "w", stdout)
// i/o.
#define sf              scanf
#define pf              printf
#define pr1(x)          cout<<x<<"\n"
#define pr2(x,y)        cout<<x<<" "<<y<<"\n"
#define pr3(x,y,z)      cout<<x<<" "<<y<<" "<<z<<"\n"
// Test Case.
#define TEST(i,t)       LL i,t;scanf("%I64d",&t);for(i=1;i<=t;i++)

typedef unsigned int            U;
typedef long int                L;
typedef unsigned long int       LU;
typedef long long int           LL;
typedef unsigned long long int  LLU;
typedef float                   F;
typedef double                  LF;
typedef char                    C;

//STL
typedef vector<LL>vl;
typedef pair<int,int>pii;
typedef pair<string,int>psi;
typedef pair<LL,LL>pll;
typedef pair<LF,LF>pdd;
typedef vector<pii> vpii;

// Compute b^p%m
int BigMod(LL B,LL P,LL M)
{
    LL R=1;
    while(P>0)
    {
        if(P%2==1)
            R=(R*B)%M;
        P/=2;
        B=(B*B)%M;
    }
    return (int)R;
}
//Ugly Numbers
LL maxDivide(LL a,LL b)
{
    while(a%b==0)
        a=a/b;
    return a;
}
LL isUgly(LL no)
{
    no=maxDivide(no,2);
    no=maxDivide(no,3);
    no=maxDivide(no,5);
    return (no==1)?1:0;
}
//XOR of 2 Numbers
LL myXOR(LL x,LL y)
{
    LL res=0;
    FORR(i,31,0)
    {
        bool b1=x&(1<<i);
        bool b2=y&(1<<i);
        bool xoredBit=(b1&b2)?0:(b1|b2);
        res<<=1;
        res|=xoredBit;
    }
    return res;
}
/*............End............*/
/*.......Global Declaration.......*/
LL ar[505],ar1[505];
int main()
{
    //READ("input.txt");
    //WRITE("output.txt");
    LL n,m,a,b,c,x,y,mn,mx,k;
    sf("%lld%lld",&n,&k);
    FOR(i,1,n)
    {
        sf("%lld",&ar[i]);
        ar1[i]=ar[i];
    }
    x=0;
    y=0;
    if(n==1)
    {
        if(n>=k)
        {
            pf("0\n%lld\n",ar[1]);
        }
        else
        {
            pf("%lld\n%lld\n",k-ar[1],k);
        }
    }
    else
    {
        FOR(i,2,n)
        {
            if(ar[i]+ar[i-1]<k)
            {
                x+=(k-(ar[i]+ar[i-1]));
                ar[i]+=(k-(ar[i]+ar[i-1]));
            }
        }
        FORR(i,n-1,1)
        {
            if(ar1[i]+ar1[i+1]<k)
            {
                y+=(k-(ar1[i]+ar1[i+1]));
                ar1[i]+=(k-(ar1[i]+ar1[i+1]));
            }
        }
        if(y<x)
        {
            pf("%lld\n",y);
            FOR(i,1,n)
            {
                pf("%lld ",ar1[i]);
            }
            pf("\n");
        }
        else
        {
            pf("%lld\n",x);
            FOR(i,1,n)
            {
                pf("%lld ",ar[i]);
            }
            pf("\n");
        }
    }
    return 0;
}


#include<iostream>
#include<cmath>
using namespace std;
int main()
{
	int n,k,a[n+10],sum=0;
	cin>>n>>k;
	a[0]=k;
	for (int i=1;i<n+1;i++)
	cin>>a[i];
	for (int i=1;i<n+1;i++)
	{
		if (a[i]+a[i-1]<k)
		{
			sum+=k-a[i]-a[i-1];
			a[i]=k-a[i-1];
		}
	}
	cout<<sum<<endl;
	cout<<a[i]<<" ";
	for (int i=2;i<n+1;i++)
	cout<<a[i]<<" ";
	return 0;
}


#include<iostream>
#include<cmath>
using namespace std;
int main()
{
	int n,k,a[n],sum=0;
	cin>>n>>k;
	a[0]=k;
	for (int i=1;i<n+1;i++)
	cin>>a[i];
	for (int i=1;i<n+1;i++)
	{
		if (a[i]+a[i-1]<k)
		{
			sum+=k-a[i]-a[i-1];
			a[i]=k-a[i-1];
		}
	}
	cout<<sum<<endl;
	for (int i=1;i<n+1;i++)
	cout<<a[i]<<" ";
	return 0;
}


#include<iostream>
#include<cmath>
using namespace std;
int main()
{
	int n,k,a[n],sum=0;
	cin>>n>>k;
	for (int i=0;i<n;i++)
	cin>>a[i];
	for (int i=0;i<n-1;i++)
	{
		if (a[i]+a[i+1]<k)
		{
			sum+=k-a[i]-a[i+1];
			a[i+1]=k-a[i];
		}
	}
	cout<<sum<<endl;
	for (int i=0;i<n;i++)
	cout<<a[i]<<" ";
	return 0;
}


#include<iostream>
#include<cmath>
using namespace std;
int main()
{
	int n,k,a[n],b[n],sum=0;
	cin>>n>>k;
	for (int i=0;i<n;i++)
	cin>>a[i];
	for (int i=0;i<n-1;i++)
	{
		if (a[i]+a[i+1]<k)
		{
			sum+=k-a[i]-a[i+1];
			a[i+1]=k-a[i];
		}
	}
	cout<<sum<<endl;
	for (int i=0;i<n;i++)
	cout<<a[i]<<" ";
	cout<<endl;
}


#include<iostream>
#include<cmath>
using namespace std;
int main()
{
	int n,k,a[n],b[n],sum=0;
	cin>>n>>k;
	for (int i=0;i<n;i++)
	cin>>a[i];
	for (int i=0;i<n-1;i++)
	{
		if (a[i]+a[i+1]<k)
		{
			sum+=k-a[i]-a[i+1];
			a[i+1]=k-a[i];
		}
	}
	cout<<sum<<endl;
	for (int i=0;i<n;i++)
	cout<<a[i]<<" ";
}


#include<iostream>
#include<cmath>
using namespace std;
int main()
{
	int n,k,a[n+10],sum=0;
	cin>>n>>k;
	a[0]=k;
	for (int i=1;i<n+1;i++)
	cin>>a[i];
	for (int i=1;i<n+1;i++)
	{
		if (a[i]+a[i-1]<k)
		{
			sum+=k-a[i]-a[i-1];
			a[i]=k-a[i-1];
		}
		else
		continue;
	}
	cout<<sum<<endl;
	cout<<a[1]<<" ";
	for (int i=2;i<n+1;i++)
	cout<<a[i]<<" ";
	return 0;
}


#include<iostream>
#include<cmath>
using namespace std;
int main()
{
	int n,k,a[n+10],sum=0;
	cin>>n>>k;
	a[0]=k;
	for (int i=1;i<n+1;i++)
	cin>>a[i];
	for (int i=1;i<n+1;i++)
	{
		if (a[i]+a[i-1]<k)
		{
			sum+=k-a[i]-a[i-1];
			a[i]=k-a[i-1];
		}
	}
	cout<<sum<<endl;
	cout<<a[1]<<" ";
	for (int i=2;i<n+1;i++)
	cout<<a[i]<<" ";
	return 0;
}


#include<iostream>
#include<cmath>
using namespace std;
int main()
{
	int n,k,sum=0;
	cin>>n>>k;
	int a[n];
	for (int i=0;i<n;i++)
	cin>>a[i];
	for (int i=0;i<n-1;i++)
	{
		if (a[i]+a[i+1]<k)
		{
			sum+=k-a[i]-a[i+1];
			a[i+1]=k-a[i];
		}
	}
	cout<<sum<<endl;
	for (int i=0;i<n;i++)
	cout<<a[i]<<" ";
}


#include <bits/stdc++.h>
#define rep(i,n) for(int i=0;i<(n);i++)
#define ALL(A) A.begin(), A.end()
#define INF 1<<20

using namespace std;

typedef long long ll;
typedef pair<int, int> P;

int dp[505][505];

int main()
{
	memset(dp, 0, sizeof(dp));
	ios_base::sync_with_stdio(0);
	cin.tie(0);
	int n, k; cin >> n >> k;
	vector<int> a(n, 0);
	rep (i, n) cin >> a[i];

	bool no_need_add = true;
	for (int i = 1; i < n; ++i){
		if (a[i-1] + a[i] >= k) continue;
		no_need_add = false;
	} // end for
	if (no_need_add){
		cout << 0 << endl;
		rep (i, n) cout << a[i] << (i != n - 1 ? ' ' : '\n');
		return 0;
	} // end if

	for (int i = 0; i <= k; ++i){
		dp[i][0] = i;
		for (int j = 1; j < n; ++j){
			dp[i][j] = k - dp[i][j-1];
		} // end for
	} // end for

	vector<int> res(n, 0);
	int minsum = INF;
	for (int i = a[0]; i <= k; ++i){
		bool ok = true;
		int sum = 0;
		for (int j = 0; j < n; ++j){
			if (dp[i][j] < a[j]) {
				ok = false;
				break;
			} // end if
			sum += (dp[i][j] - a[j]);
		} // end for
		if (ok){
			if (minsum > sum){
				minsum = sum;
				rep (j, n) res[j] = dp[i][j];
			} // end if
		} // end if
	} // end for

	cout << minsum << endl;
	rep (i, n) cout << res[i] << (i != n - 1 ? ' ' : '\n');

	return 0;
}


#include <bits/stdc++.h>
#define rep(i,n) for(int i=0;i<(n);i++)
#define ALL(A) A.begin(), A.end()

using namespace std;

typedef long long ll;
typedef pair<int, int> P;

int main()
{
	ios_base::sync_with_stdio(0);
	cin.tie(0);
	int n, k; cin >> n >> k;
	vector<int> a(n, 0);
	rep (i, n) cin >> a[i];

	if (n == 1){
		int diff = max(0, k - a[0]);
		cout << diff << endl;
		cout << a[0] + diff << endl;
		return 0;
	} // end if

	int res = 0;
	for (int i = 1; i < n; ++i){
		int diff = max(0, k - (a[i-1] + a[i]));
		res += diff;
		a[i] += diff;
	} // end if

	cout << res << endl;
	rep (i, n) cout << a[i] << (i != n - 1 ? ' ' : '\n');

	return 0;
}


#include <bits/stdc++.h>
#define rep(i,n) for(int i=0;i<(n);i++)
#define ALL(A) A.begin(), A.end()

using namespace std;

typedef long long ll;
typedef pair<int, int> P;

int main()
{
	ios_base::sync_with_stdio(0);
	cin.tie(0);
	int n, k; cin >> n >> k;
	vector<int> a(n, 0);
	rep (i, n) cin >> a[i];

	if (n == 1){
		cout << max(k - a[0], 0) << endl;
		return 0;
	} // end if

	int res = 0;
	for (int i = 1; i < n; ++i){
		int diff = max(0, k - (a[i-1] + a[i]));
		res += diff;
		a[i] += diff;
	} // end if

	cout << res << endl;
	rep (i, n) cout << a[i] << (i != n - 1 ? ' ' : '\n');

	return 0;
}


#include <bits/stdc++.h>
#define rep(i,n) for(int i=0;i<(n);i++)
#define ALL(A) A.begin(), A.end()
#define INF 1<<20

using namespace std;

typedef long long ll;
typedef pair<int, int> P;

int dp[505][505];

int main()
{
	memset(dp, 0, sizeof(dp));
	ios_base::sync_with_stdio(0);
	cin.tie(0);
	int n, k; cin >> n >> k;
	vector<int> a(n, 0);
	rep (i, n) cin >> a[i];

	bool no_need_add = true;
	for (int i = 1; i < n; ++i){
		if (a[i-1] + a[i] < k){
			no_need_add = false;
			break;
		} // end if
	} // end for

	if (no_need_add){
		cout << 0 << endl;
		rep (i, n) cout << a[i] << (i != n - 1 ? ' ' : '\n');
		return 0;
	} // end if

	for (int i = a[0]; i <= k; ++i){
		dp[i][0] = a[0];
		for (int j = 1; j < n; ++j){
			dp[i][j] = max(k - dp[i][j-1], a[j]);
		} // end for
	} // end for

	vector<int> res(n, 0);
	int minsum = INF;
	for (int i = a[0]; i <= k; ++i){
		int sum = 0;
		for (int j = 0; j < n; ++j){
			sum += (dp[i][j] - a[j]);
		} // end for
		if (minsum > sum){
			minsum = sum;
			rep (j, n) res[j] = dp[i][j];
		} // end if
	} // end for

	cout << minsum << endl;
	rep (i, n) cout << res[i] << (i != n - 1 ? ' ' : '\n');

	return 0;
}


#include <bits/stdc++.h>
#define rep(i,n) for(int i=0;i<(n);i++)
#define ALL(A) A.begin(), A.end()

using namespace std;

typedef long long ll;
typedef pair<int, int> P;

int main()
{
	ios_base::sync_with_stdio(0);
	cin.tie(0);
	int n, k; cin >> n >> k;
	vector<int> a(n, 0);
	rep (i, n) cin >> a[i];

	if (n == 1){
		cout << 0 << endl;
		cout << a[0] << endl;
		return 0;
	} // end if

	int res = 0;
	for (int i = 1; i < n; ++i){
		int diff = max(0, k - (a[i-1] + a[i]));
		res += diff;
		a[i] += diff;
	} // end if

	cout << res << endl;
	rep (i, n) cout << a[i] << (i != n - 1 ? ' ' : '\n');

	return 0;
}


#include <bits/stdc++.h>
#define rep(i,n) for(int i=0;i<(n);i++)
#define ALL(A) A.begin(), A.end()
#define INF 1<<20

using namespace std;

typedef long long ll;
typedef pair<int, int> P;

int dp[505][505];

int main()
{
	memset(dp, 0, sizeof(dp));
	ios_base::sync_with_stdio(0);
	cin.tie(0);
	int n, k; cin >> n >> k;
	vector<int> a(n, 0);
	rep (i, n) cin >> a[i];

	for (int i = a[0]; i <= k; ++i){
		dp[i][0] = a[0];
		for (int j = 1; j < n; ++j){
			dp[i][j] = max(k - dp[i][j-1], a[j]);
		} // end for
	} // end for

	vector<int> res(n, 0);
	int minsum = INF;
	for (int i = a[0]; i <= k; ++i){
		int sum = 0;
		for (int j = 0; j < n; ++j){
			sum += (dp[i][j] - a[j]);
		} // end for
		if (minsum > sum){
			minsum = sum;
			rep (j, n) res[j] = dp[i][j];
		} // end if
	} // end for

	cout << minsum << endl;
	rep (i, n) cout << res[i] << (i != n - 1 ? ' ' : '\n');

	return 0;
}


#include <stdio.h>
#include <iostream>
#include <algorithm>
#include <cstring>
#include <string>
#include <cmath>
#include <vector>
#include <set>
#include <map>
#include <queue>

using namespace std;

typedef long long ll;
typedef vector<int> vi;
typedef pair<int, int> ii;
typedef pair<pair<int, int>, int> iii;
typedef vector<ii> vii;
typedef set<int> si;
typedef map<string, int> msi;

#define INF 2000000000 // 2 billion
#define MEMSET_INF 127 // about 2B
#define MEMSET_HALF_INF 63 // about 1B

int N, K;
vi walks;
vi walks_rev;

int main(){
  scanf("%d%d", &N, &K);

  if(N == 1){
    int d;
    scanf("%d", &d);
    printf("%d\n%d\n", 0, d);
    return 0;
  }else{
    for(int i = 0; i < N; ++i){
      int a;
      scanf("%d", &a);
      walks.push_back(a);
    }
  }

  int total = 0;

  for(int i = 1; i < N; ++i){
    if((walks[i-1] + walks[i]) < K){
      int diff = K - walks[i-1] - walks[i];
      walks[i] += diff;
      total += diff;
    }
  }

    printf("%d\n", total);
    for(int i = 0; i < walks.size(); ++i) printf("%d ", walks[i]);
    printf("\n");
}


#include <stdio.h>
#include <iostream>
#include <algorithm>
#include <cstring>
#include <string>
#include <cmath>
#include <vector>
#include <set>
#include <map>
#include <queue>

using namespace std;

typedef long long ll;
typedef vector<int> vi;
typedef pair<int, int> ii;
typedef pair<pair<int, int>, int> iii;
typedef vector<ii> vii;
typedef set<int> si;
typedef map<string, int> msi;

#define INF 2000000000 // 2 billion
#define MEMSET_INF 127 // about 2B
#define MEMSET_HALF_INF 63 // about 1B

int N, K;
vi walks;
vi walks_rev;

int main(){
  scanf("%d%d", &N, &K);

  if(N == 1){
    int d; scanf("%d", &d);
    int ret = (d < K) ? K-d : 0;
    printf("%d\n", ret);
    printf("%d\n", K);
    return 0;
  }

  int total1, total2;
  total1 = total2 = 0;

  for(int i = 0; i < N; ++i){
    int a;
    scanf("%d", &a);
    walks.push_back(a);
    walks_rev.push_back(a);
  }

  for(int i = 1; i < N; ++i){
    if((walks[i-1] + walks[i]) < K){
      if(i < N-1 && walks[i] + walks[i+1] >= K) continue;
      int diff = K - walks[i-1] - walks[i];
      walks[i] += diff;
      total1 += diff;
    }
  }

    printf("%d\n", total1);
    for(int i = 0; i < walks.size(); ++i) printf("%d ", walks[i]);
    printf("\n");
}


#include <stdio.h>
#include <iostream>
#include <algorithm>
#include <cstring>
#include <string>
#include <cmath>
#include <vector>
#include <set>
#include <map>
#include <queue>

using namespace std;

typedef long long ll;
typedef vector<int> vi;
typedef pair<int, int> ii;
typedef pair<pair<int, int>, int> iii;
typedef vector<ii> vii;
typedef set<int> si;
typedef map<string, int> msi;

#define INF 2000000000 // 2 billion
#define MEMSET_INF 127 // about 2B
#define MEMSET_HALF_INF 63 // about 1B

int N, K;
vi walks;
vi walks_rev;

int main(){
  scanf("%d%d", &N, &K);

  if(N == 1){
    int d; scanf("%d", &d);
    int ret = (d < K) ? K-d : 0;
    printf("%d\n", ret);
    printf("%d\n", K);
    return 0;
  }

  int total1, total2;
  total1 = total2 = 0;

  for(int i = 0; i < N; ++i){
    int a;
    scanf("%d", &a);
    walks.push_back(a);
    walks_rev.push_back(a);
  }

  for(int i = 1; i < N; ++i){
    if((walks[i-1] + walks[i]) < K){
      if(i < N-1 && walks[i] + walks[i+1] >= K) continue;
      int diff = K - walks[i-1] - walks[i];
      walks[i] += diff;
      total1 += diff;
    }
  }

  reverse(walks_rev.begin(), walks_rev.end());
  for(int i = 1; i < N; ++i){
    if((walks_rev[i-1] + walks_rev[i]) < K){
      if(i < N-1 && walks_rev[i] + walks_rev[i+1] >= K) continue;
      int diff = K - walks_rev[i-1] - walks_rev[i];
      walks_rev[i] += diff;
      total2 += diff;
    }
  }
  reverse(walks_rev.begin(), walks_rev.end());

  if(total1 <= total2){
    printf("%d\n", total1);
    for(int i = 0; i < walks.size(); ++i) printf("%d ", walks[i]);
  }else{
    printf("%d\n", total2);
    for(int i = 0; i < walks_rev.size(); ++i) printf("%d ", walks_rev[i]);
  }
    printf("\n");
}


#include <stdio.h>
#include <iostream>
#include <algorithm>
#include <cstring>
#include <string>
#include <cmath>
#include <vector>
#include <set>
#include <map>
#include <queue>

using namespace std;

typedef long long ll;
typedef vector<int> vi;
typedef pair<int, int> ii;
typedef pair<pair<int, int>, int> iii;
typedef vector<ii> vii;
typedef set<int> si;
typedef map<string, int> msi;

#define INF 2000000000 // 2 billion
#define MEMSET_INF 127 // about 2B
#define MEMSET_HALF_INF 63 // about 1B

int N, K;
vi walks;
vi walks_rev;

int main(){
  scanf("%d%d", &N, &K);

  if(N == 1){
    int d;
    scanf("%d", &d);
    printf("%d\n%d\n", 0, d);
    return 0;
  }else{
    for(int i = 0; i < N; ++i){
      int a;
      scanf("%d", &a);
      walks.push_back(a);
    }
  }

  int total = 0;

  for(int i = 1; i < N; ++i){
    if((walks[i-1] + walks[i]) < K){
      if(i < N-1 && walks[i] + walks[i+1] >= K) continue;
      int diff = K - walks[i-1] - walks[i];
      walks[i] += diff;
      total += diff;
    }
  }

    printf("%d\n", total);
    for(int i = 0; i < walks.size(); ++i) printf("%d ", walks[i]);
    printf("\n");
}


#include <stdio.h>
#include <iostream>
#include <algorithm>
#include <cstring>
#include <string>
#include <cmath>
#include <vector>
#include <set>
#include <map>
#include <queue>

using namespace std;

typedef long long ll;
typedef vector<int> vi;
typedef pair<int, int> ii;
typedef pair<pair<int, int>, int> iii;
typedef vector<ii> vii;
typedef set<int> si;
typedef map<string, int> msi;

#define INF 2000000000 // 2 billion
#define MEMSET_INF 127 // about 2B
#define MEMSET_HALF_INF 63 // about 1B

int N, K;
vi walks;
vi walks_rev;

int main(){
  scanf("%d%d", &N, &K);

  if(N == 1){
    int d; scanf("%d", &d);
    int ret = (d < K) ? K-d : 0;
    printf("%d\n", ret);
    printf("%d\n", K);
    return 0;
  }

  int total1, total2;
  total1 = total2 =0;

  for(int i = 0; i < N; ++i){
    int a;
    scanf("%d", &a);
    walks.push_back(a);
    walks_rev.push_back(a);
    if(i == 0) continue;
    if((walks[i-1] + walks[i]) < K){
      if(i < N-1 && walks[i] + walks[i+1] >= K) continue;
      int diff = K - walks[i-1] - walks[i];
      walks[i] += diff;
      total1 += diff;
    }
  }

  reverse(walks_rev.begin(), walks_rev.end());
  for(int i = 1; i < N; ++i){
    if((walks_rev[i-1] + walks_rev[i]) < K){
      if(i < N-1 && walks_rev[i] + walks_rev[i+1] >= K) continue;
      int diff = K - walks_rev[i-1] - walks_rev[i];
      walks_rev[i] += diff;
      total2 += diff;
    }
  }
  reverse(walks_rev.begin(), walks_rev.end());

  if(total1 <= total2){
    printf("%d\n", total1);
    for(int i = 0; i < walks.size(); ++i) printf("%d ", walks[i]);
  }else{
    printf("%d\n", total2);
    for(int i = 0; i < walks_rev.size(); ++i) printf("%d ", walks_rev[i]);
  }
    printf("\n");
}


#include <iostream>
using namespace std;
long long f[505][505];
int main() {
	long long n,k,sum=0;
	cin>>n>>k;
	long long a[n+1];
	for(long long i=1;i<=n;i++){
		cin>>a[i];
		sum+=a[i];
	}
	long long b[n+1];
	for(long long i=1;i<=n;i++){
		for(long long j=a[i];j<=k;j++){
			if(i==1){
				f[i][j]=j-a[i];
			}
			else{
				if(a[i-1]>k) f[i][j]=j-a[i]+f[i-1][k+1];
				else if(k-j<a[i-1]) f[i][j]=j-a[i]+f[i-1][a[i-1]];
				else f[i][j]=j-a[i]+f[i-1][k-j];
			}
			//cout<<f[i][j]<<" ";
		}
		if(a[i]>k&&i==1) f[i][k+1]=0;
		else if(a[i]>k) f[i][k+1]=f[i-1][a[i-1]];
		//cout<<f[i][k+1];
		//cout<<endl;
	}
	long long max=f[n][a[n]];
	cout<<max<<endl;
	long long pos=k;
	for(long long i=k;i>=a[n];i--){
		if(f[n][i]==max){
			pos=i;
			break;
		}
	}
	b[n]=pos;
	for(long long i=n-1;i>0;i--){
		if(k-pos>=a[i]) pos=k-pos;
		else pos=a[i];
		b[i]=pos;
	}
	for(long long i=1;i<=n;i++) cout<<b[i]<<" ";
	return 0;
}


#include <iostream>
using namespace std;
long long f[505][505];
int main() {
	long long n,k,sum=0;
	cin>>n>>k;
	long long a[n+1];
	for(long long i=1;i<=n;i++){
		cin>>a[i];
		sum+=a[i];
	}
	long long b[n+1];
	for(long long i=1;i<=n;i++){
		for(long long j=a[i];j<=k;j++){
			if(i==1) f[i][j]=j-a[i];
			else{
				if(k-j<a[i-1]) f[i][j]=j-a[i]+f[i-1][a[i-1]];
				else f[i][j]=j-a[i]+f[i-1][k-j];
			}
			//cout<<f[i][j]<<" ";
		}
		//cout<<endl;
	}
	long long max=f[n][k];
	for(long long i=a[n];i<=k;i++) {
		if(f[n][i]<max) max=f[n][i];
	}
	cout<<max<<endl;
	long long pos=k;
	for(long long i=k;i>=a[n];i--){
		if(f[n][i]==max){
			pos=i;
			break;
		}
	}
	b[n]=pos;
	for(long long i=n-1;i>0;i--){
		if(k-pos>=a[i]) pos=k-pos;
		else pos=a[i];
		b[i]=pos;
	}
	for(long long i=1;i<=n;i++) cout<<b[i]<<" ";
	return 0;
}


#include <iostream>
using namespace std;
long long f[505][505];
int main() {
	long long n,k,sum=0;
	cin>>n>>k;
	long long a[n+1];
	for(long long i=1;i<=n;i++){
		cin>>a[i];
		sum+=a[i];
	}
	long long b[n+1];
	for(long long i=1;i<=n;i++){
		for(long long j=a[i];j<=k;j++){
			if(i==1){
				f[i][j]=j-a[i];
			}
			else{
				if(a[i-1]>k) f[i][j]=j-a[i]+f[i-1][k+1];
				else if(k-j<a[i-1]) f[i][j]=j-a[i]+f[i-1][a[i-1]];
				else f[i][j]=j-a[i]+f[i-1][k-j];
			}
			//cout<<f[i][j]<<" ";
		}
		if(a[i]>k&&i==1) f[i][k+1]=0;
		else if(a[i]>k) f[i][k+1]=f[i-1][a[i-1]];
		//cout<<f[i][k+1];
		//cout<<endl;
	}
	long long max=f[n][k];
	for(long long i=a[n];i<=k;i++) {
		if(f[n][i]<max) max=f[n][i];
	}
	cout<<max<<endl;
	long long pos=k;
	for(long long i=k;i>=a[n];i--){
		if(f[n][i]==max){
			pos=i;
			break;
		}
	}
	b[n]=pos;
	for(long long i=n-1;i>0;i--){
		if(k-pos>=a[i]) pos=k-pos;
		else pos=a[i];
		b[i]=pos;
	}
	for(long long i=1;i<=n;i++) cout<<b[i]<<" ";
	return 0;
}


#include <iostream>
using namespace std;

int main() {
	long long n,k,sum=0,result=0;
	cin>>n>>k;
	long long a[n+1];
	for(long long i=1;i<=n;i++){
		cin>>a[i];
		sum+=a[i];
	}
	for(long long i=2;i<=n;i++){
		if(a[i]+a[i-1]<k) {
			result+=k-a[i]-a[i-1];
			a[i]+=k-a[i]-a[i-1];
		}
	}
	cout<<result<<endl;
	for(long long i=1;i<=n;i++) cout<<a[i]<<" ";
	return 0;
}


#include <iostream>
using namespace std;
long long f[505][505];
int main() {
	long long n,k,sum=0;
	cin>>n>>k;
	long long a[n+1];
	for(long long i=1;i<=n;i++){
		cin>>a[i];
		sum+=a[i];
	}
	long long b[n+1];
	for(long long i=1;i<=n;i++){
		for(long long j=a[i];j<=k;j++){
			if(i==1) f[i][j]=j-a[1];
			else{
				if(a[i-1]>k) f[i][j]=j-a[i]+f[i-1][k+1];
				else if(k-j<a[i-1]) f[i][j]=j-a[i]+f[i-1][a[i-1]];
				else f[i][j]=j-a[i]+f[i-1][k-j];
			}
			//cout<<f[i][j]<<" ";
		}
		if(a[i]>k) f[i][k+1]=f[i-1][a[i-1]];
		//cout<<f[i][k+1];
		//cout<<endl;
	}
	long long max=f[n][k];
	for(long long i=a[n];i<=k;i++) {
		if(f[n][i]<max) max=f[n][i];
	}
	cout<<max<<endl;
	long long pos=k;
	for(long long i=k;i>=a[n];i--){
		if(f[n][i]==max){
			pos=i;
			break;
		}
	}
	b[n]=pos;
	for(long long i=n-1;i>0;i--){
		if(k-pos>=a[i]) pos=k-pos;
		else pos=a[i];
		b[i]=pos;
	}
	for(long long i=1;i<=n;i++) cout<<b[i]<<" ";
	return 0;
}


#include<stdio.h>
#include<string.h>
#include<math.h>
#include<ctype.h>
#include<stdlib.h>
#include<cstdio>
#include<cstring>
#include<string>
#include<cmath>
#include<iostream>
#include<cctype>
#include<map>
#include<stack>
#include<cstdlib>
#include<queue>
#include<vector>
#include<algorithm>
#include<bitset>
#include<algorithm>
#include<set>
#include<limits.h>

using namespace std;
typedef long int LD;
typedef long long int LLD;
typedef float F;
typedef double LF;
typedef unsigned int U;
typedef unsigned long int LU;
typedef unsigned long long int LLU;
typedef char C;

/*----------simple input section ------------ */
#define sf scanf
#define sfi(x) scanf("%d",&x)
#define sfc(x) scanf("%c",&x)
#define sfi2(x,y) scanf("%d%d",&x,&y)
#define sfl(x) scanf("%ld",&x)
#define sfll(x) scanf("%lld",&x)
#define sfd(x) scanf("%lf",&x)
#define sfu(x) scanf("%llu",&x)
#define sfs(x) scanf("%s",x)

/*----------simple input section ------------ */
#define pf printf
#define pfi(x) printf("%d\n",x)
#define pfl(x) printf("%ld\n",x)
#define pfll(x) printf("%lld\n",x)
#define pfu(x) printf("%llu\n",x)
#define pfs(x) printf("%s\n",x)
#define pfc(x) printf("%c\n",x)
#define pfd(x) printf("%lf\n",x)

/*----------file input section ------------ */

#define f_input  freopen("input.txt","r",stdin);
#define f_output freopen("output.txt","w",stdout);

#define pb(x) push_back(x)
#define PI acos(-1.0)
#define sq(x) (x)*(x)
#define mem(x,y) memset(x,y,sizeof(x))
#define TEST int cs,T;sfi(T);getchar();for(cs=1;cs<=T;cs++)
#define nn printf("\n")
// xx-> diagonal -> 8 horizontal/vertical->4

const int xx[] = {0, 1, 0, -1, -1, 1, -1, 1};
const int yy[] = {1, 0, -1, 0, 1, 1, -1, -1};

// KX-> Knight moves
const int kx[] = {-2, -1, 1, 2, 2, 1, -1, -2};
const int ky[] = {1, 2, 2, 1, -1, -2, -2, -1};

/******** debug **********/
#define chk1 printf("hi......1  \n")
#define chk2 printf("hi......2  \n")

#define mod 1000003
#define eps 10e-8
#define sz 30004
#define sz1 55
#define sz2 504
/******* start my code ********/

int arr[sz2];
int brr[sz2];
vector<int>v;
int main()
{
    int i,j,k,n,sum,sum2;
    sfi2(n,k);
    for(i=1; i<=n; i++)
    {
        sfi(arr[i]);
        brr[i]=arr[i];
    }
    sum2=sum=0;
    arr[0]=0;
    if(n==1)
    {
        if(arr[1]<k)
        {
            pfi(k-arr[1]);
            pfi(k);
        }
        else
        {
            pfi(0);
            pfi(arr[1]);
        }
        return 0;
    }
    for(i=2; i<=n; i++)
    {
        // pf("%d %d\n",arr[i]-arr[i-1],k);
        if(arr[i]+arr[i-1]<k)
        {
            // chk1;
            sum+=k-(arr[i]+arr[i-1]);
            arr[i]+=k-(arr[i]+arr[i-1]);
        }
    }
    for(i=1;i<n;i++)
    {
        if(brr[i]+brr[i+1]<k)
        {
            sum2+=k-(brr[i]+brr[i+1]);
            brr[i]+=k-(brr[i]+brr[i+1]);
        }
    }
    if(sum<=sum2)
    {
        pfi(sum);
        for(i=1; i<=n; i++)
            pf("%d ",arr[i]);
    }
    else
    {//chk1;
        pfi(sum2);
        for(i=1; i<=n; i++)
            pf("%d ",brr[i]);
    }
    return 0;
}

/*

*/