Codeforces Beta Round 72 (Div. 1 Only)
E
Two Subsequences
Codeforces
2800
原题
← D→
翻译中…

On an IT lesson Valera studied data compression. The teacher told about a new method, which we shall now describe to you.

翻译中…

Let ${*a*_{1}, *a*_{2}, ..., *a*_{*n*}}$ be the given sequence of lines needed to be compressed. Here and below we shall assume that all lines are of the same length and consist only of the digits $0$ and $1$. Let's define the compression function:

翻译中…
  • $*f*($empty sequence$) =$empty string
  • $*f*(*s*) = *s*$.
  • $*f*(*s*_{1}, *s*_{2}) =$ the smallest in length string, which has one of the prefixes equal to $*s*_{1}$ and one of the suffixes equal to $*s*_{2}$. For example, $*f*(001, 011) = 0011$, $*f*(111, 011) = 111011$.
  • $*f*(*a*_{1}, *a*_{2}, ..., *a*_{*n*}) = *f*(*f*(*a*_{1}, *a*_{2}, *a*_{*n* - 1}), *a*_{*n*})$. For example, $*f*(000, 000, 111) = *f*(*f*(000, 000), 111) = *f*(000, 111) = 000111$.
翻译中…

Valera faces a real challenge: he should divide the given sequence ${*a*_{1}, *a*_{2}, ..., *a*_{*n*}}$ into two subsequences ${*b*_{1}, *b*_{2}, ..., *b*_{*k*}}$ and ${*c*_{1}, *c*_{2}, ..., *c*_{*m*}}$, $*m* + *k* = *n*$, so that the value of $*S* = |*f*(*b*_{1}, *b*_{2}, ..., *b*_{*k*})| + |*f*(*c*_{1}, *c*_{2}, ..., *c*_{*m*})|$ took the minimum possible value. Here $|*p*|$ denotes the length of the string $*p*$.

翻译中…

Note that it is not allowed to change the relative order of lines in the subsequences. It is allowed to make one of the subsequences empty. Each string from the initial sequence should belong to exactly one subsequence. Elements of subsequences $*b*$ and $*c*$ don't have to be consecutive in the original sequence $*a*$, i. e. elements of $*b*$ and $*c*$ can alternate in $*a*$ (see samples 2 and 3).

翻译中…

Help Valera to find the minimum possible value of $*S*$.

翻译中…

Input

翻译中…

The first line of input data contains an integer $*n*$ — the number of strings ($1 ≤ *n* ≤ 2·10^{5}$). Then on $*n*$ lines follow elements of the sequence — strings whose lengths are from $1$ to $20$ characters, consisting only of digits $0$ and $1$. The $*i* + 1$-th input line contains the $*i*$-th element of the sequence. Elements of the sequence are separated only by a newline. It is guaranteed that all lines have the same length.

翻译中…

Output

翻译中…

Print a single number — the minimum possible value of $*S*$.

翻译中…

Note

翻译中…

Detailed answers to the tests:

翻译中…
  • The best option is to make one of the subsequences empty, and the second one equal to the whole given sequence. $|*f*(01, 10, 01)| = |*f*(*f*(01, 10), 01)| = |*f*(010, 01)| = |0101| = 4$.
  • The best option is: $*b* = {000, 001}, *c* = {111, 110}.$ $*S* = |*f*(000, 001)| + |*f*(111, 110)| = |0001| + |1110| = 8$.
  • The best option is: $*b* = {10101, 01010, 01000}, *c* = {11111, 10010}.$ $*S* = |10101000| + |111110010| = 17$.
翻译中…

样例

Input #1
3
01
10
01
Output #1
4
InputCopy OutputCopy
Input #2
4
000
111
110
001
Output #2
8
InputCopy OutputCopy
Input #3
5
10101
01010
11111
01000
10010
Output #3
17
InputCopy OutputCopy
题目信息
题号
83E
时间限制
2 s
内存限制
256 MB
标签
位运算
动态规划

题面与样例版权归 Codeforces 及出题人所有;中文翻译由 AI 生成(CI 管线),如有出入请以原文为准。