Help for Codeforces problem :Energy exchange

Here is the link of question :

How can I solve this question? Thanks