Problem:


Problem I: Beautiful Spacing

Text is a sequence of words, and a word consists of characters. Your task is to put words into a grid with W columns and sufficiently many lines. For the beauty of the layout, the following conditions have to be satisfied.

The words in the text must be placed keeping their original order. The following figures show correct and incorrect layout examples for a 4 word text "This is a pen" into 11 columns.

Figure I.1: A good layout.

Figure I.2: BAD | Do not reorder words.

Between two words adjacent in the same line, you must place at least one space character. You sometimes have to put more than one space in order to meet other conditions.

Figure I.3: BAD | Words must be separated by spaces.

A word must occupy the same number of consecutive columns as the number of characters in it. You cannot break a single word into two or more by breaking it into lines or by inserting spaces.

Figure I.4: BAD | Characters in a single word must be contiguous.

The text must be justified to the both sides. That is, the first word of a line must startfrom the first column of the line, and except the last line, the last word of a line must end at the last column.

Figure I.5: BAD | Lines must be justied to both the left and the right sides.

The text is the most beautifully laid out when there is no unnecessarily long spaces. For instance, the layout in Figure I.6 has at most 2 contiguous spaces, which is more beautiful than that in Figure I.1, having 3 contiguous spaces. Given an input text and the number of columns, please find a layout such that the length of the longest contiguous spaces between words is minimum.

Figure I.6: A good and the most beautiful layout.

Input

The input consists of multiple datasets, each in the following format.

    W N

    x1 x2 ... xN

W, N, and xi are all integers. W is the number of columns (3 ≤ W ≤ 80,000). N is the number of words (2 ≤ N ≤ 50,000). xi is the number of characters in the i-th word (1 ≤ xi ≤ (W&minus;1)/2 ). Note that the upper bound on xi assures that there always exists a layout satisfying the conditions.

The last dataset is followed by a line containing two zeros.

Output

For each dataset, print the smallest possible number of the longest contiguous spaces between words.

Sample Input

11 4
4 2 1 3
5 7
1 1 1 2 2 1 2
11 7
3 1 3 1 3 3 4
100 3
30 30 39
30 3
2 5 3
0 0

Output for the Sample Input

2
1
2
40
1



Sample input:
11 4
4 2 1 3
5 7
1 1 1 2 2 1 2
11 7
3 1 3 1 3 3 4
100 3
30 30 39
30 3
2 5 3
0 0


Sample output:
2
1
2
40
1

Submission status: Wrong Answer

Solution code:
while 1:
    W, N = map(int, input().split())
    if W == N == 0:
        break
    *X, = map(int, input().split())
    def solve(l):
        lower = 0; upper = 0
        for x in X:
            lower += x + 1
            upper += x + l
            if l + W <= upper:
                if not lower <= W + 1:
                    return 0
                lower = upper = 0
        return 1
    left = 0; right = W
    while left + 1 < right:
        mid = (left + right) // 2
        if solve(mid):
            right = mid
        else:
            left = mid
    print(right)

Submission status: Accepted

Solution code:
import sys
readline = sys.stdin.readline
write = sys.stdout.write
def check(W, N, k, su, s):
    vs = [0]*(N+2)
    vs[1] = r = 1
    p = q = 0
    for i in range(N):
        while (su[i+1] - su[p]) + (i - p)*s >= W:
            p += 1
        while (su[i+1] - su[q]) + (i - q) > W:
            q += 1
        vs[i+2] = r = r + (vs[p] > vs[q])
        if r == vs[q]:
            return k < i+2 and r - vs[k]
    return vs[N+1] - vs[k]
def solve():
    W, N = map(int, readline().split())
    if W == N == 0:
        return False
    *X, = map(int, readline().split())
    su = [0]*(N+1)
    for i in range(N):
        su[i+1] = su[i] + X[i]
    k = N
    while k > 0 and su[N] - su[k-1] + (N-k+1) <= W:
        k -= 1

    ma = 0
    cu = X[0]; c = 1
    for i in range(1, N):
        if cu + c + X[i] > W:
            ma = max(ma, (W - cu + c-2) // (c-1))
            cu = X[i]
            c = 1
        else:
            cu += X[i]
            c += 1

    left = 0; right = ma+1
    while left+1 < right:
        mid = (left + right) >> 1
        if check(W, N, k, su, mid):
            right = mid
        else:
            left = mid
    write("%d\n" % right)
    return True
while solve():
    ...