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

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

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

Input

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

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

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

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

Output

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

Examples

Input

2
aa
bb
2 1


Output

4


Input

2
aa
ab
2 1


Output

5

Solution (in JAVA):

Solution:
import java.io.*;
import java.util.*;

public class F616 {
	public static void main(String[] args) { new F616(); }
	FS in = new FS();
	PrintWriter out = new PrintWriter(System.out);

	int n, len, ptr;
	long ans, cost[];
	char[] t[], T;
	int sID;
	int[] end, idx, sffxLen;
	boolean[] tryFull;

	BIT f;
	RMQ rmq;
	SuffixArray SA;

	F616() {
		n = in.nextInt();
		
		len = 0;
		t = new char[n][];
		end = new int[n];
		for (int i = 0; i < n; i++) {
			t[i] = in.next().toCharArray();
			len += t[i].length;
			end[i] = t[i].length;
		}
		len += n;

		cost = new long[n + 1];
		for (int i = 0; i < n; i++) cost[i] = in.nextLong();
		cost[n] = 0;

		ptr = 0;
		idx = new int[len];
		sffxLen = new int[len];
		T = new char[len];
		for (sID = 0; sID < n; sID++) {
			for (int i = 0; i < end[sID]; i++) {
				idx[ptr + i] = sID;
				sffxLen[ptr + i] = end[sID] - i;
				T[ptr + i] = t[sID][i];
			}
			ptr += end[sID];
			idx[ptr] = n;
			T[ptr++] =  '~';
		}

		SA = new SuffixArray();
		int[] sa = SA.suffixArray(T);
		int[] lcp = SA.lcp(sa, T);

		tryFull = new boolean[len - n];
		for (int i = 0; i < len - n; i++) {
			int sffx = sffxLen[sa[i]];

			int prv = -1, nxt = -1;
			if (i > 0) prv = lcp[i - 1];
			if (i < len - n - 1) nxt = lcp[i];

			int max = max(prv, nxt);
			if (i > 0) lcp[i - 1] = min(lcp[i - 1], sffx);
			if (i < len - n - 1) lcp[i] = min(lcp[i], sffx);
			if (max < sffx) tryFull[i] = true;
		}

		ans = 0;
		f = new BIT(len);
		for (int i = 0; i < len; i++) f.upd(i, cost[idx[sa[i]]]);
		rmq = new RMQ(lcp);

		int l, r;
		for (int i = 0; i < len - n - 1; i++) {
			int seek = lcp[i];
			if (seek == 0) continue;

			l = i; r = i;
			for (int j = 21; j >= 0; j--) {
				if ((l - (1 << j)) >= 0 && lcpRange(l - (1 << j), i) >= seek) l -= (1 << j);
				if ((r + (1 << j)) < len && lcpRange(i, r + (1 << j)) >= seek) r += (1 << j);
			}

			ans = max(ans, seek * f.sum(l, r));
		}

		for (int i = 0; i < len - n; i++)
			if (tryFull[i])
				ans = max(ans, sffxLen[sa[i]] * f.sum(i, i));

		out.println(ans);
		out.close();
	}

	int lcpRange(int x, int y) { return rmq.query(x, y - 1); }
	class RMQ {
		int sz, vs[], lift[][];
		RMQ (int[] vs) {
			sz = vs.length; this.vs = vs;
			int maxlg = Integer.numberOfTrailingZeros(Integer.highestOneBit(sz)) + 2;
			lift = new int[maxlg][sz];
			for (int i = 0; i < sz; i++) lift[0][i] = vs[i];
			for (int lg = 1, lr = 1; lg < maxlg; lg++, lr <<= 1)
				for (int i = 0; i < sz; i++)
					lift[lg][i] = min(lift[lg - 1][i], lift[lg - 1][min(i + lr, sz - 1)]);
		}

		int query(int lo, int hi) {
			int r = hi - lo + 1;
			int e = Integer.highestOneBit(r);
			int lg = Integer.numberOfTrailingZeros(e);
			return min(lift[lg][lo], lift[lg][hi - e + 1]);
		}
	}
	class BIT {
		int sz; long[] tr;
		BIT (int n) { tr = new long[(sz = n + 2) + 1]; }
		void upd(int i, long x) {
			i++;
			while (i <= sz) {
				tr[i] += x;
				i += (i & -i);
			}
		}
		long sum(int i) {
			i++; long res = 0;
			while (i > 0) {
				res += tr[i];
				i -= (i & -i);
			} return res;
		}
		long sum(int i, int j) { return sum(j) - sum(i - 1); }
	}
	class SuffixArray {
		int[] suffixArray(char[] S) {
			int n = S.length;
			Integer[] ord = new Integer[n];
			for (int i = 0; i < n; i++) ord[i] = n - 1 - i;
			Arrays.sort(ord, (a, b) -> Character.compare(S[a], S[b]));
			int[] sa = new int[n]; int[] bckts = new int[n];
			for (int i = 0; i < n; i++) {
				sa[i] = ord[i];
				bckts[i] = S[i];
			}
			for (int j = 1; j < n; j <<= 1) {
				int[] c = bckts.clone();
				for (int i = 0; i < n; i++)
					bckts[sa[i]] = i > 0 && c[sa[i - 1]] == c[sa[i]] && sa[i - 1] + j < n && c[sa[i - 1] + (j >> 1)] == c[sa[i] + (j >> 1)] ? bckts[sa[i - 1]] : i; 
				int[] cnt = new int[n];
				for (int i = 0; i < n; i++) cnt[i] = i;
				int[] s = sa.clone();
				for (int i = 0; i < n; i++) {
					int s1 = s[i] - j;
					if (s1 >= 0) sa[cnt[bckts[s1]]++] = s1;
				}
			}
			return sa;
		}
		int[] lcp(int[] sa, char[] S) {
			int n = sa.length;
			int[] rank = new int[n];
			for (int i = 0; i < n; i++) rank[sa[i]] = i;
			int[] lcp = new int[n - 1];
			for (int i = 0, h = 0; i < n; i++) {
				if (rank[i] < n - 1) {
					for (int j = sa[rank[i] + 1]; max(i, j) + h < S.length && S[i + h] == S[j + h]; ++h);
					lcp[rank[i]] = h;
					if (h > 0) --h;
				}
			}
			return lcp;
		}
	}
	
	int abs(int x) { if (x < 0) return -x; return x; }
	long abs(long x) { if (x < 0) return -x; return x; }
	int min(int a, int b) { if (a < b) return a; return b; }	
	int max(int a, int b) { if (a > b) return a; return b; }	
	long min(long a, long b) { if (a < b) return a; return b; }	
	long max(long a, long b) { if (a > b) return a; return b; }	
	int gcd(int a, int b) { while (b > 0) { a = b^(a^(b = a)); b %= a; } return a; }
	long gcd(long a, long b) { while (b > 0) { a = b^(a^(b = a)); b %= a; } return a; }
	long lcm(int a, int b) { return (((long) a) * b) / gcd(a, b); }
	long lcm(long a, long b) { return (a * b) / gcd(a, b); }

	void sort(int[] arr) {
		int sz = arr.length, j;
		Random r = new Random();
		for (int i = 0; i < sz; i++) {
			j = r.nextInt(i + 1);
			arr[i] = arr[j]^(arr[i]^(arr[j] = arr[i]));
		} Arrays.sort(arr);
	}

	class FS {
		BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
		StringTokenizer st = new StringTokenizer("");
		String next() {
			while (!st.hasMoreTokens()) {
				try { st = new StringTokenizer(br.readLine()); }
				catch (Exception e) {}
			} return st.nextToken();
		}
		int nextInt() { return Integer.parseInt(next()); }
		long nextLong() { return Long.parseLong(next()); }
		double nextDouble() { return Double.parseDouble(next()); }
	}
}