Pekgalsvalsen
InterviewTime limit2sMemory limit1024 MB
Given a string with K distinct letters, choose the order to write the letters so that the total right-arrow distance is minimized, and print that minimum.
Problem
Doris is writing a long email to her family. The text consists of different letters and is letters long. Doris has a poor memory, so she cannot remember where the letters are on the keyboard. Instead she uses a variant of the so-called pekgalsvalsen when she types.
Doris types her text in rounds, one round for each of the different letters that make up the text. First Doris writes down all occurrences of one of the letters with one of her gills. Then she goes back to the beginning of the text and picks a new letter. She then writes all occurrences of this letter, using the right arrow key with her other gill. After that she goes back to the beginning of the text again, now to write down the third letter, and so on. She repeats this until she has written the whole text. In this way she only needs to remember where the key for a single letter is at a time.
Depending on the order in which she chooses to write the letters, this can take different amounts of time. If she is to write the text aabbac, the order a, b, c requires her to press the right arrow key 7 times. First she writes aaa without using the right arrow. Then she goes back to the text, uses the right arrow twice and writes bb. Finally she goes back to the beginning, goes right five times and writes c.
If she instead chose the order b, a, c, she would first have written bb. Then she would have used two right presses to write aabba, and finally five more times to write aabbac.
The order c, b, a requires her to press the right arrow only twice, which is optimal.
Input
The first line contains the positive integers and . The next line contains a string of characters, chosen among the first lowercase letters of the alphabet (a, b, c, ...).
Output
Print a single number: the number of times Doris must press the right arrow to write the text.