My own solution for Codeforces problem