Expedition spoj

This is working fine for many test cases that I tried, but giving wrong answer upon submitting on spoj.
I have used the fact that we will go to the fuel station with maximum capacity, till the point we can go to using the current fuel.