#P9946. Bessie's Essay Formatting
Bessie's Essay Formatting
Bessie's Essay Formatting
Bessie is writing an essay containing \(N\) words. Each word is composed only of uppercase and lowercase letters and has a length between \(1\) and \(15\). Her assignment requires that the essay be formatted in a special way: each line may contain at most \(K\) characters, excluding spaces.
The text editor works as follows:
- If the next word can be appended to the current line without the total number of characters (sum of word lengths, ignoring spaces) exceeding \(K\), then it is added to the current line.
- Otherwise, the word is placed at the beginning of the next line.
Words on the same line are separated by a single space and there should be no trailing spaces at the end of any line.
Constraints: \(1 \le N \le 100\) and \(1 \le K \le 80\).
inputFormat
The first line contains two integers, \(N\) and \(K\), where \(N\) is the number of words in the essay and \(K\) is the maximum number of characters allowed per line (excluding spaces).
The second line contains \(N\) words separated by a single space.
outputFormat
Print the formatted essay. Each line should display the words separated by a single space, and no line should end with a space.
sample
6 10
Hello world this is Bessie awesome
Hello world
this is
Bessie
awesome
</p>