String Multiplication
Contest: Codeforces Round 541 (Div. 2)
Difficulty rating: 2300
Tags: dp, greedy, strings

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 $$$s$$$ of length $$$m$$$ and $$$t$$$ is a string $$$t + s_1 + t + s_2 + \ldots + t + s_m + t$$$, where $$$s_i$$$ denotes the $$$i$$$-th symbol of the string $$$s$$$, 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 $$$s$$$ and $$$t$$$ is not necessarily equal to product of $$$t$$$ and $$$s$$$.

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 $$$3$$$, since there is a substring "aaa", while the beauty of the string "qwerqwer" is equal to $$$1$$$, since all neighboring symbols in it are different.

In order to entertain Roman, Denis wrote down $$$n$$$ strings $$$p_1, p_2, p_3, \ldots, p_n$$$ on the paper and asked him to calculate the beauty of the string $$$( \ldots (((p_1 \cdot p_2) \cdot p_3) \cdot \ldots ) \cdot p_n$$$, where $$$s \cdot t$$$ denotes a multiplication of strings $$$s$$$ and $$$t$$$. 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 $$$10^9$$$.

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

Next $$$n$$$ lines contain non-empty strings $$$p_1, p_2, \ldots, p_n$$$, consisting of lowercase english letters.

It's guaranteed, that the total length of the strings $$$p_i$$$ is at most $$$100\,000$$$, and that's the beauty of the resulting product is at most $$$10^9$$$.

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

Constraints:
Time limit: 2.0 s
Memory limit: 256.0 MB

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".

Example input:
3
a
b
a

Example output:
3

Example input:
2
bnn
a

Example output:
1

Editorial:
Let's notice, that the string multiplication is associative, that is $$$(a \cdot b) \cdot c = a \cdot (b \cdot c)$$$. So instead of left "$$$(a \cdot b) \cdot c$$$" given in statement, let's use "$$$a \cdot (b \cdot c)$$$"
That is, we have $$$s_n$$$, then go to $$$s_{n-1} \cdot s_n$$$, then $$$s_{n - 2} \cdot (s_{n-1} \cdot s_n)$$$ and so on.
One can also solve the problem without observing the associativity property and going with $$$s_1 \to s_1 \cdot s_2 \to (s_1 \cdot s_2) \cdot s_3$$$ and so on. However there is one caveat. Since the string grows very fast, "an answer" will grow as well.
And while you are promised that the answer is at most $$$10^9$$$, observe the following situtation: $$$s_1, s_2, ..., s_{100}$$$ are "aaaaaaaaaaaaaaaaaaaa", which makes an answer quite large, but if you add a $$$s_{101}$$$ equal to "c" it collides to a mere $$$1$$$, so it requires some careful handling, basically store for every value $$$x$$$ you want value $$$\min(x, 10^9)$$$. Going in another direction has an advantage, that if some value is large it will stay large for life, so since answer is $$$10^9$$$ no overflows will happen.
Now back to the suggested solution.
Let's proceed as $$$s_n \to s_{n-1} \cdot s_n \to s_{n - 2} \cdot (s_{n-1} \cdot s_n)$$$ and so on.
Note, that it's enough to store not whole the current string $$$s_{i} \cdot \ldots \cdot s_n$$$, but just some basic information about. Let's simply store:
• The length of the largest substring of a single character
• Whether the string consists of the single character or not
• The left and the right character of it
• The length of the prefix, which consists of a single character and the same for the suffix.
It's easy to see that if you know such values for some string $$$s_{i} \cdot \ldots \cdot s_n$$$, you can also compute it for $$$s_{i - 1} \cdot s_{i} \cdot \ldots \cdot s_n$$$ (here, the brackets are assumed as in $$$a \cdot (b \cdot c)$$$).