{
TASK: SLIDESJ
LANG: PASCAL
}
program Uzd_Slides;

    type Slides = record
         nr, ilgis: Integer;
    end;

    const MAX = 1010;

    var
        S   : array[1..MAX] of Slides;
        N   : Integer;
        Ats : Integer;

procedure Skaityk;
var
    f : Text;
    i : Integer;
begin
    Assign(f, 'SLIDES.DAT'); Reset(f);
    Readln(f, N);
    for i := 1 to 2*N do begin
        Readln(f, S[i].ilgis);
        S[i].nr := i;
    end;
    Close(f);
end;

procedure Rasyk;
var
    f : Text;
    i : Integer;
begin
    Assign(f, 'SLIDES.REZ'); Rewrite(f);
    Writeln(f, Ats);
    i := 1;
    repeat
        Writeln(f, S[i].nr, ' ', S[i+1].nr);
        i := i + 2;
    until (i > 2*N);
    Close(f);
end;

procedure Sukeisk(var a, b : Slides);
var
    t : Slides;
begin
    t := a;
    a := b;
    b := t;
end;

    var
        i, j : Integer;

begin
    Skaityk;

    { Iðrikiuojame slides didëjimo tvarka }
    for j := 2*N downto 1 do
        for i := 1 to (j - 1) do
            if (S[i].ilgis > S[i+1].ilgis) then
                Sukeisk(S[i], S[i+1]);

    { Suskaièiuojame reikalingà sumà }
    Ats := 0;
    i   := 1;
    repeat
        Ats := Ats + (S[i+1].ilgis - S[i].ilgis);
        i   := i + 2;
    until (i > 2*N);

    Rasyk;
end.
