Code works on custom input but give wrong answer in submission

My code gives wrong answer on submission but when give sample input through custom input. I get the same sample output as shown in the question. I can’t understand what’s wrong.Please help
My solution is

You are only checking if the speed of next car is less than previous or not. This is your mistake. If previous car was not running at its max speed then you can not compare. You have to compare with the speed that is max till now. Try n=3, speed[]=4 14 10