比赛 暑假培训五 评测结果 ATTTTTTTTA
题目名称 开心的金明 最终得分 20
用户昵称 Hamster 运行时间 0.000 s
代码语言 Pascal 内存使用 0.00 MiB
提交时间 2008-07-22 11:29:59
显示代码纯文本
program happy;
var
  w:array[1..25] of longint;
  v:array[1..25] of longint;
  x:array[1..25] of longint;
  i,j,n,m,nn:longint;
  ss,s:longint;
  fin,fout:text;

procedure init;
begin
  readln(fin,n,m);
  for i:=1 to m do
  begin
    readln(fin,w[i],v[i]);
    x[i]:=0;
  end;
  s:=0;
  ss:=0;
end;

procedure print;
begin
  writeln(fout,s);
  close(fin);
  close(fout);
end;

procedure search(t:longint);
var
  k:longint;
begin
  k:=0;
  repeat
    inc(k);
    if (x[k]=0) and (nn>w[k]) then
    begin
      nn:=nn-w[k];
      x[k]:=1;
      ss:=ss+v[k]*w[k];
      if (nn=0) or (t=m) or (k=m) then
      begin
        if ss>s then s:=ss
        else search(t+1);
      end
      else search(t+1);
      nn:=nn+w[k];
      x[k]:=0;
      ss:=ss-v[k]*w[k];
    end;
  until k=m;
end;

begin
  assign(fin,'happy.in');
  reset(fin);
  assign(fout,'happy.out');
  rewrite(fout);

  init;
  nn:=n;
  search(1);
  print;

end.