Getting TLE in Rectangle Cutting(CSES PROBLEM)

I get stuck. Don’t know why I am getting TLE in two test cases.

Problem link:CSES - Rectangle Cutting
Solution link:https://cses.fi/paste/5ae62c7db2d5d64029b7d0/