Help in codeforces problem

Problem : https://codeforces.com/contest/1015/problem/D

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