1 program hehe; 2 var 3 n,m,i,j:longint; 4 c:array[0..50] of longint; 5 6 function min(a,b:longint):longint; 7 begin 8 if a>b then exit(b); 9 exit(a); 10 end; 11 12 function ok(a:longint):boolean; 13 var 14 f,t:longint; 15 begin 16 t:=min(a,m); 17 for f:=1 to n do 18 begin 19 if c[f]<a then 20 t:=t-a+c[f]; 21 if t<0 then exit(false); 22 end; 23 exit(true); 24 end; 25 26 function ef(l,r:longint):longint; 27 var 28 mid:longint; 29 begin 30 if l=r then exit(l); 31 mid:=(l+r)>>1; 32 if ok(mid+1) then 33 exit(ef(mid+1,r)) 34 else exit(ef(l,mid)); 35 end; 36 37 begin 38 readln(n,m); 39 for i:=1 to n do read(c[i]); 40 writeln(ef(1,maxlongint-1)); 41 end.
原文:http://www.cnblogs.com/chensiang/p/4581766.html