
(**
 * Stacking Numbers
 * Problem 3, AIC 2003
 * Pascal Sample Solution (slow but simple, scores 50%)
 *)

(**
 * This solution simply runs through all possible choices for the six
 * numbers at the bottom of the pyramid.  From these it selects the
 * combination that gives the highest possible score (number at the
 * top of the pyramid).
 *
 * If there are N numbers to choose from in the input set, we expect
 * a bit under N^6 combinations overall for large N.  If N = 32 (the
 * maximum case allowed in the problem statement) then this is about
 * 1,000,000,000 combinations which is too many for a 2s time limit.
 *)

program Stack;

{ Constants from the problem statement: }
const
    MAXSET = 100;      { Maximum size of the input set }

var
    { Variables holding the input data: }
    base : longint;                   { Modular base to use with addition }
    inputSet : array[1 .. MAXSET] of longint;
                                      { Set of available starting numbers
                                            to choose from }
    setSize : longint;                { Number of available starting numbers
                                            to choose from }

    { Variables describing the current and best arrangements: }
    start : array[1 .. 6] of longint; { The six numbers at the bottom of
                                            the pyramid }
    ans : array[1 .. 6] of longint;   { The six numbers that give the best
                                            possible final score }
    currScore, bestScore : longint;   { The best possible final score }

    { Input and output files: }
    inFile : text;
    outFile : text;

    { Miscellaneous loop indexes: }
    i1, i2, i3, i4, i5, i6 : integer;
    i : integer;

(**
 * Calculates the score at the top of the pyramid from the six numbers
 * currently at the bottom of the pyramid.  The numbers at the bottom of
 * the pyramid are start[1..6].
 *)
function Score : longint;
    var
        left, right : longint;
    begin
        left := (start[1] + (4 * start[2]) + (6 * start[3]) +
            (4 * start[4]) + start[5]) mod base;
        right := (start[2] + (4 * start[3]) + (6 * start[4]) +
            (4 * start[5]) + start[6]) mod base;
        Score := left * right;
    end;

(**
 * The main program.
 *)
begin
    { Open the I/O files. }
    assign(inFile, 'stackin.txt');
    assign(outFile, 'stackout.txt');
    reset(inFile);
    rewrite(outFile);

    { Read the input data. }
    readln(inFile, base, setSize);
    for i := 1 to setSize do
        read(inFile, inputSet[i]);

    { Search through all possible combinations.
      This is done with six nested for loops, one for each number at
      the bottom of the pyramid.
      Note that we ensure at each stage that these six numbers are
      distinct. }
    bestScore := -1;

    for i1 := 1 to setSize do begin
        start[1] := inputSet[i1];
        for i2 := 1 to setSize do begin
            if i2 = i1 then
                continue;
            start[2] := inputSet[i2];
            for i3 := 1 to setSize do begin
                if (i3 = i2) or (i3 = i1) then
                    continue;
                start[3] := inputSet[i3];
                for i4 := 1 to setSize do begin
                    if (i4 = i3) or (i4 = i2) or (i4 = i1) then
                        continue;
                    start[4] := inputSet[i4];
                    for i5 := 1 to setSize do begin
                        if (i5 = i4) or (i5 = i3) or (i5 = i2) or
                                (i5 = i1) then
                            continue;
                        start[5] := inputSet[i5];
                        for i6 := 1 to setSize do begin
                            if (i6 = i5) or (i6 = i4) or (i6 = i3) or
                                    (i6 = i2) or (i6 = i1) then
                                continue;
                            start[6] := inputSet[i6];

                            { We have a selection of six starting numbers.
                              See if the resulting score is better than
                              we've seen so far. }
                            currScore := score;
                            if currScore > bestScore then begin
                                bestScore := currScore;
                                for i := 1 to 6 do
                                    ans[i] := start[i];
                            end;
                        end;
                    end;
                end;
            end;
        end;
    end;

    { Output the best solution that was found. }
    writeln(outFile, bestScore);
    writeln(outFile, ans[1], ' ', ans[2], ' ', ans[3], ' ', ans[4], ' ',
        ans[5], ' ', ans[6]);

    { Tidy up. }
    close(inFile);
    close(outFile);
end.
