Help for Codeforces problem :Energy exchange

Here is the link of question :
https://codeforces.com/problemset/problem/68/B

How can I solve this question? Thanks