Cod sursa(job #637010)

Utilizator ctlin04UAIC.VlasCatalin ctlin04 Data 20 noiembrie 2011 09:49:52
Problema Ferma2 Scor 90
Compilator fpc Status done
Runda .com 2011 Marime 2.14 kb
Program ferma2;
 var a:array [0..1001,0..1001] of longint;
     b1:array [1..1 shl 17] of char;
     i,j,n,min,s,k,cnt,x,g,h,aux,l,p:longint;
     s1:string;
     sum:int64;
     fi,fo:text;
function citire:longint;
 var aux:longint;
begin
aux:=0;
 while (s1[p]>='0') and (s1[p]<='9') and (p<=l) do begin
                                            aux:=aux*10+ord(s1[p])-48;
                                             if p=l then begin
                                                        read(fi, s1);
                                                         p:=0;
                                                          l:=length(s1);
                                                         end;
                                                      inc(p);
                                                  end;
 while ((s1[p]<'0') or (s1[p]>'9')) and (s1<>'') do begin
                                                inc(p);
                                                if p>l then begin
                                                            read(fi, s1);
                                                              p:=1;
                                                               l:=length(s1);
                                                            end;
                                                  end;
citire:=aux;
end;
begin
assign(fi,'ferma2.in');
 assign(fo,'ferma2.out');
settextbuf(fi,b1);
 reset(fi); rewrite(fo);
  readln(fi,n,k);
 for i:=1 to n do begin
 read(fi,s1); l:=length(s1); p:=1;
  for j:=1 to i do begin s:=citire; a[i,j]:=a[i,j-1]+s; end;
   sum:=sum+a[i,i];
    readln(fi);
   end;
  min:=100000000;
 for i:=1 to k+1 do
  for j:=1 to i do begin
                   x:=1; cnt:=1; s:=0; g:=i; h:=j;
                    while cnt<=n-k do begin
                                    s:=s+a[g,h]-a[g,h-x];
                                     if s>=min then break;
                                     inc(x); inc(g); inc(h); inc(cnt);
                                       end;
                   if s<min then min:=s;
                   end;
 write(fo,sum-min);
 close(fo);
end.