#K32907. Text Justification

    ID: 24969 Type: Default 1000ms 256MiB

Text Justification

Text Justification

Given a sequence of words and a width W in characters, format the text such that each line has exactly W characters and is fully (left and right) justified. You should pack as many words as possible in each line. Extra spaces between words should be distributed as evenly as possible. If the number of spaces on a line do not divide evenly between words, the extra spaces will be distributed from left to right.

For the last line of text, it should be left-justified and no extra space is inserted between words.

Note: If a line contains only one word, it should be left-justified by adding trailing spaces. In case a single word is longer than the given width W, that word should be printed as it is without any extra spacing.

The distribution of spaces follows the formula: if a line (not the last) has k words, and the total number of letters is L, then the total spaces to insert is S = W - L. Let n = k - 1 be the number of gaps, then each gap will have at least \( \lfloor S/n \rfloor \) spaces and the leftmost \( S \bmod n \) gaps will get an extra one space. For the last line, words are separated by a single space and padded with spaces on the right to ensure a total length of W.

inputFormat

The input is read from standard input (stdin) and is in the following format:

  1. An integer N indicating the number of words.
  2. A line containing N space-separated words.
  3. An integer W specifying the width of each justified line.

outputFormat

The output should be written to standard output (stdout) and should contain the justified text. Each justified line should be printed on a new line.

## sample
7
This is an example of text justification. 
16
This    is    an

example of text justification.

</p>