Go: package main

import (
	"bufio"
	"os"
	"strings"
	"strconv"
	"fmt"
	)

var scanner = bufio.NewScanner(os.Stdin)
const MAX_WIDTH = 1500000

func main() {
	// 二分探索で探す
	// 二分探索はソート済みの配列に対して、特定の条件を満たす値を
	// 効率よく選択するアルゴリズム
	for scanner.Scan() {
		buf := strings.Split(scanner.Text(), " ")
		m, _ := strconv.Atoi(buf[0])
		n, _ := strconv.Atoi(buf[1])
		if n == 0 && m == 0 {
			break
		}
		books := make([]int, n)
		for i := 0; i < n; i++ {
			scanner.Scan()
			books[i], _ = strconv.Atoi(scanner.Text())
		}
		check := func(width int) bool {
			// widthの幅の本棚に順番にbooksを詰めて行ったら
			// 何段になるのかを確認し、それがmを以下であることを確認する
			shelf_index := 1
			position_in_stage := 0
			for i := 0; i < n; i++ {
				// 入れることができない本がある時点でこのwidthは不採用
				if books[i] > width {
					return false
				}
				// 新たに本を追加することができるかをチェックする
				if position_in_stage + books[i] <= width {
					// 新たに本を入れることができたので、そのステージにおける
					// 現在の本の位置を記録
					position_in_stage += books[i]
				} else {
					// 新たな本を入れることができなかったので、一段下のステージに
					// 入れれば良い
					position_in_stage = books[i]
					shelf_index++ // 本棚のステージ数が増えた
				}
			}
			return shelf_index <= m // 条件を満たすかの確認
		}
		low, high := 0, MAX_WIDTH
		for low < high {
			mid := (low+high)/2
			if check(mid) {
				high = mid
			} else {
				low = mid+1
			}
			// log.Printf("low: %d, high: %d\n", low, high)
		}
		fmt.Println(low)
	}
}

C++: #include <iostream>
#include <map>
using namespace std;

int m,n,*b,*l,mp;
map<int,int> hk;

int max(){
	int tmp = l[0];
	for(int i=1; i<m; i++)
		if(tmp < l[i])
			tmp = l[i];
	return tmp;
}

void solve(){
	mp = 0;
	int k;
	l[0] = 0;
	for(int i=0; i<n; i++){
		k = i+1;
		l[0] += b[i];
		for(int j=1; j<m-1; j++){
			l[j] = 0;
			for(; k<n; k++){
				l[j] += b[k];
				if(k == n-1){
					k++;
					break;
				}
				if((l[0] - l[j] >= 0 && l[0] - l[j] < l[j] + b[k+1] - l[0];)||(l[j] >= l[0])){
					k++;
					break;
				}
			}
		}
		l[m-1] = 0;
		for(; k<n; k++)
			l[m-1] += b[k];
		hk[mp++] = max();
	}
}

int min(){
	int tmp = hk[0];
	map<int, int>::iterator it = hk.begin();
	while(it!=hk.end()){
		if(tmp > (*it).second)
			tmp = (*it).second;
		it++;
	}
	return tmp;
}

int main(){
	while(true){
		cin >> m >> n;
		if(!m && !n)
			return 0;
		b = new int[n];
		l = new int[m];
		for(int i=0; i< n; i++)
			cin >> b[i];
		solve();
		cout << min() << endl;
		delete[] b;
		delete[] l;
		hk.clear();
	}
}

C++: //2?????¢?´¢????????£????§£????????????????????????????????§??????
#include<cstdio>
#include<iostream>

using namespace std;

int gcd(int m, int n)
{
	if ((0 == m) || (0 == n))
		return 0;
	while (m != n)
	{
		if (m > n) m = m - n;
		else         n = n - m;
	}
	return m;
}




int main(void) {

	
	int floor, n, book[101] = {0};
	cin >> floor >> n;

	if (floor == 0)return 0;

	for (int i = 0; i < n; i++) {
		cin >> book[i];
	}

	//?????§??¬?´???°????±???????
	int m=book[0];
	int w_avr = book[0];
	for (int i = 1; i < n; i++) {
		m = gcd(m, book[i]);
		w_avr += book[i];
	}
	//?????????????????§?????¬??????????????????????????°??§?????£?????°??????????????¬????????§?????????
	int ans= w_avr/floor;

	for (int i = 0; i < n; i++) {
		if (ans < book[i]) {
			ans = book[i];
		}
	}
	while (ans%m != 0) {
		ans++;
	}
	while (1) {
		int count = floor;
		int length = ans;
		int i = 0;
		while (count != 0&&i<n) {
			if (length >= book[i]) {
				length -= book[i];
				i++;
			}
			else {
				length = ans;
				count = count - 1;
			}
		}
		if (count != 0) {
			break;
		}
		ans += m;
	}
	cout << ans<< endl;
}

C++: //AOJ:0181 Persistence

#include<cstdio>
#include<cstring>
#include<algorithm>
#include<iostream>
#include<cmath>

using namespace std;

#define MAX -210000000
#define MIN 210000000
#define HABA 1500000

int n,m,book[100];
int low=0,high=HABA,tana,cunt,mid,flg;
int main(){
	scanf("%d %d",&m,&n);
	while(m!=0 && n!=0){
		for(int i=0;i<n;i++){
			scanf("%d",&book[i]);
		}
		low=0;
		high=HABA;
		while(low<=high/*k!=50*/){
			mid=(low+high)/2;
			//printf("mid=%d\n",mid);
			tana=0;
			cunt=1;
			flg=0;
			for(int i=0;i<n;i++){
				if(tana+book[i]>mid && tana==0){//printf("%d low=%d high=%d mid=%d cunt=%d\n",k,low,high,mid,cunt);
				//	printf("??\???????????¬?????????\n");
					flg=1;
					break;
				}
				if(tana+book[i]>mid){
					cunt++;
					if(cunt>m){
					//	printf("?£?????¶??????????\n");
						flg=1;
						break;
					}
					tana=book[i];
				}else tana+=book[i];
			}
			if(flg==1){
				low=mid+1;
				//printf("k low  %d\n",low);
			}else high=mid-1;
		}
		printf("%d\n",low);
		scanf("%d %d",&m,&n);
	}
	
	return 0;
}

C++: #include <iostream>
using namespace std;

int main(void){
 cout << 0 << endl;
}

C++: #include<iostream>

const int INF = 1 << 24;
int m, n, a[100];//dansu, honsu
bool C(double x){
	int i = 0, j = 0, l = 0;
	while(i < n && j < m){
		if(l + a[i] <= x){//入る
			l += a[i];
			i++;
		}else{//もう入らない
			l = 0;
			j++;
		}
	}

	if(i == n && j < m)return true;
	return false;
}

int main(){
	while(std::cin >> m >> n, m){
		for(int i=0;i<n;i++){
			std::cin >> a[i];
		}

		int lb = 0, ub = INF;

		for(int i=0;i<100;i++){
			int mid = (lb + ub) / 2;
			if(C(mid))ub = mid;
			else lb = mid;
		}

		std::cout << ub << std::endl;
	}
}

C++: #include <iostream>
using namespace std;

int m,n;
int lef,rig,cen;
int a[100];
int ans;

bool func(){
	int sum=0,M=1;
	for (int i=0; i<n; i++) {
		if (sum+a[i]<=cen) {
			sum+=a[i];
		}
		else{
			M++;
			if (M>m) {
				return false;
			}
			sum=0;
			if (a[i]>cen) {
				return false;
			}
			sum+=a[i];
		}
	}
	return true;
}

int main(){
	
	while (1) {
		cin>>m>>n;
		if (m==0&&n==0) {
			break;
		}
		for (int i=0; i<n; i++) {
			cin>>a[i];
		}
		lef=0;
		rig=1500000-1;
		ans=0;
		
		while (lef<=rig) {
			cen=(lef+rig)/2;
			if (func()) {
				ans=cen;
				rig=cen-1;
			}
			else{
				lef=cen+1;
			}
			
		}
		cout << ans << endl;
	}		
	
}

C++: #include <cstdio>
#include <cstdlib>
#include <cstring>
#include <cmath>
#include <ctime>
#include <cassert>
#include <iostream>
#include <cctype>
#include <sstream>
#include <string>
#include <list>
#include <vector>
#include <queue>
#include <set>
#include <stack>
#include <map>
#include <utility>
#include <numeric>
#include <algorithm>
#include <iterator>
#include <bitset>
#include <complex>
#include <fstream>
using namespace std;
typedef long long ll;
const double EPS = 1e-9;
typedef vector<int> vint;
typedef pair<int, int> pint;
#define rep(i, n) REP(i, 0, n)
#define ALL(v) v.begin(), v.end()
#define MSG(a) cout << #a << " " << a << endl;
#define REP(i, x, n) for(int i = x; i < n; i++)
template<class T> T RoundOff(T a){ return int(a+.5-(a<0)); }
template<class T, class C> void chmax(T& a, C b){ if(a < b) a = b; }
template<class T, class C> void chmin(T& a, C b){ if(b < a) a = b; }
template<class T, class C> pair<T, C> mp(T a, C b){ return make_pair(a, b); }

int height(vint& v, int w)
{
	int res = 0, pl = 0;
	
	rep(i, v.size())
	{
		if(w < pl + v[i])
		{
			res++;
			
			if(w < v[i]) return 1 << 30;
			
			pl = 0;
		}
		
		pl += v[i];
	}
	
	return res + (pl != 0);
}

int main()
{
	int m, n;
	while(cin >> m >> n && n)
	{		
		vint v(n);
		rep(i, n) cin >> v[i];
		
		int lb = 1, ub = 1500000;
		
		while(ub - lb > 1)
		{
			int mid = (ub + lb) / 2;
			
			if(height(v, ceil(mid)) <= m) ub = mid;
			else lb = mid;
		}
		
		cout << ub << endl;
	}
}