Under construction

Still being built. Things may change

Stock Market

Interview

Time limit1sMemory limit2048 MB

Summary
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 P0P_0. Morgan, a robot, can predict the future. Morgan tells Adrian that the price changes repeat every NN days.

Formally, let DiD_i be the price change from day ii to day i+1i + 1 for 0≤i≤N−10 \le i \le N - 1. For i≥Ni \ge N, Di=Di mod ND_i = D_{i \bmod N}. For i>0i > 0, the price on day ii is Pi=Pi−1+Di−1P_i = P_{i-1} + D_{i-1}. A price can be negative.

Morgan also knows the price is on a downward trend. The sum of all DiD_i is negative.

Day01234567891011121314151617...
Price20241817211210148711204-2-31-8...

Adrian can sell the stock only when the price is at least XX, 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 XX. If P0≥XP_0 \ge X, he can sell on day 0.

Input

The first line has three integers NN, P0P_0, and XX (1≤N≤100 0001 \le N \le 100\,000; 1≤P0,X≤1091 \le P_0, X \le 10^9). The next line has NN integers DiD_i (−109≤Di≤109-10^9 \le D_i \le 10^9) that repeat every NN days. The sum of all DiD_i is guaranteed to be negative.

Output

If a price at least XX exists, print the lowest such price on one line. Otherwise, print -1 on one line.

Examples3

  1. Example 1

    Input
    6 20 5
    4 -6 -1 4 -9 -2
    
    Expected output
    7
    
  2. Example 2

    Input
    6 20 1
    4 -6 -1 4 -9 -2
    
    Expected output
    1
    
  3. Example 3

    Input
    1 3 4
    -1
    
    Expected output
    -1