Page Splitting

Revision en1, by tvvtdddlbvdlbdns, 2025-12-03 11:53:16

Problem – Pagination

You are given a text consisting of n words, numbered from 1 to n. The i-th word has length a[i].

We want to arrange these words into lines and then into pages.

Each line has maximum length L. A line consists of one or more consecutive words. The total length of all words on a line must not exceed L. Each page can contain at most k lines. The words must appear in order; you cannot reorder them.

First, you split the sequence of words into lines. Then, you split the sequence of lines into pages, with up to k lines per page.

There is one strict rule: Among all possible ways to paginate the text, you must choose one that uses the minimum possible number of pages.

Now we define the “penalty”:

For a line whose words have total length s, its line penalty is L — s. For a page, its page penalty is the maximum line penalty among all lines on that page. The total penalty of a pagination is the sum of the penalties of all pages.

Your task: Among all paginations that use the minimum possible number of pages, find the minimal possible total penalty.

Input

The first line contains three positive integers n, L, k: - n – number of words - L – maximum length of a line - k – maximum number of lines per page

Each of the next n lines contains a positive integer a[i] – the length of the i-th word.

Output

Print a single integer: the minimal total penalty of a pagination that uses the minimum possible number of pages.

Example

Input

 6 10 3 4 5 9 7 8 6 

Output

 5 
That's the problem. My teacher solved with dp[i][j][k]: ith word, jth line, k penalty point. I don't understand my teacher's solution, but he said: "The case that one word place in line i worst than if it place in line i + 1 will make my solution wrong", Can someone solve this problem and explain it for me, because i dont understand him and he also say his solution has some counter. Thanks Sorry for my bad English!
Tags dp, greedy

History

 
 
 
 
Revisions
 
 
  Rev. Lang. By When Δ Comment
en1 English tvvtdddlbvdlbdns 2025-12-03 11:53:16 2017 Initial revision (published)