Skip to main content
Tuesday (24.11.2026) at 20:00: ALGOR Contest: Round #4 (Special Challenge) starts!

~/algor $ cat problems/string-qatorlarga-bolish

Split text into lines

Medium 1000 ms 64 MB 52%
Share

The screen is $k$ characters wide. Split a text of words into lines: the words go in order, words on the same line are separated by single spaces, and no line is longer than $k$; if the next word does not fit, it starts a new line. Words are never broken (no word is longer than $k$). Print the resulting lines.

A screen of width k: the words laid out in lines, a word that did not fit moved to the next line

Input

The first line contains a positive integer $k$.
The second line contains the words separated by single spaces.

Output

The resulting lines.

Note

$1 \le k \le 1000$
the words consist of lowercase Latin letters, none longer than $k$
the length of the second line does not exceed $10^{5}$

Examples

Input #1
12
men har kuni algoritm masalalarini yechaman
Output #1
men har kuni
algoritm
masalalarini
yechaman
Input #2
5
salom
Output #2
salom

Sign in to submit a solution

Signing up is free and takes a minute.