Stock Market
InterviewTime limit1sMemory limit2048 MB
Given a periodic price sequence whose total change is negative, find the lowest price the stock reaches that is at least X, or report -1 if none exists.
- Level
Medium6 of 10
- Topics
- Prefix sum, Math, Array
- Solved
- No attempts yet
Problem
Adrian owns a stock he bought earlier and wants to sell it. On day 0, the price is . Morgan, a robot, can predict the future. Morgan tells Adrian that the price changes repeat every days.
Formally, let be the price change from day to day for . For , . For , the price on day is . A price can be negative.
Morgan also knows the price is on a downward trend. The sum of all is negative.
Adrian can sell the stock only when the price is at least , the price he paid for it, so that he takes no loss. He wants to sell at the lowest price that is still at least . If , he can sell on day 0.
Input
The first line has three integers , , and (; ). The next line has integers () that repeat every days. The sum of all is guaranteed to be negative.
Output
If a price at least exists, print the lowest such price on one line. Otherwise, print -1 on one line.