# BZOJ1044: [HAOI2008]木棍分割

## 1044: [HAOI2008]木棍分割

Time Limit: 10 Sec  Memory Limit: 162 MB
Submit: 1580  Solved: 567
[Submit][Status]

3 2
1
1
10

## Sample Output

10 2

n<=50000, 0<=m<=min(n-1,1000).
1<=Li<=1000.

## Source

1.感觉此题很难写

2.写程序的时候，一要易懂，不容易出错，而才是简短，优美，不要一上来就想着可以怎么怎么减少代码，否则会把自己搞晕

 1 const maxn=50000+100;p=10007;
2 var i,j,n,m,x,ans,cnt,l,r,mid,t,sum:longint;
3     q,pre,len,s:array[0..maxn] of longint;
4     f:array[0..maxn,0..1] of longint;
5 procedure init;
6  begin
8   for i:=1 to n do begin readln(len[i]);s[i]:=s[i-1]+len[i];end;
9   s[n+1]:=maxlongint;
10  end;
11 function test(x:longint):boolean;
12  var i,j,sum:longint;
13  begin
14   sum:=0;j:=0;
15   for i:=1 to n do
16    if sum+len[i]<=x then inc(sum,len[i])
17    else begin inc(j);sum:=len[i];end;
18  exit(j<=m);
19  end;
20 procedure main;
21  begin
22   l:=0;r:=1000000000;
23   while l<r do
24    begin
25      mid:=(l+r)>>1;
26      if test(mid) then r:=mid else l:=mid+1;
27    end;
28   ans:=l;
29   j:=0;