eolymp
bolt
Спробуйте наш новий інтерфейс для відправки розв'язків
Задачі

Beautiful Spacing

Beautiful Spacing

Text is a sequence of words, and a word consists of characters. Your task is to put words into a grid with \textbf{W}columns and sufficiently many lines. For the beauty of the layout, the following conditions have to be satisfied. \textbf{1}.The words in the text must be placed keeping their original order. The following figures show correct and incorrect layout examples for a 4 word text "\textbf{This is a pen}" into \textbf{11} columns. \textbf{2}. Between two words adjacent in the same line, you must place at least one space character. You sometimes have to put more than one space in order to meet other conditions. \includegraphics{https://static.e-olymp.com/content/1a/1abe47db5cf474c76b2fe0a2eb1db661814ae575.jpg} \textit{\textbf{Figure 3}}: BAD -- Words must be separated by spaces. \textbf{3}. A word must occupy the same number of consecutive columns as the number of characters in it. You cannot break a single word into two or more by breaking it into lines or by inserting spaces. \includegraphics{https://static.e-olymp.com/content/21/2162b592479b45fd0335cadaed14f5e5763d51c8.jpg} \textit{\textbf{Figure 4}}: BAD -- Characters in a single word must be contiguous. \textbf{4}. The text must be justified to the both sides. That is, the first word of a line must start from the first column of the line, and except the last line, the last word of a line must end at the last column. \includegraphics{https://static.e-olymp.com/content/6a/6af09e27ff7d4d8fa6217960a171fb96f97a1275.jpg} \textit{\textbf{Figure 5}}: BAD -- Lines must be justified to both the left and the right sides. The text is the most beautifully laid out when there is no unnecessarily long spaces. For instance, the layout in \textit{\textbf{Figure 6}} has at most \textbf{2} contiguous spaces, which is more beautiful than that in \textit{\textbf{Figure 1}}, having \textbf{3} contiguous spaces. Given an input text and the number of columns, please find a layout such that the length of the longest contiguous spaces between words is minimum. \includegraphics{https://static.e-olymp.com/content/eb/eb2595200f393b71db97594c4f5850a372a70930.jpg} \textit{\textbf{Figure 6}}: A good and the most beautiful layout. \InputFile The input consists of multiple datasets, each in the following format. \textbf{W N} \textbf{x_1 x_2 ... x_N} \textbf{W}, \textbf{N}, and \textbf{x_i} are all integers. \textbf{W} is the number of columns (\textbf{3} ≤ \textbf{W} ≤ \textbf{80000}). \textbf{N} is the number of words (\textbf{2} ≤ \textbf{N} ≤ \textbf{50000}). \textbf{x_i} is the number of characters in the \textbf{i}-th word (\textbf{1} ≤ \textbf{x_i} ≤ \textbf{(W-1)/2}). Note that the upper bound on \textbf{x_i} assures that there always exists a layout satisfying the conditions. The last dataset is followed by a line containing two zeros. \OutputFile For each dataset, print the smallest possible number of the longest contiguous spaces between words.
Ліміт часу 15 секунд
Ліміт використання пам'яті 128 MiB
Вхідні дані #1
11 4
4 2 1 3
5 7
1 1 1 2 2 1 2
11 7
3 1 3 1 3 3 4
100 3
30 30 39
30 3
2 5 3
0 0
Вихідні дані #1
2
1
2
40
1
Джерело ACM International Collegiate Programming Contest, Asia Regional Contest, Tokyo, 2012-11-18