var
 t:array[1..400000]of int64;
 t1:array[1..400000]of longint;
 r:array[1..100000]of int64;
 pi,st,p,a,tt:array[1..100000]of longint;
 mx,sum:int64;
 stn,q,n,i,right:longint;

function max(a,b:longint):longint;
begin
 if a>b then max:=a else max:=b;
end;

function min(a,b:longint):longint;
begin
 if a<b then min:=a else min:=b;
end;

procedure modify(v,tl,tr,pos:longint;val:int64);
var
 tm:longint;
begin
 if tl=tr then
 begin
  t[v]:=val;
  t1[v]:=pos;
 end else
 begin
  tm:=(tl+tr)shr 1;
  if pos<=tm then modify(v shl 1,tl,tm,pos,val)
   else modify(v shl 1+1,tm+1,tr,pos,val);
  t[v]:=t[v shl 1]+t[v shl 1+1];
  t1[v]:=max(t1[v shl 1],t1[v shl 1+1]);
 end;
end;

procedure get(v,tl,tr,l,r:longint);
var
 tm:longint;
begin
 if l>r then exit;
 if (tl=l)and(tr=r) then
 begin
  sum:=sum+t[v];
  right:=max(right,t1[v]);
  exit;
 end;
 tm:=(tl+tr)shr 1;
 get(v shl 1,tl,tm,l,min(r,tm));
 get(v shl 1+1,tm+1,tr,max(l,tm+1),r);
end;

begin
 assign(input,'assault.in');
 reset(input);
 assign(output,'assault.out');
 rewrite(output);

 readln(n);
 for i:=1 to n do read(a[i]);
 readln;
 for i:=1 to n do read(tt[i]);

 fillchar(t,sizeof(t),0);
 fillchar(t1,sizeof(t1),0);
 fillchar(r,sizeof(r),0);
 fillchar(p,sizeof(p),0);

 for i:=1 to n do
 begin
  sum:=0;
  right:=0;
  if tt[i]>1 then get(1,1,100000,1,tt[i]-1);

  if sum+a[i]>r[tt[i]] then
  begin
   r[tt[i]]:=sum+a[i];
   modify(1,1,100000,tt[i],a[i]);
   p[tt[i]]:=right;
   pi[tt[i]]:=i;
  end;
 end;

 mx:=0;
 for i:=1 to 100000 do if r[i]>mx then
 begin
  mx:=r[i];
  q:=i;
 end;

 stn:=0;
 while q<>0 do
 begin
  inc(stn);
  st[stn]:=pi[q];
  q:=p[q];
 end;

 writeln(mx);
 writeln(stn);
 for i:=stn downto 2 do write(st[i],' ');
 writeln(st[1]);
end.
