How to Prevent TLE from happening even after unnesting loops

My code ended up with TLE even after i unnested loops. I had switched algorithms between versions but remained TLE.

Question :
Version 1:
Version 2:

So, how to prevent tle from happening?

Maximum execution time is 1 second.
Last edited on
can't see the problem, ask me to login.
also, you should try to explain to us (the problem and your solution), as it may be the case that you misunderstood the problem.

you need to calculate the order of your solution, according with the input limits and the worst case escenario.
in your first version you've got O(n*Et), in the second one O(Et), ¿how big is Et? ¿how big is n? ¿how does Et compare to n?
as a rule of thumb, the number for the worst case should fall around one million.
just by looking at the limits, your solution should be around O(n) or O(n lg n)

your idea to consume the oldest fruit first is correct, the problem is that you are processing them one at a time.
you are doing
for (int j = E; j>0; --j)
instead of simply fruits -= E;
Ok here's version 3:
Registered users can post here. Sign in or register to post.