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>
using namespace std;

const int BOOK_LIMIT = 100;
const int COL_LIMIT = 20;
const int WIDTH_LIMIT = 1500000;

struct Column
{
	int head;
	int w;
};

class Shelf
{
public:
	void input( int numColumn, int numBook )
	{
		width = 0;
		m = numColumn;
		n = numBook;
		int sum = 0;
		int w = 0;
		for( int i = 0; i < n; ++i ){
			int t;
			cin >> t;
			book[ i ] = t;
			sum += t;
			if( w < t ){
				w = t;
			}
		}
		if( sum / m > WIDTH_LIMIT ){
			width = w;
		}else{
			width = ( sum / m > w ? sum / m : w );
		}
	}

	int putAll()
	{
		int ib = putInit();
		int low = m - 1;
		while( ib < n ){
			if( col[ low ].w + book[ ib ] > width ){
				secureSpace( ib );
			}
			if( col[ low ].w == 0 ){
				col[ low ].head = ib;
			}
			col[ m - 1 ].w += book[ ib++ ];
		}
		return width;
	}

	int putInit()
	{
		int ib = 0;
		int is = 0;
		while( ib < n && is < m ){
			col[ is ].head = ib;
			col[ is ].w = 0;
			while( col[ is ].w + book[ ib ] <= width ){
				col[ is ].w += book[ ib ];
				++ib;
			}
			++is;
		}
		while( is < m ){
			col[ is ].head = -1;
			col[ is ].w = 0;
			++is;
		}
		return ib;
	}

	void secureSpace( int ib )
	{
		int ex = 0x7fffffff;
		int space = 0;
		int is = 1;
		for( int i = 0; i < m - 1; ++i ){
			int s = width - col[ i ].w;
			int e = book[ col[ i + 1 ].head ] - s;
			if( e < ex && e + width < WIDTH_LIMIT ){
				ex = e;
				is = i + 1;
			}
			if( space < s ){
				space = s;
			}
		}
		if( ex == 0x7fffffff ){
			ex = book[ ib ] - space;
		}
		width += ex;
		for( int i = is; i < m; ++i ){
			raiseBook( i );
		}
		if( col[ m - 1 ].w + book[ ib ] > width ){
			secureSpace( ib );
		}
	}

	void raiseBook( int is )
	{
		int upper = is - 1;
		int lower = is;
		int bw = book[ col[ lower ].head ];
		if( ( bw + col[ upper ].w ) <= width ){
			if( col[ upper ].w == 0 ){
				col[ upper ].head = col[ lower ].head;
			}
			col[ upper ].w += bw;
			++col[ lower ].head;
			col[ lower ].w -= bw;
			if( col[ lower ].w == 0 ){
				col[ lower ].head = -1;
			}
		}
	}

private:
	int book[ BOOK_LIMIT ];
	Column col[ COL_LIMIT ];
	int m, n;
	int width;
};

int main()
{
	int m, n;
	while( cin >> m >> n && ( m || n ) ){
		Shelf s;
		s.input( m, n );
		cout << s.putAll() << endl;
	}

	return 0;
}

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

using namespace std;

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




int main(void) {

	while (1) {

		int floor, n, book[1000] = { 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=ans-1;
		}
		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++: #include<bits/stdc++.h>
using namespace std;
int books[105],m,c,ri,le,mid,ans,sum[25],n;
bool flag=false;

int main(){
    while(1){
        cin >> m >> n;
        if(n==0&&m==0)break;
        for(int i=0;i<n;i++){
                cin >> books[i];
                ri+=books[i];
        }
        mid=ri/2;
        le=0;

        for(int i=0;i<100;i++){
            for(int j=0;j<m;j++){
                for(;;){
                    if(c==n){
                        flag=true;
                        break;
                    }
                    if(sum[j]+books[c]>mid)break;
                    else sum[j]+=books[c];
                    c++;
                    }
                }

                if(flag==true){
                    ri=mid;
                    mid=ri/2;
                }else{
                    le=mid;
                    mid=(le+ri)/2;
                }
                flag=false;
                c=0;
                for(int o=0;o<n;o++)sum[o]=0;
            }
            cout << ri << endl;
    }

 return 0;
}

C++: #include<cstdio>
#include<iostream>

using namespace std;
#define INF 1500000


int main(void) {

	while (1) {
		int max = INF, min = 0;
		int floor, n, book[102] = { 0 };
		cin >> floor >> n;

		if (floor == 0)return 0;

		for (int i = 0; i < n; i++) {
			cin >> book[i];
		}
		while (max - min > 1) {
			int count = floor;
			int  mid = (max + min) / 2;
			int length = mid;
			int i = 0;
			while (count != 0 && i < n) {
				if (length >= book[i]) {
					length -= book[i];
					i++;
				}
				else {
					length = mid;
					count = count - 1;
				}
			}	
				if (count != 0) {
					max = mid;
				}else {
					min = mid;
				
			}
		}
		cout << max << endl;
	}
}

C++: #include "bits/stdc++.h"
using namespace std;

#define int long long
#define DEBUG 0
#define rep(i,a,b) for(int i=(a);i<(b);i++)
#define rrep(i,a,b) for(int i=(b)-1;i>=(a);i--)
#define all(a) (a).begin(),(a).end()
#define dump(o) if(DEBUG){cerr<<#o<<" "<<o<<endl;}
#define dumpc(o) if(DEBUG){cerr<<#o; for(auto &e:(o))cerr<<" "<<e;cerr<<endl;}
using pii = pair<int, int>; using vi = vector<int>;
static const int INF = sizeof(int) == sizeof(long long) ? 0x3f3f3f3f3f3f3f3fLL : 0x3f3f3f3f;
static const int MOD = (int)(1e9 + 7);

int m, n;
//??¬?£?????????????????????????
bool f(vector<int> &w, int a) {
	int cnt = 1;
	int tmp = 0;
	for (int i = 0; i < w.size(); i++) {
		if (tmp + w[i] > a) {
			cnt += 1;
			tmp = 0;
		}
		tmp += w[i];
	}
	if (cnt > m)return false;
	return true;
}

signed main() {
	for (; cin >> m >> n&&m;) {
		vector<int> w(n);
		rep(i, 0, n)cin >> w[i];
		if (n <= m) {
			cout << *max_element(all(w)) << endl;
			return 0;
		}

		int L = 0, R = 1500001;
		while (L < R) {
			int MID = (L + R) / 2;
			if (f(w, MID))R = MID;
			else L = MID + 1;
		}
		cout << R << endl;
	}
	return 0;
}