AUCTION - Going Once, Going Twice, Gone!

Tác giả: ladpro98

Ngôn ngữ: Pascal

program auction;
uses    math;
var     n,m,price,res,i:longint;
        a:array[1..1000] of longint;

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     pivot,i,j:longint;
begin
        if l>=r then exit;
        pivot:=a[random(r-l+1)+l];
        i:=l;j:=r;
        repeat;
                while a[i]<pivot do inc(i);
                while a[j]>pivot 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;
begin
        readln(n,m);
        for i:=1 to m do readln(a[i]);
        sort(1,m);
        for i:=m downto (m-min(m,n)+1) do
        begin
                if res<(m-i+1)*a[i] then
                begin
                        res:=(m-i+1)*a[i];
                        price:=a[i];
                end;
        end;
        write(price,' ',res);
end.

Download