VCOWFLIX - Đi xem phim
Tác giả: ladpro98
Ngôn ngữ: Pascal
program vcowflix;
uses math;
const fi='';
var a:array[1..16] of longint;
c,n,res:longint;
procedure input;
var inp:text;
i:longint;
begin
assign(inp,fi);
reset(inp);
readln(inp,c,n);
for i:=1 to n do readln(inp,a[i]);
close(inp);
end;
procedure duyet(tong,i:longint);
var j:longint;
begin
if (tong>c) or (i>n) then exit;
res:=max(res,tong);
for j:=i+1 to n do
duyet(tong+a[j],j);
end;
procedure swap(i,j:longint);
var t:longint;
begin
t:=a[i];
a[i]:=a[j];
a[j]:=t;
end;
procedure sort(i,j:longint);
var pivot:longint;
l,r:longint;
begin
if l>= r then exit;
l:=i;r:=j;
pivot:=a[random(r-l+1)+l];
repeat
while a[l]>pivot do inc(l);
while a[r]<pivot do dec(r);
if l<=r then
begin
if l<r then swap(l,r);
inc(l);
dec(r);
end;
until l>r;
sort(i,r);
sort(l,j);
end;
begin
input;
sort(1,n);
duyet(0,0);
write(res);
end.