How to solve problem Layer Cake from 2015-16 ACM ICPC NEERC ?

codeforces
problem

#1

link to the problem : http://codeforces.com/contest/589/problem/B

I’m not able to think of any approach for this, I have seen some solutions (code), but I’m still not able to understand.