var n,i,j,k,last,sum,t1,t2:longint; a,s,h1,h2,p1,p2,w1,w2:array[0..100002] of longint; procedure down1(v0:longint); var i1,j1,rec1,recp:longint; begin i1:=v0; j1:=v0 shl 1; rec1:=h1[v0]; recp:=p1[v0]; while j1<=k do begin if(j1<k)and(h1[j1+1]>h1[j1]) then inc(j1); if rec1<h1[j1] then begin h1[i1]:=h1[j1]; w1[p1[j1]]:=i1; p1[i1]:=p1[j1]; i1:=j1; j1:=i1 shl 1; end else j1:=k+1; end; h1[i1]:=rec1; p1[i1]:=recp; w1[p1[i1]]:=i1; end; procedure down2(v0:longint); var i1,j1,rec1,recp,recw:longint; begin i1:=v0; j1:=v0 shl 1; rec1:=h2[v0]; recp:=p2[v0]; while j1<=k do begin if(j1<k)and(h2[j1+1]>h2[j1]) then inc(j1); if rec1<h2[j1] then begin h2[i1]:=h2[j1]; w2[p2[j1]]:=i1; p2[i1]:=p2[j1]; i1:=j1; j1:=i1 shl 1; end else j1:=k+1; end; h2[i1]:=rec1; p2[i1]:=recp; w2[p2[i1]]:=i1; end; begin //assign(input,'salesman.in'); //assign(output,'salesman.out'); //reset(input); //rewrite(output); readln(n); for i:=1 to n do begin read(s[i]); s[i]:=s[i] shl 1; end; readln; for i:=1 to n do read(a[i]); h1:=a; for i:=1 to n do begin p1[i]:=i; w1[i]:=i; h2[i]:=a[i]+s[i]; p2[i]:=i; w2[i]:=i; end; k:=n; for i:=n shr 1 downto 1 do down1(i); for i:=n shr 1 downto 1 do down2(i); i:=1; writeln(h2[1]); sum:=h2[1]; last:=p2[1]; t1:=w1[p2[1]]; h1[t1]:=h1[k]; p1[t1]:=p1[k]; w1[p1[t1]]:=t1; h2[1]:=h2[k]; p2[1]:=p2[k]; w2[p2[1]]:=1; dec(k); down2(1); down1(t1); for i:=2 to n do begin if p1[1]>last then t1:=h1[1]+s[p1[1]]-s[last] else t1:=h1[1]; if p2[1]>=last then t2:=h2[1]-s[last] else t2:=a[p2[1]]; if t1>t2 then begin inc(sum,t1); writeln(sum); if p1[1]>last then last:=p1[1]; t2:=w2[p1[1]]; h2[t2]:=h2[k]; p2[t2]:=p2[k]; w2[p2[t2]]:=t2; h1[1]:=h1[k]; p1[1]:=p1[k]; w1[p1[1]]:=1; dec(k); down1(1); down2(t2); end else begin inc(sum,t2); writeln(sum); if p2[1]>last then last:=p2[1]; t1:=w1[p2[1]]; h1[t1]:=h1[k]; p1[t1]:=p1[k]; w1[p1[t1]]:=t1; h2[1]:=h2[k]; p2[1]:=p2[k]; w2[p2[1]]:=1; dec(k); down2(1); down1(t1); end; end; //close(input); //close(output); end. /************************************************************** Problem: 2343 User: admin Language: Pascal Result: Wrong Answer ****************************************************************/