My solution for codeforces.com