This page is still under construction.

Parts of this page are still being built. What you see may change.

Type Printer

Time limit1sMemory limit128 MB

Summary
Find the minimum number of add, remove, and print operations to type N distinct words on a printer that keeps a single editable string, with any print order allowed.
Level

Medium7 of 10

Topics
Trie, DFS, Tree, Greedy
Solved
No attempts yet

Problem

You need to print NN words on a movable type printer. A movable type printer is one of those old printers that require you to place small metal pieces (each engraved with a single letter) in order to form a word, after which a sheet of paper is pressed against them to print the word. The printer you have supports the following three operations:

  • Add a single letter to the end of the word currently in the printer.
  • Remove the last letter from the word currently in the printer. This is allowed only if there is at least one letter in the printer.
  • Print the word currently in the printer.

Initially the printer is empty; it contains no letter pieces. After all printing is done, you are allowed to leave some letters in the printer. You may also print the words in any order you like.

Every operation takes time, so you want to minimize the total number of operations.

Given the NN words you want to print, write a program that finds the minimum number of operations needed to print all the words in some order.

Input

The first line contains the integer NN, the number of words to print. (1≤N≤250001 \le N \le 25000)

Each of the next NN lines contains one word. Each word consists only of lowercase letters ('a'–'z') and has length between 11 and 2020, inclusive. All words are distinct.

Output

Print a single integer MM: the minimum number of operations required to print all the words.

Examples1

  1. Example 1

    Input
    3
    print
    the
    poem
    
    Expected output
    20