Cow Tours problem from USACO

Cow Tours

I’m trying to solve Cow Tours problem on USACO.
Here is my code source

My code gets fail on 7th test. Can’t figure out what’s wrong?

Hi, i am planning to explore south America if anyone know about where i can get peru bus tickets then inform me.