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−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

Solution in C++14 (judge verdict: Accepted)

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef pair<ll, ll> l_l;
typedef pair<int, int> i_i;
template<class T>
inline bool chmax(T &a, T b) {
    if(a < b) {
        a = b;
        return true;
    }
    return false;
}

template<class T>
inline bool chmin(T &a, T b) {
    if(a > b) {
        a = b;
        return true;
    }
    return false;
}

const long double EPS = 1e-10;
const long long INF = 1e18;
const long double PI = acos(-1.0L);
//const ll mod = 1000000007;
ll W, N;
ll x[60000];
ll xsum[2][60000];
ll imos[60000];
bool f(int border) {
    //cerr << "---------" << border << "------" << endl;
    for(int i = 0; i < 60000; i++) {
        imos[i] = 0;
    }
    xsum[0][0] = 0;
    xsum[1][0] = 0;
    for(int i = 0; i < N; i++) {
        xsum[0][i+1] = xsum[0][i] + x[i] + 1;
        xsum[1][i+1] = xsum[1][i] + x[i] + border;
    }
    imos[0] = 1;
    imos[1] = -1;
    ll now = 0;
    for(int idx = 0; idx < N; idx++) {
        now += imos[idx];
        if(now == 0) continue;
        //cerr << "-" << idx << "-" << endl;
        if(xsum[0][N] - xsum[0][idx] <= W + 1) return true;
        int ok = N + 1;
        int ng = idx;
        while(abs(ok - ng) > 1) {
            int mid = (ok + ng) / 2;
            if(xsum[1][mid] - xsum[1][idx] >= W + border) ok = mid;
            else ng = mid;
        }
        imos[ok]++;
        ng = idx;
        ok = N + 1;
        while(abs(ok - ng) > 1) {
            int mid = (ok + ng) / 2;
            if(xsum[0][mid] - xsum[0][idx] > W + 1) ok = mid;
            else ng = mid;
        }
        imos[ok]--;
    }
    return false;
}


void solve() {
    for(int i = 0; i < N; i++) {
        cin >> x[i];
    }
    int ok = W;
    int ng = 0;
    while(abs(ok - ng) > 1) {
        int mid = (ok + ng) / 2;
        if(f(mid)) {
            ok = mid;
        } else ng = mid;
    }
    cout << ok << endl;
}

int main() {
    //cout.precision(10);
    cin.tie(0);
    ios::sync_with_stdio(false);
    while(cin >> W >> N) {
        if(W == 0) break;
        solve();
    }
    return 0;
}