Editorial
Let . If there are lines, their lengths are , so
For , set . A split exists exactly when is a positive multiple of , and then .
Since , we need . Check such values of from largest to smallest and choose the first valid one. Then print consecutive pieces of of lengths . A single line is possible only when , so a string of length has no answer.
There are candidate values of , and printing takes time. Space usage including the input string is .
Solution written by GPT6