Problem:
本棚
太郎君はとある小説にはまっています。その小説は全部で
n
巻あり、各巻で本の厚さが異なります。太郎君はこの小説が大変気に入ったので、その小説専用の本棚を買おうと思っています。しかし、部屋に大きな本棚を置くとかなり狭くなってしまうので、出来るだけ本棚の幅が小さくなるように工夫しなければなりません。床から天井の高さを測ったところ、どうやら
m
段の本棚なら置けることが分かりました。そこで、小説
n
巻をどのように分ければ
m
段の本棚の幅を最小に出来るでしょうか?
 太郎君にはこだわりがあり、各段に納める小説は巻の番号順に並んでいなければなりません。
本棚の段数、小説の巻数、各本の厚さを入力として、全巻を 1巻から順に収めることができる本棚の中で幅が最小となるものの幅を求めるプログラムを作成してください。ただし、本棚の枠の大きさは幅に含めないこととします。
Input
複数のデータセットの並びが入力として与えられます。入力の終わりはゼロふたつの行で示されます。各データセットは以下の形式で与えられます。
m
n
w
1
w
2
:
w
n
１行目に部屋に置くことができる本棚の段数
m
(1 ≤
m
≤ 20)、 小説の巻数
n
(1 ≤
n
≤ 100) が与えられます。続く
n
行に第
i
巻の本の厚さを表す整数
w
i
(1 ≤
w
i
≤ 1000000) が与えられます。
ただし、本棚の幅は 1500000 を超えないものとします。
データセットの数は 50  を超えません。
Output
データセット毎に最小となる本棚の幅を１行に出力します。
Sample Input
3 9
500
300
800
200
100
600
900
700
400
4 3
1000
1000
1000
0 0
Output for the Sample Input
1800
1000

Submitted solutions:

code_snippet: #include<iostream>
#include<algorithm>
#include <map>
#include <vector>
#include <stack>
#include<string>
#include<queue>
#include <regex>

using namespace std;

int w[100];
int record[100][100];

int minW,tempW,totalW,leftW;

int EstimateMin(int leftW,int floor)
{
	if (floor == 0)
		return -1;
	return leftW / floor;
}

void Backtrace(int beg,int n,int floor)
{
	if (floor == 0)
	{
		if (tempW < minW && beg == n + 1)
			minW = tempW;
		return;
	}
	int temp = tempW;
	for (int i = beg - 1; i <= n; i++)
	{
		if (record[beg][i] >= minW)
			break;
		if (EstimateMin(leftW - record[beg][i], floor - 1) >= minW)
			continue;
		tempW = max(record[beg][i], temp);
		leftW -= record[beg][i];
		Backtrace(i + 1, n, floor - 1);
		leftW += record[beg][i];
		tempW = temp;
	}
}

int main()
{
	int m, n;
	cin >> m >> n;
	while (m || n)
	{
		totalW = 0;
		for (size_t i = 1; i <= n; i++)
		{
			cin >> w[i];
			totalW += w[i];
		}
		for (size_t i = 1; i <= n; i++)
		{
			record[i][i - 1] = 0;
			int sumw = 0;
			for (size_t j = i; j <= n; j++)
			{
				sumw += w[j];
				record[i][j] = sumw;
			}
		}
		tempW = 0;
		minW = totalW;
		leftW = totalW;
		Backtrace(1, n, m);
		cout << minW << endl;
		cin >> m >> n;
	}
	//system("pause");
}
accuracy: 1
time_ms: 50
memory_kb: 3112
final_rank: 7

code_snippet: // 基本テンプレート
 
#include <iostream>
#include <iomanip>
#include <cstdio>
#include <string>
#include <cstring>
#include <deque>
#include <list>
#include <queue>
#include <stack>
#include <vector>
#include <utility>
#include <algorithm>
#include <map>
#include <set>
#include <complex>
#include <cmath>
#include <limits>
#include <cfloat>
#include <climits>
#include <ctime>
#include <cassert>
#include <numeric>
#include <fstream>
#include <functional>
using namespace std;
 
#define rep(i,a,n) for(int (i)=(a); (i)<(n); (i)++)
#define repq(i,a,n) for(int (i)=(a); (i)<=(n); (i)++)
#define repr(i,a,n) for(int (i)=(a); (i)>=(n); (i)--)
#define debug(...) fprintf(stderr, __VA_ARGS__)
#define int long long int
 
template<typename T> void chmax(T &a, T b) {a = max(a, b);}
template<typename T> void chmin(T &a, T b) {a = min(a, b);}
template<typename T> void chadd(T &a, T b) {a = a + b;}
 
typedef pair<int, int> pii;
typedef long long ll;
 
int dx[] = {0, 0, 1, -1};
int dy[] = {1, -1, 0, 0};
const ll INF = 1001001001001001LL;
const ll MOD = 1000000007LL;

int M, N, width[110];
int dp[110][110][25];

signed main() {
    while(cin >> M >> N, M) {
        fill(dp[0][0], dp[N][N], INF);
        for(int i=0; i<N; i++) {
            cin >> width[i];
        }

        for(int i=0; i<N; i++) {
            int sum = 0;
            for(int j=i; j<N; j++) {
                sum += width[j];
                dp[i][j+1][1] = sum;
            }
        }

        for(int d=2; d<=N; d++) {
            for(int l=0; l<N-d+1; l++) {
                int r = l + d;
                int mi = INF;
                for(int m=2; m<=M; m++) {
                    chmin(dp[l][r][m], mi);
                    for(int k=l+1; k<r; k++) {
                        for(int p=1; p<m; p++) {
                            int vl = dp[l][k][p  ];
                            int vr = dp[k][r][m-p];
                            chmin(dp[l][r][m], max(vl, vr));
                        }
                    }
                    chmin(mi, dp[l][r][m]);
                }
            }
        }
        cout << dp[0][N][M] << endl;
    }
    return 0;
}
accuracy: 1
time_ms: 20
memory_kb: 3964
final_rank: 10

code_snippet: #include<iostream>
#include<algorithm>
using namespace std;
int main() {
	int m, n;
	while (cin >> m >> n, m, n) {
		int w[110];
		int f = 0;
		for (int i = 0; i < n; i++) {
			cin >> w[i]; f = max(w[i], f);
		}
		while (1) {
			int p = 1, q = f;
			for (int i = 0; i < n; i++) {
				if (w[i] <= q)q -= w[i];
				else {
					p++;
					q = f - w[i];
				}
			}
			if (p <= m)break;
			f++;
		}
		cout << f << endl;
	}
}
accuracy: 1
time_ms: 170
memory_kb: 3112
final_rank: 9

code_snippet: #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;
		int i;

		while(left < right){

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

			if(i < kansu){
				left = m + 1;
				m = (left + right)/2;
			}else{
				right = right - 1;
				m = (left + right)/2;
			}
		}

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

	}

    return 0;
}
accuracy: 1
time_ms: 660
memory_kb: 2600
final_rank: 12

code_snippet: #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++;
					if(dansu_index < dansu)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;
}
accuracy: 1
time_ms: 640
memory_kb: 2564
final_rank: 11

code_snippet: #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(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{
				right = right - 1;
				m = (left + right)/2;
			}
		}

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

	}

    return 0;
}
accuracy: 1
time_ms: 710
memory_kb: 2688
final_rank: 15

code_snippet: #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;
}
accuracy: 1
time_ms: 740
memory_kb: 2564
final_rank: 14

code_snippet: #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(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;
}
accuracy: 1
time_ms: 710
memory_kb: 2708
final_rank: 16

code_snippet: #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){

			for(int i = 0; i < dansu; i++)shelf[i] = 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++;
				}
			}

			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;
}
accuracy: 1
time_ms: 730
memory_kb: 2584
final_rank: 13

code_snippet: #include<bits/stdc++.h>
#define rep(i,n)for(int i=0;i<n;i++)
using namespace std;

int w[100];
int main() {
	int m, n;
	while (scanf("%d%d", &m, &n), m) {
		int ans = 0;
		rep(i, n) {
			scanf("%d", &w[i]);
			ans = max(ans, w[i]);
		}
		for (;; ans++) {
			int j = 0, k = 0;
			bool flag = true;
			rep(i, n) {
				j += w[i];
				if (j > ans) {
					k++; j = w[i];
					if (k >= m) {
						flag = false;
						break;
					}
				}
			}
			if (flag) {
				printf("%d\n", ans);
				break;
			}
		}
	}
}
accuracy: 1
time_ms: 90
memory_kb: 3164
final_rank: 8

code_snippet: #include <iostream>
#include <cstdio>
#include <algorithm>

using namespace std;

#define FOR(i,a,b) for (int i = a; i < b; i++)
#define REP(i,n) FOR(i,0,n)

int main() {
	int n, m;
	while (cin >> m >> n, n || m) {
		int v[100];
		REP(i, n) scanf("%d", &v[i]);
		
		int sum[100];
		sum[0] = v[0];
		FOR(i,1,n) sum[i] = sum[i - 1] + v[i];
		
		int ans = 1;
		REP(i, n) ans = max(ans, v[i]);
		ans = max(ans, sum[n - 1] / m);
		
		while (true) {
			int cnt = 0, pos = 0;
			while (pos < n) {
				cnt++;
				if (cnt > m) break;
				
				int now = v[pos++];
				while (pos < n && now + v[pos] <= ans) now += v[pos++];
			}
			
			if (cnt <= m) {
				cout << ans << endl;
				break;
			}
			
			ans++;
		}
	}
	
	return 0;
}
accuracy: 1
time_ms: 20
memory_kb: 1244
final_rank: 1

code_snippet: #include <iostream>
#include <cstdio>
#include <algorithm>

using namespace std;

#define FOR(i,a,b) for (int i = a; i < b; i++)
#define REP(i,n) FOR(i,0,n)

int main() {
	int n, m;
	while (cin >> m >> n, n || m) {
		int v[100];
		REP(i, n) scanf("%d", &v[i]);
		
		int ans = 1;
		REP(i, n) ans = max(ans, v[i]);
		
		while (true) {
			int cnt = 0, pos = 0;
			while (pos < n) {
				cnt++;
				int now = v[pos++];
				while (pos < n && now + v[pos] <= ans) now += v[pos++];
			}
			
			if (cnt <= m) {
				cout << ans << endl;
				break;
			}
			
			ans++;
		}
	}
	
	return 0;
}
accuracy: 1
time_ms: 290
memory_kb: 1240
final_rank: 6

code_snippet: //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[102] = { 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;

	}
}
accuracy: 1
time_ms: 190
memory_kb: 1160
final_rank: 2

code_snippet: //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;

	}
}
accuracy: 1
time_ms: 190
memory_kb: 1164
final_rank: 3

code_snippet: #include <iostream>
using namespace std;

int main()
{
	int i,n,m,hon[100],max,dan,temp;
	while(cin >> m >> n, n || m){
		max = 0;
		for(i = 0;i < n;i++){
			cin >> hon[i];
			if(max < hon[i])
				max = hon[i];
		}
		dan = m + 1;
		for(i = max;dan > m;i++){
			dan = 1;
			temp = 0;
			for(int j = 0;j < n;j++){
				if(temp + hon[j] <= i){
					temp += hon[j];
				} else {
					temp = hon[j];
					dan++;
				}
			}
		}
		cout << i - 1 << endl;
	}
	return 0;
}
accuracy: 1
time_ms: 290
memory_kb: 1160
final_rank: 5

code_snippet: #include<iostream>
#include<vector>
using namespace std;
int main(){
  int n,m;
  while(cin >>n>>m,n||m){
    vector<int> book;
    int x,ans = 0;
    for(int i=0; i<m; i++){
      cin >>x;
      book.push_back(x);
      if(ans<x){ans = x;}
    }
    for(int i;;ans++){
      int last = ans,l = n;
      for(i=0; i<m; i++){
	if(last < book[i]){
	  last = ans;
	  l--;
	  if(l == 0){break;}
	  last-=book[i];
	}
	else{last-=book[i];}
      }
      if(i == m){cout <<ans<<endl;break;}
    }
  }
  return 0;
}
accuracy: 1
time_ms: 190
memory_kb: 1216
final_rank: 4