AUCTION - Going Once, Going Twice, Gone!

Tác giả: ll931110

Ngôn ngữ: Pascal

Program AUCTION;
        Const
                input  = '';
                output = '';
        Var
                n,m: longint;
                  a: array[1..1000] of longint;

Procedure init;
          Var
                f: text;
                i: longint;
          Begin
                Assign(f, input);
                        Reset(f);
                        Readln(f, n, m);
                        For i:= 1 to m do readln(f, a[i]);
                Close(f);
          End;

Procedure quicksort;

        Procedure partition(l,h: longint);
                Var
                        i,j,p,x: longint;
                Begin
                        If l >= h then exit;
                        p:= a[random(h - l + 1) + l];

                        i:= l;          j:= h;
                Repeat
                        While a[i] < p do inc(i);
                        While a[j] > p do dec(j);

                        If i <= j then
                                Begin
                                        If i < j then
                                                Begin
                                                        x:= a[i];
                                                        a[i]:= a[j];
                                                        a[j]:= x;
                                                End;
                                        inc(i);
                                        dec(j);
                                End;
                Until i > j;

                partition(l,j);
                partition(i,h);
          End;

       Begin
        partition(1,m);
       End;

Procedure solve;
          Var
                  f: text;
                i,s: longint;
                max,val: longint;
          Begin
                max:= 0;
                val:= 0;

                Assign(f, output);
                        Rewrite(f);
                        For i:= 1 to m do
                                Begin
                                        If n <= m - i + 1 then s:= a[i] * n
                                              else s:= a[i] * (m - i + 1);
                                        If max < s then
                                                Begin
                                                        max:= s;
                                                        val:= a[i];
                                                End;
                                End;
                        Writeln(f, val, ' ', max);
                Close(f);
          End;

Begin
        init;
        quicksort;
        solve;
End.


Download