Text Wrap

2.7

9 votes
Binary search algorithm, Binary Search, Input/Output, Algorithms, Basic Programming, Basics of Input/Output
Problem

Prakhar has a string with words where the length of the word is

The words are displayed in the window separated by a space. More precisely, when the sentence is displayed in a window of width , the following conditions are satisfied.

  • The first word is displayed at the beginning of the top line.
  • The word () is displayed either with a gap of  after the word, or at the beginning of the line below the line containing the word
  • The width of each line does not exceed . Here, the width of a line refers to the distance from the left end of the leftmost word to the right end of the rightmost word.
  • A word should not be broken into  or more lines 

Prakhar Wants to fit these words in or less than lines, find the minimum possible width of the window.

Input Format:

The first line contains space seperated integers - the total number of words and - the required number of lines. 

The next line contains space seperated integers  

Output Format:

Print the minimum possible width  of the window

Constraints:

Sample Input
6 4
5 4 3 6 2 4
Sample Output
8
Time Limit: 1
Memory Limit: 256
Source Limit:
Explanation

It can be proven that we cannot fit these words in 4 or less than 4 lines with width 7 or lesser.

Editor Image

?