Solved: D problem - round 247, Codeforces.