Raggedy, Raggedy

Time limit1sMemory limit128 MB

Problem

Consider the problem of laying out text in lines of a fixed maximum width $L$ (also known as line filling).

If you do a poor job, the ends of the lines are unnecessarily ragged. By convention, we allow the last line of a paragraph to be arbitrarily ragged — it may contain just a few characters — but we expect the earlier lines to be of approximately uniform length, filling up the column in which the text is set.

The straightforward approach of greedily filling each line with as many words as fit and then moving on does not always give the most pleasing result. For instance, at $L = 6$ the words See if we care. could be laid out as

See if
we
care.

which is arguably less pleasing than

See
if we
care.

Define a word as any sequence of non-whitespace characters bounded by a line start or end or by a blank. The whitespace characters are blanks and line-terminator characters.

Given $N$ words of widths $w_1, w_2, \ldots, w_N$ and a maximum line width $L$ (with $w_i \le L$ for every $i$), define $w(i, j)$ as the width of the line containing words $i$ through $j$ inclusive: the sum of their widths plus one blank between each adjacent pair.

$$w(i, j) = \left(\sum_{k=i}^{j} w_k\right) + (j - i)$$

The raggedness of a line containing words $i$ through $j$ is

$$r(i, j) = \bigl(L - w(i, j)\bigr)^2$$

Lay out each paragraph so that no line exceeds $L$ characters and the total raggedness, summed over every line except the last line of the paragraph, is minimized. (The final line of a paragraph may be arbitrarily shorter than the lines above it.) Line-terminator characters are not counted as part of a line's width.

Input

The input consists of one or more datasets.

Each dataset begins with a line containing a single integer $L$, the maximum line width (not counting line-terminator characters), with $0 < L \le 80$. A value of $0$ indicates the end of the input.

The rest of the dataset is a paragraph of text on up to $250$ lines, terminated by an empty line. A paragraph contains from $1$ to $500$ words, where a word is any consecutive run of non-whitespace characters. No word is longer than $L$ characters.

Output

For each paragraph, print a single line containing the minimum achievable total raggedness — the smallest possible value of $\sum r(i, j)$ taken over every line except the last, across all valid layouts in which no line exceeds $L$ characters. After each paragraph, print a line containing === (three equal signs).

Word widths are measured in characters.