I would come up, these are two stages, first build an approximate solution, and then improve this solution.
First, scroll through the list of lines and assign each line in turn to the page with the largest remaining space. You might want to check if there is enough space to redistribute the last lines of a page to earlier pages, so reduce the number of pages needed.
Secondly, select the pages with the largest and least free space. Change the shorter line with one with the longer line from the other so that the space remaining on the two pages is closer. Repeat (make sure you don't end up in an endless loop) until you have enough reasonable balance on all pages.
This is an approximate solution, not an exact one, but it should be able to get a reasonable result pretty quickly.
source share