Aesthetic Text
Time limit1sMemory limit128 MB
Split a sequence of words into lines of length at most m, minimizing the total absolute difference between consecutive line lengths.
- Level
Medium7 of 10
- Topics
- Dynamic programming, Prefix sum
- Solved
- No attempts yet
Problem
Consider a text consisting of words numbered from to . Any decomposition of the text into lines is represented by a sequence : the words numbered through go on the first line, the words numbered through go on the second line, and so on, and finally the words numbered through go on the last, -th line.
Each word has a certain length, measured in characters. Let denote the length of word number . Within a line, every two neighboring words are separated by a space one character wide. The length of a line is defined as the sum of the lengths of the words on it, increased by the number of spaces between them. Let denote the length of the -th line. That is, if the -th line contains the words numbered from to inclusive, its length is
We call the value
the coefficient of aestheticism of the decomposition. In particular, a decomposition with a single line has coefficient .
Naturally, the smaller the coefficient, the more aesthetic the decomposition. We consider only decompositions in which no line is longer than a fixed constant . Among all such decompositions of the text into any number of lines, we seek the most aesthetic one, that is, the one with the smallest coefficient of aestheticism.
For example, consider a text of words with lengths and its decomposition into lines. The first line holds word (length ), the second holds words and (), and the third holds word (length ):
XXXX
XXX XX
XXXXX
The coefficient of this decomposition is , which is exactly the smallest coefficient of aestheticism for both and .
Write a program that:
- reads , , and the lengths of the words from standard input,
- determines the smallest coefficient of aestheticism over the decompositions in which every line has length at most ,
- writes the result to standard output.
Input
The first line contains two integers and separated by a single space (, ). The second and last line contains integers, the lengths of the successive words, separated by single spaces, with for every .
Output
The first and only line of standard output should contain exactly one integer: the smallest coefficient of aestheticism over the decompositions in which no line has length greater than .