- 花生采摘
- 2015-09-05 18:39:38 @
var n,m,x,i,j,max,l,h,s,t1,t2:longint;
a:array[1..1000,1..1000]of integer;
begin
read(n,m,x);
for i:=1 to n do
for j:=1 to m do begin
read(a[i,j]);
if a[i,j]>max then begin
max:=a[i,j];h:=i;l:=j;
end;
end;
x:=x-l-1;s:=s+max;a[h,l]:=0;max:=0;
if x-h<0 then write(0) else begin
repeat
for i:=1 to n do
for j:=1 to m do
if a[i,j]>max then begin
max:=a[i,j];t1:=i;t2:=j;
end;
x:=x-abs(t1-h)-abs(t2-l)-1;
if x-t1<0 then break else begin
s:=s+max;a[t1,t2]:=0;max:=0;h:=t1;l:=t2;
end;
until x-h<0;
write(s);
end;
end.