Output #1
10
13
First, the $1$\-st query makes $A=(5,1,3,3)$ and $B=(1,2)$. In this case, the operations in the subproblem proceed as follows.
* Initialize $X=\lbrace 5,1,3,3\rbrace=\lbrace 1,3,3,5\rbrace$.
* Insert $B_1=1$ into $X$, making $X=\lbrace 1,1,3,3,5\rbrace$. Then, remove one median from $X$, making $X=\lbrace 1,1,3,5\rbrace$.
* Insert $B_2=2$ into $X$, making $X=\lbrace 1,1,2,3,5\rbrace$. Then, remove one median from $X$, making $X=\lbrace 1,1,3,5\rbrace$.
The sum of elements in $X$ when all operations are finished is $1+1+3+5=10$.
Next, the $2$\-nd query makes $A=(5,1,3,3)$ and $B=(5,2)$. In this case, the operations in the subproblem proceed as follows.
* Initialize $X=\lbrace 5,1,3,3\rbrace=\lbrace 1,3,3,5\rbrace$.
* Insert $B_1=5$ into $X$, making $X=\lbrace 1,3,3,5,5\rbrace$. Then, remove one median from $X$, making $X=\lbrace 1,3,5,5\rbrace$.
* Insert $B_2=2$ into $X$, making $X=\lbrace 1,2,3,5,5\rbrace$. Then, remove one median from $X$, making $X=\lbrace 1,2,5,5\rbrace$.
The sum of elements in $X$ when all operations are finished is $1+2+5+5=13$.