Help in codeforces problem

Problem : Problem - D - Codeforces

Can anyone help me with problem , I read the editorial but unable to understood ?