QOJ.ac

QOJ

Time Limit: 1 s Memory Limit: 128 MB Total points: 100

#483. Aesthetic Text

统计

Let us consider a text consisting of $n$ words numbered from $1$ to $n$. We represent any of its decompositions into $k$ lines by a sequence of numbers $(a_1,a_2,…,a_{k-1})$, such that the words with numbers from $1$ to $a_1$ are in the first line, the words with numbers from $a_1 +1$ to $a_2$ are in the second line, and so on, and finally, the words with numbers from $a_{k-1} +1$ to $n$ are in the last, $k$-th line.

Each word has a certain length (measured in the number of characters). Let $\text{length}(x)$ denote the length of the word no. $x$. Furthermore, every two subsequent words in a line are separated by a space of width of a single character. By length of the line we denote the sum of lengths of the words in this line, increased by the number of spaces between them. Let $\text{line}(w)$ denote the length of the line no. w. I.e., if the line no. w contains the words with numbers from $i$ to $j$ inclusive, its length is:

$$\text{line}(2)=\text{length}(i)+\text{length}(i+1)+…+\text{length}(j)+(j-i)$$

As an example, let us consider a text consisting of 4 words of lengths 4, 3, 2 and 5, respectively, and its decomposition (1,3) into 3 lines. Then the length of the first line is 4, second - 6, and third - 5:

XXXX (1st line)

XXX XX (2nd line)

XXXXX (3rd line)

We shall refer to the number

$$ |\text{line}(1)-\text{line}(2)|+|\text{line}(2)-\text{line}(3)|+…+|\text{line}(k-1)-\text{line}(k)|$$

as the coefficient of aestheticism of a decomposition of the given text into $k$ lines. Particularly, if the decomposition has only one line, its coefficient of aestheticism is 0.

Needles to say, the smaller the coefficient, the more aesthetical the decomposition. We shall consider only these decompositions that have no line whose length exceeds some constant $m$. Of all such decompositions of a given text into any number of lines we seek the one most aesthetical, i.e. the one with the smallest coefficient of aestheticism. The aforementioned examplary decomposition's coefficient is 3, and that is exactly the minimum coefficient of aestheticism for $m=6$ and $m=7$.

Write a programme that:

  • reads from the standard input the numbers and and the lengths of the words,
  • determines the minimum coefficient of aestheticism for those decompositions, whose every line is of length not exceeding m,
  • writes the result to the standard output.

Input

The first line of the standard input contains the numbers $m$ and $n$, $1 ≤ m ≤ 1\,000\,000$, $1 ≤ n ≤ 2\,000$, separated by a single space. The second, last line of the standard input contains $n$ integers, denoting the lengths of subsequent words, $1 ≤ \text{length}(i) ≤ m$ for $i=1,2,…,n$, separated by single spaces.

Output

The first and only line of the standard output should contain exactly one integer: the minimum coefficient of aestheticism for those decompositions, whose every line's length does not exceed $m$.

Example

Input

6 4
4 3 2 5

Output

3

Input

4 2
1 2

Output

0
About Issues

We understand that our problem archive is not perfect. If you find any issues with the problem, including the statement, scoring configuration, time/memory limits, test cases, etc.

You may use this form to submit an issue regarding the problem. A problem moderator will review your issue and proceed it properly.

STOP! Before you submit an issue, please READ the following guidelines:

  1. This is not a place to publish a discussion, editorial, or requests to debug your code. Your issue will only be visible by you and problem moderators. Other users will not be able to view or reply your issues.
  2. Do not submit duplicated issues. If you have already submitted one, please wait for an moderator to review it. Submitting multiple issues will not speed up the review process and might cause your account to be banned.
  3. Issues must be filed in English or Chinese only.
  4. Be sure your issue is related to this problem. If you need to submit an issue regarding another problem, contest, category, etc., you should submit it to the corresponding page.

Active Issues 0

No issues in this category.

Closed/Resolved Issues 0

No issues in this category.