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

public class elect_is implements Runnable {
    private static final String FILENAME = elect_is.class.getName().substring(0, elect_is.class.getName().indexOf('_'));
    
    File inFile = new File(FILENAME + ".in");
    PrintWriter out;
    BufferedReader in;

    private StringTokenizer st;

    elect_is() {     
        try {
            in = new BufferedReader(new FileReader(inFile));
            st = new StringTokenizer(in.readLine());
            out = new PrintWriter(new File(FILENAME + ".out"));
        } catch (IOException e) {
            e.printStackTrace();
            System.exit(239);           
        }       
    }
    
    int nextInt() throws IOException {
        if (!st.hasMoreTokens()) {
            st = new StringTokenizer(in.readLine());
        }
        return Integer.parseInt(st.nextToken());
    }
    
    class C implements Comparator<Integer> {
    	int[] v;
    	int[] p;
    	C(int[] v, int[] p) {
    		this.v = v;
    		this.p = p;
    	}
		public int compare(Integer o1, Integer o2) {
			int t = v[o2] - v[o1];
			if (t == 0) {
				return ((p[o2] == -1) ? (int)1e7 : p[o2]) 
					 - ((p[o1] == -1) ? (int)1e7 : p[o1]); 
			} else
				return t;
		}
    }
    
    
    public void run() {
        try {
            int n = nextInt();
            int i;
            int[] v = new int[n];
            int[] p = new int[n];
            int min = 0;
            for (i = 0; i < n; i++) {            	
            	v[i] = nextInt();
            	p[i] = nextInt();
            	if (p[i] == -1)
            		min = Math.max(min, v[i]);
            }
            Integer[] r = new Integer[n];
            for (i = 0; i < n; i++)
            	r[i] = i;
            Arrays.sort(r, new C(v, p));
            int[] ps = new int[n];
            ps[0] = 0;
            for (i = 1; i < n; i++)
            	ps[i] = ps[i - 1] + v[r[i - 1]];
            
            int res = Integer.MAX_VALUE;
            int resi = -1;
            int resLevel = -1;
            for (i = 0; i < n; i++) {
            	while (i < n - 1 && v[r[i + 1]] == v[r[i]]) {            		
            		i++;
            	}
            	int I = r[i];
            	if (p[I] == -1) {
            		continue;
            	}
            	int lb = 0;
            	int rb = i;
            	while (lb + 1 < rb) {
            		int x = (lb + rb) / 2;
            		if (ps[x] + v[I] > v[r[x]]) {
            			rb = x;
            		} else {
            			lb = x;
            		}
            	}
            	int d = (ps[rb] + v[I]) / (rb + 1);
            	if (d * (rb + 1) == (ps[rb] + v[I]))
            		d--;
            	int t = p[I] + (ps[rb] - d * rb);            	
            	if (res > t) {
            		res = t;
            		resi = I;
            		resLevel = d;
            	}
            }
            out.println(res);
            out.println(resi + 1);
            if (p[resi] == -1) {
            	throw new RuntimeException("Buying honest party");
            }
            int nres = p[resi];
            for (int j = 0; j < n; j++) {
				if (r[j] == resi || v[r[j]] < v[resi])
					break;
				v[resi] += v[r[j]] - resLevel;
								
				nres += v[r[j]] - resLevel;
				
				v[r[j]] = resLevel;							
			}
            if (nres != res) {
            	throw new RuntimeException("Wrong money calc");
            }
            for (int j = 0; j < n; j++) {
				out.print(v[j] + " ");				
			}
            out.println();
        } catch (IOException e) {
			e.printStackTrace();
		} finally {
            out.close();
        }
        
                
    }
    
    public static void main(String[] args) {
        new Thread(new elect_is()).start();
    }

}