VCOWFLIX - Đi xem phim

Tác giả: RR

Ngôn ngữ: Pascal

uses math;
var
  a:array[1..20] of longint;
  gh,sum,res,mask,i,n:longint;
begin
  read(gh,n);
  for i:=1 to n do read(a[i]);
  for mask:=0 to 1 shl n-1 do
    begin
      sum:=0;
      for i:=1 to n do
        begin
          if (mask shr (i-1)) and 1=1 then inc(sum,a[i]);
          if sum<=gh then res:=max(res,sum);
        end;
    end;

  writeln(res);
end.

Download