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++: 
#define _CRT_SECURE_NO_WARNINGS
#define _USE_MATH_DEFINES
#include <cfloat>
#include <climits>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <ctime>
#include <iostream>
#include <memory>
#include <string>

#include <algorithm>
#include <complex>
#include <list>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <stack>
#include <vector>
#include <bitset>

using namespace std;

#ifdef _MSC_VER
#define __typeof__ decltype
#define strtok_r strtok_s
#endif

#define ITER(c)             __typeof__((c).begin())
#define FOREACH(it, c)      for (ITER(c) it=(c).begin(); it != (c).end(); ++it)
#define RITER(c)            __typeof__((c).rbegin())
#define RFOREACH(it, c)     for (RITER(c) it=(c).rbegin(); it != (c).rend(); ++it)
#define REP(i, n)           REPEAT(i, 0, n)
#define RREP(i, n)          RREPEAT(i, 0, n)
#define REPEAT(i, k, n)     for(int i = (k); i < ((k)+(n)); ++i)
#define RREPEAT(i, k, n)    for(int i = (k)+(n)-1; i >= (k); --i)
#define FROMTO(i,f,t)       for(int i = f; i < t; i++)
#define ALL(c)              (c).begin(), (c).end()
#define LLPOW(p1,p2)        ((ll)pow((double)(p1), (int)p2))
#define ESIZEOF(A)          (sizeof(A)/sizeof((A)[0]))
#define CIN_NO_SYNC         do { cin.tie(0); ios_base::sync_with_stdio(false); } while(0)
#define GETSTR(p)           fgets((p), sizeof(p), stdin)
#define CHOMP(p)            do{ char *_q = (p) + strlen(p)-1; if(*_q == '\n' || *_q == '\r') *_q = 0; } while(0)
#define FILL(m,v)           memset(m, v, sizeof(m))
#define mp                  make_pair
#define pb                  push_back
template<class _T> _T MAX(_T p1,_T p2){return (p1>p2)?p1:p2;} 
template<class _T> _T MIN(_T p1,_T p2){return (p1<p2)?p1:p2;} 
template <class _Tv> inline string join(const _Tv &v,string sep=" "){
    ostringstream s;FOREACH(it,v){if(it!=v.begin())s<<sep;s<<*it;};return s.str();
}

typedef long long ll;
typedef unsigned long long ull;
typedef double D;
typedef complex<D> P;
typedef vector<int> VI;
typedef vector<VI> VVI;
#define X real()
#define Y imag()
#define EPS (1e-9)
#define DEQ(p1,p2) (abs((p1)-(p2)) < EPS)

#ifdef _DEBUG
template<class _Tv> inline void _prfe(const char *_n,const _Tv _c,bool _p=false){
    ITER(_c) _it=_c.begin();
    if(_p){cout<<_n<<" = ["<<endl;for(;_it!=_c.end();++_it)cout<<"    "<<*_it<<endl;cout<<"]"<<endl; }
    else{cout<<_n<<" = [ ";if(_c.size()){cout<<*_it++;for(;_it!=_c.end();++_it)cout<<", "<<*_it;}cout<<" ]"<<endl; }
}
ostream &operator<<(ostream &os, const pair<int,int> &p){return(os<<"("<<p.first<<","<<p.second<<")");}
#define pf printf
#define pr(n) do{cout<<#n" = "<<(n)<<endl;}while(0)
#define prfe(n) _prfe(#n, n)
#define prfep(n) _prfe(#n, n, true)
#define dbgchk(n) do{if(n)throw;}while(0)
#else
#define pf(...) /* */
#define pr(...) /* */
#define prfe(...) /* */
#define prfep(...) /* */
#define dbgchk(...) /* */
#endif

int p[101];
int dp[21][101]; // [分割数][取り込み済み冊数]

int dfs(int i, int n, int m){
    int mn = 1<<29;
    if(dp[m][i] >= 0) return dp[m][i];

    if(i >= n) return 0;
    if(m == 1){
        mn = p[n] - p[i];
        pf("dfs(%d,%d,%d) = %d\n", i, n, m, mn);
        return (dp[m][i] = mn);
    }
    for(int j = i + 1; j <= n; ++j){
        int mx = MAX(p[j] - p[i], dfs(j, n, m-1));
        mn = MIN(mn, mx);
    }
    pf("dfs(%d,%d,%d) = %d\n", i, n, m, mn);
    return (dp[m][i] = mn);
}

int solve(int n, int m, VI &vi){
    p[0] = 0;
    REP(i,n) p[i+1] = p[i] + vi[i];
    FILL(dp, -1);
    return dfs(0, n, m);
}

int main(){
    int n, m;
    while(cin >> m >> n){
        if(n == 0 && m == 0) break;
        VI vi;
        REP(i,n){
            int k;
            cin >> k;
            vi.pb(k);
        }
        cout << solve(n, m, vi) << endl;
    }
    return 0;
}

C++: #include <iostream>

using namespace std;

#define MAX 100

int book[MAX],m,n;

bool check(int limit){
  int cnt = 0, sum = 0;

  for(int i = 0 ; i < n ; i++){
    if(book[i] > limit){
      return false;
    }

    sum += book[i];
    if(sum == limit){
      sum = 0;
      cnt++;
    }else if(sum > limit){
      sum = book[i];
      cnt++;
    }
  }

  if(sum > 0){
    cnt++;
  }

  return cnt <= m;
}

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

    int lb = 0, ub = 1500000;
    while(ub - lb > 0){
      int mid = (lb + ub) / 2;

      if(check(mid)){
        ub = mid;
      }else{
        lb = mid + 1;
      }
    }
    cout << ub << endl;
  }

  return 0;
}

C++: #include "bits/stdc++.h"
using namespace std;
typedef long long ll;
#define rep(i,n) for(ll i=0;i<(ll)(n);i++)
#define all(a)  (a).begin(),(a).end()
#define pb push_back
#define INF 99999999


int m,n;
int b[110];

bool solve(int v){
    int l=0;
    int x=v;
    rep(i,n){
        while(x<b[i]&&l<m){
            x=v;
            l++;
        }
        if(l>=m)return false;
        if(x>=b[i]){
            x-=b[i];
        }
    }
    return true;
}

int main(){
    while(cin>>m>>n){
        if(m==0&&m==0)break;
        rep(i,n)cin>>b[i];
        
        int left=0;
        int right=1500000-1;
        while(right>left+1){
//            cout<<left<<" "<<right<<endl;
            int cent=(left+right)/2;
            bool res = solve(cent);
            if(res) right=cent;
            else left=cent;
        }
        cout<<right<<endl;
    }
}

C++: #include "bits/stdc++.h"
using namespace std;
int books[105];
int m,n;
bool check(int mid){
    int now = 0; int d = 0; int l = 0;
    while(now < n && d < m){
        if(l + books[now] <= mid){
            l += books[now++];
        }
        else{
            d++; l = 0;
        }
    }
    if(d > m-1)return false;
    else return true;
}
int main(){
    while(scanf("%d%d",&m,&n),(m|n)){
        int sum = 0;
        int ma = -1;
        for(int i = 0; i < n; i++){
            scanf("%d",&books[i]);
            sum += books[i];
            ma = max(ma,books[i]);
        }

        int l = ma,r = sum;
        int mid=0;
        for(int i = 0;i < 100; i++){
            mid = (l+r)/2;
            if( check(mid) ){
                r = mid;
            }
            else l = mid;
        }
        printf("%d\n",r);
    }
    return 0;
}