Report
InterviewTime limit2sMemory limit1024 MB
Split two word lists by a vertical line on a w-wide roll so the greedy line-wrapping needs the fewest rows, then print that row count.
- Level
Medium5 of 10
- Topics
- Binary search, Greedy, Simulation
- Solved
- No attempts yet
Statement
Vers needs to prepare a report on her last combat sortie. She has already composed the text in her head, and only has to write it down. The report has two parts: the first contains words, the -th of which has letters, and the second contains words, the -th of which has letters. The Kria language has no punctuation marks. Vers must write the report on a squared paper roll that is cells wide. Since the report has two parts, she draws a vertical line across the roll, splitting it into two parts of whole width, then writes the first part on the left and the second part on the right.
Both parts are written in the same way, each on its own part of the roll. One letter takes exactly one cell. The first word is written on the first line of the roll, starting from the leftmost cell of that part. Each following word is written on the same line as the previous word if it fits, separated from it by exactly one empty cell. Otherwise it goes on the next line, starting from the leftmost cell. If the width of a part is less than the length of some word that must be written in it, writing that part on a roll of that width is impossible.
It is guaranteed that a vertical line can be drawn so that both parts can be written. Vers wants to draw the line so that the length of the roll needed to write the report is minimal. Find this minimal length.
Input
The first line contains three integers , , and , where is the width of the roll () and , are the numbers of words in the first and second parts ().
The next line contains integers , the length of the -th word of the first part ().
The next line contains integers , the length of the -th word of the second part ().
It is guaranteed that a vertical line can be drawn so that both parts can be written.
Output
Print one integer, the minimal length of the roll that is enough to write the report.
Hint
In the sample test, the roll is split between the 7th and 8th columns of cells, and then two words are written on each line in both parts of the report.