Solved problem B in contest Codeforces Round #284 (Div. 2)