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;

int main(){
    while(1){
        int m, n;
        cin >> m >> n;

        if(m + n == 0)  break;

        int w[n], sum = 0, minp = 1000001;
        for(int i = 0; i < n; i++){
            cin >> w[i];
            sum += w[i];
            minp = min(minp, w[i]); 
        }

        int l = minp, r = sum;

        while(r-l > 0){
            int mid = (r+l) / 2;

            int tmpm = 1, nowp = 0;
            for(int i = 0; i < n; i++){
                if(nowp + w[i] <= mid){
                    nowp += w[i];
                }else{
                    nowp = w[i];
                    tmpm++;
                }
            }

            if(tmpm <= m)   r = mid;
            else            l = mid+1;
        }

        cout << r << endl;
    }

    return 0;
}

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

long long int a[150];
int n, m;

bool i_can(int wid, int m){
	int k = 1, sum = 0;
	for(int i = 0;i<=n;i++){
		sum += a[i];
		if(sum > wid){
			sum = a[i];
			k++;
		}
		if (m < k) return false;
	}
	return true;
}

int nibun(int s, int e, int m){
	if(!(i_can((s+e)/2,m))){
		return nibun((s + e) / 2 + 1, e, m);
	}
	else if (s == e) return s;
	else return nibun(s, (s + e) / 2, m);
}

int main(){
	while(cin >> m >> n){
		if (!n && !m) break;
		a[0] = 0;
		for(int i = 1;i<=n;i++){
			cin >> a[i];
		}
		cout << nibun(0, 1500000, m) << 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;

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

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

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

		while(left < right){

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

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

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

	}

    return 0;
}

C++: #include<bits/stdc++.h>
#define r(i,n) for(int i=0;i<n;i++)
using namespace std;
int n,m,a[101];
bool nibun(int x){
	int s=0,q=0,t=0;
	r(i,n){
		if(s+a[i]<=x)s+=a[i];
		else s=0,q++,i--,t++;
		if(t>300)return 0;
	}
	return m>=q+1;
}
main(){
	while(cin>>m>>n,m){
	  r(i,n)cin>>a[i];
	  int l=0,r=1500001;
	  while(l<r){
    	int mid=(l+r)/2;
		if(nibun(mid))r=mid;
		else l=mid+1;
	  }
	  cout<<l<<endl;
    }
}

C++: #include<cstdio>
#include<iostream>
#include<algorithm>
using namespace std;
int main(){
    while(1){
        int m,n,book[101]={0};
        int nowans,nowmin=0,nowmax=1500001;
        scanf("%d %d",&m,&n);
        if(m==0&&n==0)
            break;
        for(int i=0;i<n;i++)
            scanf("%d",&book[i]);
        for(int i=0;i<100;i++){
            int po=0;
            int dan[21]={0};
            nowans=(nowmin+nowmax)/2+1;
            for(int j=0;j<n+1;j++){
                if(j==n){
                    nowmax=nowans;
                    break;
                }
                if(dan[po]+book[j]<=nowans){
                    dan[po]+=book[j];
                    continue;
                }
                else if(book[j]>nowans){
                    nowmin=nowans;
                    break;
                }
                else if(dan[po]+book[j]>nowans){
                    po++;
                    if(po==m){
                        nowmin=nowans;
                        break;
                    }
                    dan[po]+=book[j];
                }
            }
        }
        printf("%d\n",nowans);
    }
}

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

int main(){
	long long int book[100], n, m, sum, width, max;
	while(cin >> m >> n){
		if(!m && !n) break;
		sum = 0;
		width = 0;
		max = 0;
		for(int i = 0;i<n;i++){
			cin >> book[i];
			sum += book[i];
		}
		for(int i = 0;i<n;i++){
			width += book[i];
			if(width >= (float)(sum / m)){
				if(max < width){
					max = width;
				}
				width = 0;
				continue;
			}
		}
		width = 0;
		for(int i = n-1;i>=0;i--){
			width += book[i];
			if(width >= (float)(sum / m)){
				if(max < width){
					max = width;
				}
				width = 0;
				continue;
			}
		}
		cout << max << endl;
	}
	return 0;
}

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

using namespace std;

void solve()
{
	int m, n;
	while(cin >> m >> n)
	{
		if(!m && !n)
		{
			break;
		}
		vector<int> Vec(n);
		for(int i = 0; i < n; ++i)
		{
			cin >> Vec[i];
		}
		int pre_width = 0;
		int width = 1500000;
		for(int a = 0; a < 50; ++a)
		{
			int pos = 0;
			for(int i = 0; i < m; ++i)
			{
				int buf = width;
				while(buf > Vec[pos])
				{
					buf -= Vec[pos];
					pos++;
					if(pos == n)
					{
						int pre_width_buf = width;
						width -= abs(width - pre_width) / 2;
						pre_width = pre_width_buf;
						goto NEXT;
					}
				}
			}
			if(pos < n)
			{
				int pre_width_buf = width;
				width += abs(width - pre_width) / 2;
				pre_width = pre_width_buf;
			}
NEXT:
			;
		}
		cout << width << endl;
	}
}

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

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

int main() {
  int M, N;
  while(cin >> M >> N && M) {
    vector<int> books(N);
    for(int i=0; i<N; i++) {
      cin >> books[i];
    }
    
    int l = 1, r = 1500001;
    while(r - l > 1) {
      int m = (l+r)/2;
      int bpos = 0;
      for(int i=0; i<M; i++) {
	int size = m;
	for(;;) {
	  if(bpos == N) break;
	  if(size - books[bpos] < 0) break;
	  size -= books[bpos++];
	}
	if(bpos == N) break;
      }
      if(bpos == N) {
	r = m;
      } else l = m+1;
    }
    
    cout << l << endl;
  }
  
  return 0;
}