CF BUDDY
← Problems·

1131E · String Multiplication

2300 · dp, greedy, strings

Problem: Roman and Denis are on the trip to the programming competition. Since the trip was long, they soon got bored, and hence decided to came up with something. Roman invented a pizza's recipe, while Denis invented a string multiplication. According to Denis, the result of multiplication (product) of strings ss of length mm and tt is a string t+s1+t+s2++t+sm+tt + s_1 + t + s_2 + \ldots + t + s_m + t, where sis_i denotes the ii-th symbol of the string ss, and "+" denotes string concatenation. For example, the product of strings "abc" and "de" is a string "deadebdecde", while the product of the strings "ab" and "z" is a string "zazbz". Note, that unlike the numbers multiplication, the product of strings ss and tt is not necessarily equal to product of tt and ss.

Roman was jealous of Denis, since he invented such a cool operation, and hence decided to invent something string-related too. Since Roman is beauty-lover, he decided to define the beauty of the string as the length of the longest substring, consisting of only one letter. For example, the beauty of the string "xayyaaabca" is equal to 33, since there is a substring "aaa", while the beauty of the string "qwerqwer" is equal to 11, since all neighboring symbols in it are different.

In order to entertain Roman, Denis wrote down nn strings p1,p2,p3,,pnp_1, p_2, p_3, \ldots, p_n on the paper and asked him to calculate the beauty of the string ((((p1p2)p3))pn( \ldots (((p_1 \cdot p_2) \cdot p_3) \cdot \ldots ) \cdot p_n, where sts \cdot t denotes a multiplication of strings ss and tt. Roman hasn't fully realized how Denis's multiplication works, so he asked you for a help. Denis knows, that Roman is very impressionable, he guarantees, that the beauty of the resulting string is at most 10910^9.

Input Format: The first line contains a single integer nn (2n1000002 \leq n \leq 100\,000) — the number of strings, wroted by Denis.

Next nn lines contain non-empty strings p1,p2,,pnp_1, p_2, \ldots, p_n, consisting of lowercase english letters.

It's guaranteed, that the total length of the strings pip_i is at most 100000100\,000, and that's the beauty of the resulting product is at most 10910^9.

Output Format: Print exactly one integer — the beauty of the product of the strings.

Note: In the first example, the product of strings is equal to "abaaaba".

In the second example, the product of strings is equal to "abanana".

Sample Cases

Case 1

Input

3
a
b
a

Output

3

Case 2

Input

2
bnn
a

Output

1

Similar problems

00:00:00
Loading editor…
Welcome! I'm your coding tutor for this problem. Use the chips below to reveal stored hints or get AI feedback on your code. I'll guide you step by step — never giving away the solution.

Sign in to unlock AI tutor feedback