ACMNB - ACM

Tác giả: ladpro98

Ngôn ngữ: Pascal

program acmnb;
uses    math;
const   fi='';
        maxN=800004;
type    e=record
        x,y,s:longint;
        end;
var     a:array[1..maxN] of e;
        n:longint;

procedure input;
var     f:text;
        i:longint;
begin
        assign(f,fi);
        reset(f);
        readln(f,n);
        for i:=1 to n shl 1 do
        begin
                readln(f,a[i].x,a[i].y);
                a[i].s:=a[i].x-a[i].y;
        end;
        close(f);

end;

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

procedure sort(l,r:longint);
var     p,i,j:longint;
begin
        if l>=r then exit;
        p:=a[random(r-l+1)+l].s;
        i:=l;j:=r;
        repeat
                while a[i].s<p do inc(i);
                while a[j].s>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 cal;
var     i,res:longint;
begin
        res:=0;
        for i:=1 to n do
        inc(res,a[i].x);
        for i:=n+1 to n shl 1 do
        inc(res,a[i].y);
        write(res);
end;

begin
        input;
        sort(1,n shl 1);
        cal;
end.

Download