Raggedy, Raggedy

Time limit1sMemory limit128 MB

Summary
Given word widths and a max line length, split the words into lines and minimize the sum of squared unused space on every line except the last.
Level

Medium6 of 10

Topics
Dynamic programming, Prefix sum, Implementation, Math
Solved
No attempts yet

Problem

Consider the problem of laying out text in lines of a fixed maximum width LL (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=6L = 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 NN words of widths w1,w2,…,wNw_1, w_2, \ldots, w_N and a maximum line width LL (with wi≤Lw_i \le L for every ii), define w(i,j)w(i, j) as the width of the line containing words ii through jj inclusive: the sum of their widths plus one blank between each adjacent pair.

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

The raggedness of a line containing words ii through jj is

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

Lay out each paragraph so that no line exceeds LL 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 LL, the maximum line width (not counting line-terminator characters), with 0<L≤800 < L \le 80. A value of 00 indicates the end of the input.

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

Output

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

Word widths are measured in characters.

Examples2

  1. Example 1

    Input
    6
    See if we
    care.
    
    25
    Raggedy, raggedy are we.
    Just as raggedy as raggedy can be.
    We don’t get nothin’ for our labor.
    So raggedy, raggedy are we.
    - P Seeger
    
    0
    
    Expected output
    10
    ===
    138
    ===
    
  2. Example 2

    Input
    3
    ab cd
    
    0
    
    Expected output
    1
    ===