Problem B
Word Clouds Revisited
When we last visited with Tagg Johnson, he was developing software to create images known as word clouds. Although we will not consider all the details, a word cloud provides a visual representation of textual data in which the size of the bounding box for each word depends on the relative frequency of that word in the original text.
Tagg was surprised to stumble upon an oddity involving his algorithm for laying out words in rows. A maximum width is specified for the size of a word cloud, and Tagg’s desire is to place the entries into the cloud in a way that minimizes the overall height. Entries must be placed in a predetermined order, with each subsequent entry either placed horizontally to the right of the previous entry, or else to the far left of a new row. The height of each row is equal to the maximum height of all entries placed in that row, and the overall height of the cloud is equal to the sum of the row heights.
Because his goal is to minimize the height, Tagg’s original
algorithm would place each entry in the same row as the
previous entry, unless it did not physically fit because of the
given limit on the width of the cloud. As an example,
Figure 1 shows the layout Tagg’s original algorithm
produces for a particular cloud with a maximum width of
![\includegraphics[width=3.5in]{greedy.png}](/problems/wordclouds/file/statement/en/img-0001.png)
The first, second, and third entries are placed in the first
row (totaling width
However, Tagg later realized that an even better cloud was
possible for this same data set, as shown in Figure 2. By
placing the first and second entries in the first row, the
third and fourth entries in the second row, and the fifth and
sixth entries in a third row, the overall height of the cloud
is only
![\includegraphics[width=3.5in]{best.png}](/problems/wordclouds/file/statement/en/img-0002.png)
So now Tagg wants to redesign his software so that, given a list of words and a specific maximum width, it builds a word cloud with the minimum height.
Input
The first line contains two integers,
Output
Output the minimum height that is required for the word cloud under the restrictions given above.
Sample Input 1 | Sample Output 1 |
---|---|
6 260 65 23 38 11 135 48 97 43 95 28 130 23 |
99 |
Sample Input 2 | Sample Output 2 |
---|---|
3 309 150 100 10 10 150 100 |
200 |