Place the tiles problem

problem - https://hack.codingblocks.com/app/contests/141/p/739
soln - https://ide.codingblocks.com/s/170153

getting tle in some of the cases.