{$q+,r+,o-}
{$apptype console}
uses
  sysutils, math;

var
  i, n: integer;
  p, v, vv, ind: array[1..100000] of integer;
  o: array[0..100000] of int64;
  q: array[0..1000000] of integer;
  mins, s, z: int64;
  l, r, m, mini, minr: integer;

procedure sort(l, r: integer);
var
  i, j, x, t: integer;
begin
  i := l;
  j := r;
  x := v[l + random(r - l + 1)];
  while (i <= j) do begin
    while (v[i] > x) do inc(i);
    while (v[j] < x) do dec(j);
    if i <= j then begin
      t := v[i]; v[i] := v[j]; v[j] := t;
      t := p[i]; p[i] := p[j]; p[j] := t;
      t := ind[i]; ind[i] := ind[j]; ind[j] := t;
      inc(i);
      dec(j);
    end;
  end;
  if (i < r) then sort(i, r);
  if (l < j) then sort(l, j);
end;

begin
  reset(input, 'elect.in');
  rewrite(output, 'elect.out');
  read(n);
  assert((n >= 1) and (n <= 100000));
  for i := 1 to n do begin
    read(v[i]);
    assert((v[i] >= 1) and (v[i] <= 1000000));
    read(p[i]);
    assert(((p[i] >= 1) and (p[i] <= 1000000)) or (p[i] = -1));
  end;
  if n = 1 then begin
    writeln(p[1]);
    writeln(1);
    writeln(v[1]);
    close(output);
    exit;
  end;
  for i := 1 to n do ind[i] := i;
  sort(1, n);

  q[0] := n;
  for i := 1 to 1000000 do begin
    q[i] := q[i - 1];
    while (q[i] > 0) and (v[q[i]] = i) do dec(q[i]);
  end;

  o[0] := 0;
  for i := 1 to n do o[i] := o[i - 1] + v[i];

  mins := 100000000000;
  for i := 1 to n do if (p[i] >= 0) then begin
    s := p[i];
    l := 0;
    r := 1000001;
    while (r > l + 1) do begin
      m := (l + r) div 2;
      z := o[q[m]];
      z := z - int64(m) * q[m];
      if (i <= q[m]) then z := z - (v[i] - m);
      if (v[i] + z) > m + 1 then begin
        l := m;
      end else begin
        r := m;
      end;
    end;
    s := s + (r + 1) - v[i];
    if (s < mins) then begin
      mins := s;
      mini := i;
      minr := r;
    end;
  end;

  writeln(mins);
  writeln(ind[mini]);

  for i := 1 to n do begin
  	if (i <> mini) and (v[i] > minr) then begin
  		v[mini] := v[mini] + v[i] - minr;
  		v[i] := minr;
  	end;
  end;
  i := 1;
  while v[mini] < minr + 1 do begin
  	if (i = mini) then inc(i);
  	if (v[i] >= minr + 1 - v[mini]) then begin
  		v[i] := v[i] - (minr + 1 - v[mini]);
  		v[mini] := minr + 1;
  	end else begin
  		v[mini] := v[mini] + v[i];
  		v[i] := 0;
  	end;
  	inc(i);
  end;

  for i := 1 to n do begin
    vv[ind[i]] := v[i];
  end;
  for i := 1 to n do write(vv[i], ' ');
end.