Problem: I have a cylindrical vessel of height $H$ and radius $R$. There are two liquid layers in the vessel. The first has density $D_1$ and height $h_1$, the second has density $D_2$ and height $h_2$. The second liquid is floating on the first liquid (thus $D_2 < D_1$) and they are both entirely within the vessel ($h_1+h_2 \le H$).
Now I have a cube of density $D$ and edge length $n$. The cube fits into the vessel ($n \le \sqrt2R$) but it might not be completely inside it ($n>H$ is possible). I place it into the vessel such that one side is horizontal (the cube is not lopsided). Some liquid might overflow as a result. How do I use Archimedes' principle to calculate the topmost liquid level after placing the cube? I can ignore damping and other physical effects.
EDIT: Note that the only values I know are $H, R, D_1, h_1, D_2, h_2, n$ and their abovementioned constraints.
My workings: I tried using the formula directly, where $\text{weight of cube}=\text{weight of displaced liquid}$. I first get $Dn^3=D_2n^2s_2$ where $s_2$ is the height submerged in the second liquid. If $s_2 \le h_2$ then the cube is only submerged in the second liquid. So the answer is $\min(H, h_1+h_2+{n^2s_2\over\pi R^2})$, accounting for overflows. This case is simple.
But if $s_2>h_2$ then the cube can be submerged in the first liquid too. I thought of subtracting the weight of the second liquid displaced to obtain the height submerged in first liquid, but realize that I don't know that. The weight displaced depends on the final liquid height, which can be affected by overflows, whether the cube fits totally within the two liquids, etc. In fact, it can be the case that after the liquid level rises, the cube now displaces more of the second liquid, causing it to not be submerged in the first (is that even possible?) It seems very messy and I have no idea how to start. :(
Does anyone have a nice solution to this problem?