Archimedes' principle for two liquid layers 
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?
 A: You do no say what information you know and do not know.  For example if the cube sinks and $h_1$ is big enough, it is possible that $s_2=0$.  But if you know $s_1$ and $s_2$ then it is easy.
The volume of liquid displaced is $(s_1+s_2)n^2$ so the extra height (ignoring overflows) is $\dfrac{(s_1+s_2)n^2}{\pi R^2 }.$ So the final overall height of the liquid is $$\min\left(H,h_1+h_2+\dfrac{(s_1+s_2)n^2}{\pi R^2 }\right)$$ and if you know $s_1$ and $s_2$ then you do not need to use the densities.
It is possible to calculate $s_1$ and $s_2$ using the information available including the densities, and that is where there are several cases to consider.
A: This will help for sure. Floating between two liquids
You only have to calculate the height. And the information you get for this is enough.
I used this formula to write a program in a contest in codeforces. The problem is same as yours. No change."Cocktail"
A: "how does it distinguish case a and b?"
Well,let's first clear our conceptions about the archimedes' principle. Then I think you will get your answer for yourself.
Archimedes' principle states that, a body immersed in a fluid is buoyed up by a force equal to the weight of the displaced fluid.
Focus on this."... a force equal to the weight of the displaced fluid."

In case a and b, our cube of edge length n displaced $s_1n^2$ volume from liquid 1 and $s_2n^2$ volume from liquid 2.
So,from what we focused on before, we get,total buoyant force is equal to $s_1n^2D_1g+s_2n^2D_2g$.
Now you see, two liquids are acting force on our cube but it is still steady!
Yes, we missed the force that is acting opposite. It is the weight of our cube,$n^3Dg$.
From Newtonian mechanics, for equllibrium condition, we get,
$s_1n^2D_1g+s_2n^2D_2g=n^3Dg$
=>$s_1D_1+s_2D_2=nD$
Our theoretical discussion ends here.
You may now distinguish case a and b easily.In case a,$s_2$ is lower than the increased height of liquid 2. We will find this case when $D_2<=D<D_1$ and $s_2=n-s_1$.
And in case b, $s_2$ is higher than the increased height of liquid 2 or $s_2 < n-s_1$. We will find this case in either ($D_2>D$ and increased height of liquid 2 < $x_2$) or ($D_2 <= D< D_1$ and $s_2 < n-s_1$).
In the first condition $D_2>D$ means our cube will float on liquid 2, where $x_2$ is the length of the part of the qube under the surface of liquid 2.
Now, I think, you have understood the cases fully. I cannot further proceed to explain the other cases and calculations as they are the part of the algorithm of cocktail problem. I could post my accepted code here,but you and other would miss the true pleasure of discovery. So, you should think logically and visualise the cases until you find yourself playing with a cube and some fluids and then shouting 'Eurekaaa.....!'.     
