Tomek thinks that smartphones are overrated.
He plans to release a new cellphone with an old-school keyboard, which may require you to tap a key multiple times to type a single letter.
For example, if the keyboard has two keys, one with the letters "adef" and the other one with the letters "zyx", then typing 'a' requires one keystroke, typing 'f' requires four keystrokes, typing 'y' requires two keystrokes, and so on.
Tomek has already designed the keyboard's layout.
That is, he already knows the number of keys on the keyboard, and for each key he knows the maximum number of letters it may hold.
He now wants to create a specific keyboard for a language that uses N different letters.
He has a large body of text in this language, and he already analyzed it to find the frequencies of all N letters of its alphabet.
You are given a int frequencies with N elements.
Each element of frequencies is the number of times one of the letters in Tomek's alphabet appears in the text he has.
Each element of frequencies will be strictly positive.
(I.e., each of the N letters occurs at least once in Tomek's text.)
You are also given a int keySize.
The number of elements of keySize is the number of keys on Tomek's keyboard.
Each element of keySize gives the maximal number of letters on one of the keys.
Find an assignment of letters to keys that minimizes the number of keystrokes needed to type Tomek's entire text.
Return that minimum number of keystrokes.
If there is not enough room on the keys and some letters of the alphabet won't fit, return -1 instead.
|Method signature:||int minKeystrokes(int frequencies, int keySizes)|
|(be sure your method is public)|
|-||frequencies will contain between 1 and 50 elements, inclusive.|
|-||Each element of frequencies will be between 1 and 1,000, inclusive.|
|-||keySizes will contain between 1 and 50 elements, inclusive.|
|-||Each element of keySizes will be between 1 and 50, inclusive.|
|Tomek's language has four letters.
Let us call them A, B, C, and D.
Tomek's text contains seven As, three Bs, four Cs, and one D.
The keyboard has two keys, each of them may contain at most two letters.
One optimal solution is to use the keys "AD" and "CB".
We can then type each A and each C using a single keystroke, and we need two keystrokes for each B and each D.
Therefore, the total number of keystrokes when typing the entire text will be 7*1 + 3*2 + 4*1 + 1*2 = 19.
|There is not enough space on the keys to fit all four letters.|
This problem statement is the exclusive and proprietary property of TopCoder, Inc. Any unauthorized use or reproduction of this information without the prior written consent of TopCoder, Inc. is strictly prohibited. (c)2010, TopCoder, Inc. All rights reserved.