Aesthetic Text

No attempts yetTime limit1sMemory limit128 MB

Problem

Consider a text consisting of nn words numbered from 11 to nn. Any decomposition of the text into kk lines is represented by a sequence (a1,a2,,ak1)(a_1, a_2, \dots, a_{k-1}): the words numbered 11 through a1a_1 go on the first line, the words numbered a1+1a_1 + 1 through a2a_2 go on the second line, and so on, and finally the words numbered ak1+1a_{k-1} + 1 through nn go on the last, kk-th line.

Each word has a certain length, measured in characters. Let length(x)length(x) denote the length of word number xx. Within a line, every two neighboring words are separated by a space one character wide. The length of a line is defined as the sum of the lengths of the words on it, increased by the number of spaces between them. Let line(w)line(w) denote the length of the ww-th line. That is, if the ww-th line contains the words numbered from ii to jj inclusive, its length is

line(w)=length(i)+length(i+1)++length(j)+(ji)line(w) = length(i) + length(i+1) + \dots + length(j) + (j - i)

We call the value

line(1)line(2)+line(2)line(3)++line(k1)line(k)|line(1) - line(2)| + |line(2) - line(3)| + \dots + |line(k-1) - line(k)|

the coefficient of aestheticism of the decomposition. In particular, a decomposition with a single line has coefficient 00.

Naturally, the smaller the coefficient, the more aesthetic the decomposition. We consider only decompositions in which no line is longer than a fixed constant mm. Among all such decompositions of the text into any number of lines, we seek the most aesthetic one, that is, the one with the smallest coefficient of aestheticism.

For example, consider a text of 44 words with lengths 4,3,2,54, 3, 2, 5 and its decomposition (1,3)(1, 3) into 33 lines. The first line holds word 11 (length 44), the second holds words 22 and 33 (3+2+1=63 + 2 + 1 = 6), and the third holds word 44 (length 55):

XXXX
XXX XX
XXXXX

The coefficient of this decomposition is 46+65=3|4 - 6| + |6 - 5| = 3, which is exactly the smallest coefficient of aestheticism for both m=6m = 6 and m=7m = 7.

Write a program that:

  • reads mm, nn, and the lengths of the words from standard input,
  • determines the smallest coefficient of aestheticism over the decompositions in which every line has length at most mm,
  • writes the result to standard output.

Input

The first line contains two integers mm and nn separated by a single space (1m1,000,0001 \le m \le 1{,}000{,}000, 1n2,0001 \le n \le 2{,}000). The second and last line contains nn integers, the lengths of the successive words, separated by single spaces, with 1length(i)m1 \le length(i) \le m for every i=1,2,,ni = 1, 2, \dots, n.

Output

The first and only line of standard output should contain exactly one integer: the smallest coefficient of aestheticism over the decompositions in which no line has length greater than mm.