NDCCARD - Các lá bài Blackjack

Tác giả: ladpro98

Ngôn ngữ: Pascal

program ndccard;
uses    math;
const   maxV=22222;
        maxN=10004;
        fi='';
var     check:array[1..maxV] of boolean;
        a:array[1..maxN] of longint;
        n,m,res:longint;

procedure input;
var     inp:text;
        i:longint;
begin
        assign(inp,fi);
        reset(inp);
        read(inp,n,m);
        for i:=1 to n do
        read(inp,a[i]);
        close(inp);
        res:=maxV*maxV+1;
end;

procedure swap(i,j:longint);
var     t:longint;
begin
        t:=a[i];
        a[i]:=a[j];
        a[j]:=t;
end;

procedure sort(l,r:longint);
var     i,j,p:longint;
begin
        if l>=r then exit;
        p:=a[random(r-l+1)+l];
        i:=l;j:=r;
        repeat
                while a[i]<p do inc(i);
                while a[j]>p do dec(j);
                if i<=j then
                begin
                        if i<j then swap(i,j);
                        inc(i);dec(j);
                end;
        until i>j;
        sort(l,j);sort(i,r);
end;

procedure process;
var     i,j:longint;
begin
        for i:=1 to n-1 do
        for j:=i+1 to n do
        check[a[i]+a[j]]:=true;
        j:=a[n]+a[n-1];
        for i:=1 to n-2 do
        begin
                while (j>=1) and ((not check[j]) or (a[i]+j>m)) do dec(j);
                if (a[i]+j<=m) then
                res:=min(res,m-a[i]-j);
        end;
end;

begin
        input;
        sort(1,n);
        process;
        write(m-res);
end.

Download