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++ (judge verdict: Time Limit Exceeded)

#include<bits/stdc++.h>
using namespace std;
typedef long long ll;

struct BIT{
  vector<ll> t;
  void init(int n){
    t.clear();
    t.resize(n+1);
  }
  
  ll sum(int i){
    ll res=0;
    while(0<i){
      res+=t[i];
      i-=(i&-i);
    }
    return res;
  }
  ll sum(int a,int b){
    if(a<=1)return sum(b);
    else return sum(b)-sum(a-1);
  }
  void add(int i,ll x){
    while(i<(int)t.size()){
      t[i]+=x;
      i+=(i&-i);
    }
  }
};

#define MAX_N 50005
int W,N;
ll x[MAX_N];
BIT X,DP;

int judge(int l,int r,int M){
  if(l==r)return -1;
  
  ll d=X.sum(l,r);
  if(d+(r-l)>W)return 1;
  if(d+(r-l)*M<W)return -1;
  return 0;
}

int getlower(int i,int M){
  int left=i+1,right=N,mid;
  int cc=0;
  while(left<right && ++cc<30){
    mid=(left+right)/2;
    if(judge(i,mid,M)==-1)left=mid+1;
    else right=mid;
  }
  return left;
}

int getupper(int i,int M){
  int left=i+1,right=N+1,mid;
  int cc=0;
  while(right-left>1 && ++cc<30){
    mid=(left+right)/2;
    if(judge(i,mid,M)<=0)left=mid;
    else right=mid;
  }
  return left;
}

bool check(int M){
  DP.init(N+1);
  DP.add(N+1,1);
  
  for(int i=N;i>=1;i--){

    if(X.sum(i,N)+N-i<=W){
      DP.add(i,1);
      continue;
    }

    int fi=getlower(i,M);
    int se=getupper(i,M);
    if( DP.sum(fi+1,se+1) > 0 )
      DP.add(i,1);

    //    cout<<i<<' '<<DP.sum(i,i)<<endl;
  }
  return (DP.sum(1)==1);
}

int main(){
  while(1){
    scanf("%d %d",&W,&N);
    if(W==0&&N==0)break;
    X.init(N+5);
    for(int i=1;i<=N;i++){
      scanf("%lld",&x[i]);
      X.add(i,x[i]);
    }
    
    int L=1,R=W-2,M;
    int cc=0;
    while(L<R && ++cc<30){
      M=(L+R)/2;
      if(check(M))R=M;
      else L=M+1;
    }

    printf("%d\n",L);
    
  }
  return 0;
}