Writings e-mails is fun, but, unfortunately, they do not look very nice, mainly because not all lines have the same lengths. In this problem, your task is to write an e-mail formatting program which reformats a paragraph of an e-mail (e.g. by inserting spaces) so that, afterwards, all lines have the same length (even the last one of each paragraph).
The easiest way to perform this task would be to insert more spaces between the words in lines which are too short. But this is not the best way. Consider the following example:
****************************
This is the example you are
actually considering.
Let us assume that we want to get lines as long as the row of stars. Then, by simply inserting spaces, we would get
****************************
This is the example you are
actually considering.
But this looks rather odd because of the big gap in the second line. By moving the word ``are'' from the first to the second line, we get a better result:
****************************
This is the example you
are actually considering.
Of course, this has to be formalized. To do this, we assign a badness to each gap between words. The badness assigned to a gap of n spaces is (n - 1)^2. The goal of the program is to minimize the sum of all badnesses. For example, the badness of the first example is 1 + 7^2 = 50 whereas the badness of the second one is only 1 + 1 + 1 + 4 + 1 + 4 = 12.
In the output, every line has to start and to end with a word. (I.e. there cannot be a gap at the beginning or the end of a line.) The only exception to this is the following:
If a line contains only one word this word shall be put at the beginning of the line, and a badness of 500 is assigned to this line if it is shorter than it should be. (Of course, in this case, the length of the line is simply the length of the word.)