Kickstart 2019 : Round F- Problem Flattening

Problem: Kick Start - Google’s Coding Competitions

Can anyone please explain me the approach to the above mentioned problem. ?

Link to the solution with comments for understanding