Stock Market

시간 제한1초메모리 제한2048 MB

요약
주기적으로 반복되는 주가가 장기적으로 하락할 때, X 이상이면서 가장 낮은 가격을 찾는다. 없으면 -1을 출력한다.
난이도

보통10점 중 6점

유형
수학, 누적 합, 이분 탐색
정답자
아직 제출이 없습니다

문제

Adrian owns a stock that he previously purchased, and wants to sell that stock. Currently, at day 00, the price of the stock is P_0P\_0. As a robot, Morgan can predict the future. Morgan tells Adrian that the price changes will repeat every NN days.

Formally, suppose that the price change from day ii to day i+1i + 1 for 0≤i≤N−10 ≤ i ≤ N - 1 is D_iD\_i. The price change from day ii to i+1i + 1 for i≥Ni ≥ N is D_i=D_i mod ND\_i = D\_{i \bmod N}. The price of the stock at day ii for i>0i > 0 is P_i=P_i−1+D_i−1P\_i = P\_{i-1} + D\_{i-1}. It is possible for a price to be negative.

Moreover, Morgan also knows that the price is on a downward trend. That is, the sum of all D_iD\_i is negative.

The following table is the stock price of each day if N=6N = 6, P_0=20P\_0 = 20, and D_0..5=\[4,−6,−1,4,−9,−2]D\_{0..5} = \[4, -6, -1, 4, -9, -2].

Day0011223344556677889910101111121213131414151516161717…\dots
Price2020242418181717212112121010141488771111220044−2-2−3-311−8-8…\dots

Adrian can only sell the stock when the price is at least XX, the price when he purchased the stock, to avoid any losses. As a thrill seeker, Adrian also would like to sell his stock at the lowest price possible while still being at least XX.

Help Adrian to determine the lowest price of the stock that is not lower than XX, or tell him if it is impossible. Note that Adrian can sell his stock at day 00, if P_0≥XP\_0 ≥ X.

입력

Input begins with three integers NN P_0P\_0 XX (1≤N≤100,0001 ≤ N ≤ 100\\, 000; 1≤P_0,X≤1091 ≤ P\_0, X ≤ 10^9) representing the number of days in a cycle, the price at day 00, and the price when Adrian purchased the stock, respectively. The next line contains NN integers D_iD\_i (−109≤D_i≤109-10^9 ≤ D\_i ≤ 10^9) representing the price changes that repeat every NN days. It is guaranteed that the sum of all D_iD\_i is negative.

출력

If a price not lower than XX exists, output an integer in a single line representing the lowest price of the stock that is not lower than XX. Otherwise, output -1 in a single line.

예제3

  1. 예제 1

    입력
    6 20 5
    4 -6 -1 4 -9 -2
    
    예상 출력
    7
    
  2. 예제 2

    입력
    6 20 1
    4 -6 -1 4 -9 -2
    
    예상 출력
    1
    
  3. 예제 3

    입력
    1 3 4
    -1
    
    예상 출력
    -1