Under construction

Still being built. Things may change

Simple Prefix Compression

Interview

Time limit1sMemory limit128 MB

Summary
Sum stored lengths after prefix compression: each string stores a control byte plus the part not shared as a prefix with the previous string.
Level

Easy3 of 10

Topics
String, Implementation
Solved
No attempts yet

Problem

Many databases store character fields (and especially indices) using prefix compression. This technique compresses a sequence of strings A1,A2,…,ANA_1, A_2, \ldots, A_N as follows.

The first string A1A_1 is stored in full. For every subsequent string Ai+1A_{i+1}, let jj be the length of the longest common prefix it shares with the previous string AiA_i — the largest j≤min⁡(p,q)j \le \min(p, q) such that the first jj characters of Ai=ai,1ai,2⋯ai,pA_i = a_{i,1} a_{i,2} \cdots a_{i,p} and Ai+1=ai+1,1ai+1,2⋯ai+1,qA_{i+1} = a_{i+1,1} a_{i+1,2} \cdots a_{i+1,q} are equal. Then Ai+1A_{i+1} is stored as a single control character whose code is jj, followed by the remaining characters ai+1,j+1ai+1,j+2⋯ai+1,qa_{i+1,j+1} a_{i+1,j+2} \cdots a_{i+1,q}. Its stored length is therefore 1+(q−j)1 + (q - j).

If j=0j = 0 (the two strings have no common prefix), the string is still prefixed by one control byte, so its stored length is one greater than its original length.

Compute the minimal total stored length of the whole sequence.

Input

The first line contains an integer NN. Each of the next NN lines contains one string AiA_i.

Output

Print a single integer: the minimal total length of the compressed strings.

Constraints

  • 1≤N≤100001 \le N \le 10000
  • 1≤length⁡(Ai)≤2551 \le \operatorname{length}(A_i) \le 255

Examples2

  1. Example 1

    Input
    3
    abc
    atest
    atext
    
    Expected output
    11
    
  2. Example 2

    Input
    2
    test
    notest
    
    Expected output
    11