Codeforces 877D

Can Anybody give a better explanation to this problem http://codeforces.com/contest/877/problem/D