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<bits/stdc++.h>
using namespace std;

#define lint long long
#define P pair<int, int>
#define LLP pair<long long, long long>
#define REP(i, x, n) for(int i = (x), i##_len = (int)(n) ; i < i##_len ; ++i)
#define rep(i, n) for(int i = 0, i##_len = (int)(n) ; i < i##_len ; ++i)
#define repr(i, n) for(int i = (int)(n) - 1 ; i >= 0 ; --i)
#define SORT(x) sort((x).begin(), (x).end())
#define SORT_INV(x) sort((x).rbegin(), (x).rend())

const int IINF = 1e9 + 10;
const long long LLINF = (long long)1e18 + 10;
const long long MOD = (long long)1e9 + 7;
const int dx4[] = {1, 0, -1, 0}, dy4[] = {0, 1, 0, -1};
const int dx8[] = {1, 1, 0, -1, -1, -1, 0, 1}, dy8[] = {0, -1, -1, -1, 0, 1, 1, 1};
const double EPS = 1e-8;

int n, m;
vector<int> w;
int maxi;

bool check(int mid){
    if(maxi > mid){
        return false;
    }
    int cnt = 1, sum = 0;
    rep(i, n){
        if(sum + w[i] <= mid){
            sum += w[i];
        }else{
            ++cnt;
            sum = w[i];
        }
    }
    return cnt <= m;
}

int meguru_search(int ok, int ng){
    while(abs(ok - ng) > 1){
        int mid = (ok + ng) / 2;
        if(check(mid)){
            ok = mid;
        }else{
            ng = mid;
        }
    }
    return ok;
}

int main(){
    cin.tie(0);
    ios::sync_with_stdio(false);

    while(cin >> m >> n, m){

        w.resize(n);
        int sum = 0;
        maxi = 0;
        rep(i, n){
            cin >> w[i];
            sum += w[i];
            maxi = max(maxi, w[i]);
        }

        int ans = meguru_search(sum, 0);
        cout << ans << endl;

    }

    return 0;
}

C++: #include <iostream>
#include <cstdio>
#include <vector>
#include <algorithm>
#include <complex>
#include <cstring>
#include <cstdlib>
#include <string>
#include <cmath>
#include <queue>
using namespace std;

#define REP(i,n) for(int i=0;i<(int)n;++i)
#define FOR(i,c) for(__typeof((c).begin())i=(c).begin();i!=(c).end();++i)
#define ALL(c) (c).begin(), (c).end()
const int INF = 1<<30;

template<class T> T lower_search(T lb, T ub, bool (*C)(T)) {
  while(ub-lb>1) { // ツつアツつアツづ債適ツ督鳴づ可。
    T mid = (lb+ub)/2;
    if (C(mid)) ub = mid;
    else lb = mid;
  }
  return ub;
}
int m, n;
int a[100];

bool C(int x) {
  int hoge = 0;
  REP(i,m) {
    int tmp = 0;
    while(tmp + a[hoge] <= x) {
      tmp += a[hoge];
      hoge++;
      if (hoge >= n)
        return true;
    }
  }
  return false;
}
int main() {
  while(cin >> m >> n, m+n) {
    REP(i,n) {
      cin >> a[i];
    }
    cout << lower_search(0, INF, C) << endl;
  }
}

C++: #include<iostream>
#include<algorithm>
#include<vector>
#include<cmath>
#include<climits>
using namespace std;



int m;
vector<int> cost;
void init(){
  cost.clear();
}

bool input(){
  int n;
  cin>>m>>n;
  if(n==0&&m==0)return 0;
  for(int i=0;i<n;i++){
    int tmp;
    cin>>tmp;
    cost.push_back(tmp);
  }
  return 1;
}


bool judge(long long w){
  long long tmp_w=0;
  long long tmp_m=1;
  for(int i=0;i<cost.size();i++){
    if(tmp_w+cost[i]>w){
      tmp_w=0;
      tmp_m++;
      i--;
      if(tmp_m>m){
        return false;
      }
    }else{
      tmp_w+=cost[i];
    }
  }
  return true;
}

int binary_serch(long long t_min,long long t_max){
  if(t_min>=t_max)return t_max;
  long long c=(t_min+t_max)/2;
  if(judge(c)){
    return binary_serch(t_min,c);
  }else{
    return binary_serch(c+1,t_max);
  }
}

int solve(){
  return binary_serch(0,1500000); 
}


int main(){
  while(init(),input()){
    cout<<solve()<<endl;
  }
}

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

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

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

void erase(){
	for(int i=1; i<m; i++)
		l[i] = 0;
}

void solve(int x, int y){
	if(flag)
		return;
	l[x] = 0;
	for(int i=y; i<n; i++){
		l[x] += b[i];
		if(x != m-1)
			solve(x+1, i+1);
	}
	if(x == m-1){
//		for(int i=0; i<m; i++)
//			cout << l[i] << ' ';
//		cout << endl;
		hk[mp++] = max();
		if(hk[mp-1] < hk[mp])
			flag = true;
		return;
	}
}

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];
		mp = 0;
		flag = false;
		solve(0, 0);
		cout << min() << endl;
		delete[] b;
		delete[] l;
		hk.clear();
	}
}

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

using namespace std;

int jagde( int *book, int ix, int n, int m )
{
	int sum;
	int j;

	sum = 0;
	j = 0;
	for( int i = 0;i < n;i++ ){
		sum += book[i];
		if( sum > ix ){
			j++;
			i--;
			sum = 0;
		}
	}
	if( j >= m ) return -1;

	ix--;

	sum = 0;
	j = 0;
	for( int i = 0;i < n;i++ ){
		sum += book[i];
		if( book[i] > ix ) return 0;
		if( sum > ix ){
			j++;
			i--;
			sum = 0;
		}
	}
	if( j >= m ) return 0;
	return 1;
}


int answer( int m, int n, int *book )
{
	int low,high;
	int sum[101];

	sum[0] = book[0];
	low = book[0];
	for( int i = 1;i < n;i++ ){
		if( low < book[i] ) low = book[i];
		sum[i] = sum[i - 1] + book[i];
	}

	high = sum[n - 1];

	for( int i = 0;i < 22;i++ ){
		int ix = (low + high) / 2;
		int answer = jagde( book, ix, n, m );

		if( answer == -1 ){
			low = ix + 1;
		} else if( answer == 0 ) {
			return ix;
		} else if( answer == 1 ) {
			high = ix - 1;
		}
	}
	return low;

}

int main(int argc, char const* argv[])
{
	int m,n;
	int book[101];

	while( cin >> m >> n && m != 0 ){
		for( int i = 0;i < n;i++ ) cin >> book[i];

		cout << answer( m, n, book ) << endl;
	}
	return 0;
}

C++: #include <iostream>
#include <cstdlib>
#define MAX_N 100
#define INF 1500000
using namespace std;
int t[MAX_N];
int m, n;

bool C(int x) {
	int i, sum, shelf = m;
	
	i = 0;
	while (shelf--) {
		sum = 0;
		while (i < n && sum + t[i] <= x) {
			sum += t[i];
			i++;
		}
	}
	return i == n;
}

int solve() {
	int ok = INF, ng = 0, mid;
	while (abs(ok - ng) > 1) {
		mid = (ok + ng) / 2;
		if (C(mid)) ok = mid;
		else ng = mid;
	}
	return ok;
}

int main() {
	while (cin >> m, m) {
		cin >> n;
		for (int i = 0; i < n; ++i) {
			cin >> t[i];
		}
		cout << solve() << endl;
	}
	return 0;
}