比赛 noip-081029 评测结果 AAAAAEEEEE
题目名称 取数字问题 最终得分 50
用户昵称 lc 运行时间 0.000 s
代码语言 Pascal 内存使用 0.00 MiB
提交时间 2008-10-29 21:41:14
显示代码纯文本
program ex10_29_1;
 const
  maxsum=1000;
 var
     a:array[1..10,1..10] of integer;
     f:array[0..10,0..10,-maxsum-10..maxsum+10] of boolean;
     n,m:integer;

procedure init;
 var
    i,j:integer;
 begin
 readln(n,m);
 for i:=1 to n do
  for j:=1 to m do
  read(a[i,j]);
 end;

procedure main;
 var
    i,j,k,ans:integer;
    ok:boolean;

 begin
  f[1,1,a[1,1]]:=true;
  for i:=1 to n do
   for j:=1 to m do
    for k:=-maxsum to maxsum do
    f[i,j,k]:=f[i,j,k] or f[i-1,j,k-a[i,j]] or f[i,j-1,k-a[i,j]];

  ok:=false;
  for ans:=1 to maxsum do
  if f[n,m,ans] then begin ok:=true; break; end;
  if ok then writeln(ans)
        else writeln(-1);
 end;


begin
 assign(input,'number.in');
 assign(output,'number.out');
 reset(input); rewrite(output);
 init;
 main;
 close(input); close(output);
end.