I was trying to solve some past problems. I got stuck on these 2 problems. I read the editorials but didn't quite understood it It would be really nice if someone can explain what we have to do in these problems and how we can implement them.

(https://codeforces.com/contest/1249/problem/D1)

(https://codeforces.com/contest/1249/problem/C2)

Thanks!