I don't see anything in the problem statement that says you have to save all of the values.

So...

How about just looking at each entry as it comes in? If it's -1, then quit. If it is not -1,...