Codeforces solution in python3