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<algorithm>
#include<cmath>
using namespace std;
bool check(int now,int a[150],int m,int n)
{
	int ima = 0;
	for(int i=0;i<m;i++)
	{
		int aho=now;
		for(;ima<n;ima++)
		{
			aho-=a[ima];
			if(aho<0)break;
		}
		if(ima==n)return true;
	}
	return false;
	
	
	
}


int main ()
{
	while(1){
		int n,m;int bok[150];
		cin>>m>>n;if(!m&&!n)break;
		for(int i=0;i<n;i++)cin>>bok[i];
		
		int l = 0, r = 50000000;
		while( l != r ){
			int now = (l+r) / 2;
			if( check(now,bok,m,n) ){
				r = now;
			}else{
				l = now+1;
			}
		}
		cout<<l<<endl;
	}
	
	
	
	return 0;
}

C++: #include<iostream>
using namespace std;
int n, m;
int a[101];
bool search(int v);
int main()
{	
	cin >> m >> n;
	for (int i = 0; i < n; i++){
		cin >> a[i];
	}
	int l, r;
	l = 0; r = 1 << 15;
	for (int i = 0; i < 100; i++){
		int mi = (l + r) / 2;
		if (search(mi))r = mi;
		else l = mi;
	}
	cout << r-1 << endl;
}
bool search(int v){
	int n2=0, m2=0,b=0;
	while (n2 < n&&m2 < m){
		if (b + a[n2] < v){
			b += a[n2];
			n2++;
		}
		else{
			b = 0;
			m2++;
		}
	}
	if (n2 == n&&m2 < m)return true;
	return false;
}

C++: #include <iostream>
#include <vector>
#include <cmath>

using namespace std;
int m, n;
int ans;

bool f(int mid, const vector<int>& page)
{
	int need = 1;
	int s = 0;
	for(int i = 0; i < n; ++i)
	{
		if(page[i] > mid)
		{
			return(false);
		}
		if(s + page[i] > mid)
		{
			++need;
			s = 0;
		}
		s += page[i];
	}
	return (need <= m);
}

void solve()
{
	while(cin >> m >> n, m || n)
	{
		vector<int> page(n);
		for(int i = 0; i < n; ++i)
		{
			cin >> page[i];
		}
		int right = 1500000;
		int left = 1;
		while(right - left > 1)
		{
			int mid = (left + right) / 2;
			if(f(mid, page))
			{
				right = mid;
			}
			else
			{
				left = mid;
			}
		}
		if(f(left, page))
		{
			cout << left << endl;
		}
		else
		{
			cout << right << endl;
		}
	}
}

int main()
{
	solve();
	return(0);
}

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

int main() {
  for(int m, n; cin >> m >> n && (m|n);) {
    vector<int> a(n);
    for(int i = 0; i < n; ++i) {
      cin >> a[i];
    }
    int l = *min_element(a.begin(), a.end()) , r = 1500001;
    while(r-l > 1) {
      int mid = (l+r)/2;
      int x = 0, y = 1;
      for(int i = 0; i < n; ++i) {
        if(x + a[i] >= mid) {
          x = a[i];
          ++y;
        } else {
          x += a[i];
        }
      }
      if(y <= m) {
        r = mid;
      } else {
        l = mid;
      }
    }
    cout << l << endl;
  }
  return 0;
}

C++: #include <stdio.h>
#include <vector>
#include <algorithm>

#define MAX_SIZE 1500000

using namespace std;

int main(){
	int dansu,kansu,shelf[20],book[100],min_size,left,right,m,dansu_index,max_length;

	while(true){
		scanf("%d %d",&dansu,&kansu);
		if(dansu == 0 && kansu == 0)break;

		max_length = 0;

		for(int i = 0; i < kansu; i++){
			scanf("%d",&book[i]);
			max_length = max(max_length,book[i]);
		}

		min_size = MAX_SIZE;
		left = max_length,right = MAX_SIZE;
		m = (left+right)/2;

		while(left < right){

			for(int i = 0; i < dansu; i++)shelf[i] = m;
			dansu_index = 0;
			for(int i = 0; i < kansu && dansu_index < dansu;){
				if(book[i] > m){
					dansu_index = dansu;
					break;
				}
				if(shelf[dansu_index] >= book[i]){
					shelf[dansu_index] -= book[i];
					i++;
				}else{
					dansu_index++;
				}
			}

			if(dansu_index == dansu){
				left = m + 1;
				m = (left + right)/2;
			}else{
				min_size = min(min_size,m);
				right = right - 1;
				m = (left + right)/2;
			}
		}

		printf("%d\n",min_size);

	}

    return 0;
}

C++: #include <stdio.h>
 
int main(){
    int m,n;
    long int book[124]={0};
    int i,j;
    long int sum=0;
    long int x;
    int count;
    int sum2=0;
    int judge;
    
    scanf("%d %d\n",&m,&n);
    do{
    for(i=0;i<n;i++){
        scanf("%ld\n",&book[i]);
        sum+=book[i];
    }
    x=sum/m;
    sum2=0;
    for(i=0;i<200;i++){
        sum2+=book[i];
        if(sum2>x){
            judge=i-1;
            break;
        }
    }
    //printf("%d\n",x);
    for(j=judge;j<n;j++){
        count=1;
        x=0;
        for(i=0;i<=j;i++){
            x+=book[i];
        }
        //printf("%d %d %d %d\n",sum,judge,sum2,x);
        sum2=0;
    for(i=0;i<n;i++){
        sum2+=book[i];
        if(sum2>x){
            sum2=book[i];
            count++;
        }
    }
    //printf("count %d\n",count);
    if(count<=m){
        break;
    }
    }
    printf("%ld\n",x);
    scanf("%d %d\n",&m,&n);
    }while (m!=0||n!=0);
    return 0;
    
}

C++: #include <iostream>
#include <algorithm>
#include <map>
#include <set>
#include <queue>
#include <stack>
#include <numeric>
#include <bitset>
#include <cmath>

static const int MOD = 1000000007;
using ll = long long;
using u32 = unsigned;
using u64 = unsigned long long;
using namespace std;

template<class T> constexpr T INF = ::numeric_limits<T>::max()/32*15+208;

int main() {
    int m, n;
    while(cin >> m >> n, m){
        vector<int> v(n);
        for (auto &&i : v) scanf("%d", &i);
        int ng = *max_element(v.begin(),v.end())-1, ok = 1500001;
        while(ok-ng > 1){
            int mid = (ok+ng)/2;
            int val = 0, cnt = 1;
            for (int i = 0; i < n; ++i) {
                if(val+v[i] > mid){
                    cnt++; val = v[i];
                }else {
                    val += v[i];
                }
            }
            if(cnt <= m) ok = mid;
            else ng = mid;
        }
        cout << ok << "\n";
    }
    return 0;
}