import java.util.*;
import java.io.*;

public class carwash_as implements Runnable {
    class Car {
        int i;
        long a;
        long b;
        long sa;
        long sb;

        Car(int i, long a, long b) {
            this.i = i;
            this.a = a;
            this.b = b;
            sa = -1;
            sb = -1;
        }
    }

    public void solve() throws IOException {
        // Scanner in = new Scanner(new File("carwash.in"));
        // PrintWriter out = new PrintWriter("carwash.out");
        Scanner in = new Scanner(System.in);
        PrintWriter out = new PrintWriter(System.out);

        int n = in.nextInt();
        Car[] cars = new Car[n];
        for (int i = 0; i < n; i++) {
            cars[i] = new Car(i, in.nextInt(), in.nextInt());
        }

        Car l = new Car(-1, 0, 0);
        Car r = new Car(-1, 0, 0);
        ArrayList<Car> left = new ArrayList<Car>();
        ArrayList<Car> right = new ArrayList<Car>();
        long ta = 0;
        long tb = 0;
        for (int i = 0; i < n; i++) {
            ta += cars[i].a;
            tb += cars[i].b;

            if (cars[i].a >= cars[i].b) {
                if (cars[i].a >= r.b) {
                    right.add(r);
                    r = cars[i];
                } else {
                    right.add(cars[i]);
                }
            } else {
                if (cars[i].b >= l.a) {
                    left.add(l);
                    l = cars[i];
                } else {
                    left.add(cars[i]);
                }
            }
        }

        Collections.reverse(left);
        ArrayList<Car> a = new ArrayList<Car>();
        ArrayList<Car> b = new ArrayList<Car>();
        a.addAll(left);
        b.addAll(left);
        a.addAll(right);
        b.addAll(right);
        if (ta - l.a < tb - r.b) {
            a.add(r);
            a.add(l);
            b.add(0, l);
            b.add(r);
        } else {
            a.add(0, l);
            a.add(r);
            b.add(0, r);
            b.add(1, l);
        }

        int ai = 0;
        int bi = 0;
        long cta = 0;
        long ctb = 0;
        while (ai < a.size() || bi < b.size()) {
            if ((cta <= ctb && ai < a.size()) || bi == b.size()) {
                Car prb = bi == 0 ? null : b.get(bi - 1);
                Car cra = a.get(ai);
                if (prb == null || prb.i != cra.i || cta > ctb) {
                    cra.sa = cta;
                } else {
                    cra.sa = ctb;
                }
                cta = cra.sa + cra.a;
                ai++;
            } else {
                Car pra = ai == 0 ? null : a.get(ai - 1);
                Car crb = b.get(bi);
                if (pra == null || pra.i != crb.i || ctb > cta) {
                    crb.sb = ctb;
                } else {
                    crb.sb = cta;
                }
                ctb = crb.sb + crb.b;
                bi++;
            }
        }

        out.println(Math.max(cta, ctb));
        for (int i = 0; i < n; i++) {
            out.println(cars[i].sa + " " + cars[i].sb);
        }

        in.close();
        out.close();
    }

    public void run() {
        try {
            solve();
        } catch (IOException e) {
        }
    }

    public static void main(String[] arg) {
        new Thread(new carwash_as()).start();
    }
}