### 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 :https://dunjudge.me/contest/468/problems/847/problem-fruits.pdf
Version 1:https://pastebin.com/1esPH9aE
Version 2:https://pastebin.com/p29iPiww

So, how to prevent tle from happening?

Maximum execution time is 1 second.
Last edited on
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
 ``12`` ``````for (int j = E; j>0; --j) --fruits;``````
instead of simply `fruits -= E;`
Ok here's version 3:
https://pastebin.com/xEFesV00