DRGNDEN - July long - Why is this working on Python but not on C++?

I’m facing issues in debugging my c++ code.

I made this for 45 pts during the contest. The python one gets ACed for 10 pts and for the rest shows TLE, while the C++ code shows WA’s for 2 subtasks and TLE for the last one

Please help me find the bug


Solution in python
Solution in c++