{$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, minl: 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;
  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 := 1000000;
    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 then begin
        l := m;
      end else begin
        r := m;
      end;
    end;
    z := o[q[l]] - int64(l) * q[l];
    if (i <= q[l]) then z := z - (v[i] - l);
    assert(v[i] + z > l);
    s := s + z;
    if (s < mins) then begin
      mins := s;
      mini := ind[i];
      minl := l;
    end;
  end;

  writeln(mins);
  writeln(mini);
  for i := 1 to n do begin
    vv[ind[i]] := v[i];
  end;
  for i := 1 to n do if (vv[i] > minl) and (i <> mini) then begin
    vv[mini] := vv[mini] + (vv[i] - minl);
    vv[i] := minl;
  end;
  for i := 1 to n do write(vv[i], ' ');
end.