Suspicious Stocks

Interview

Time limit1sMemory limit128 MB

Summary
Given daily stock prices and starting cash, buy as many whole shares as possible on one day, then sell all on a later day, and report the maximum profit.
Level

Medium4 of 10

Topics
Array, Brute force, Greedy, Implementation
Solved
No attempts yet

Problem

Corruption is always extremely hard to prove. Even when we know that certain people accept bribes, it is almost impossible to punish them, because the only witnesses are those people and the ones who offered the bribe, and neither side is likely to testify.

One way to prove that something illegal happened is to count how much money a person holds and then show that it was impossible to earn that money legally. This is not easy, however: the person can simply claim that the money was a gift from an uncle or was earned by trading stocks.

We would like to check such explanations. Since we cannot verify the existence of "uncles," we will focus on the stocks. Given the price history of a stock, your task is to compute the maximum profit that could possibly have been earned by buying and then selling the stock.

Input

The input consists of several scenarios. Each scenario is given on two lines.

The first line contains two positive integers DD and MM (1≤D≤700001 \le D \le 70000, 1≤M≤400001 \le M \le 40000) separated by a space. DD is the number of trading days and MM is the amount of money available at the beginning.

The second line contains DD positive integers p1,p2,…,pDp_1, p_2, \ldots, p_D (1≤pi≤400001 \le p_i \le 40000) separated by spaces. pip_i is the price of one share on day ii, meaning that on that day you can buy or sell one share for that price.

The last scenario is followed by a line containing a single zero.

Output

For each scenario, print one line with a single number: the maximum profit achievable with at most one Buy operation followed by at most one Sell operation. Assume you own no shares at the beginning and that you may perform only one Buy during the whole period.

You may buy as many whole shares as your money allows, but only whole shares (an integer number), never fractions. For example, if a share costs $3 and you have $11, you can buy only 3 shares.

The Buy operation is followed by exactly one Sell operation on one of the later days. Naturally, to maximize the profit it is best to sell all the shares you hold.

Examples1

  1. Example 1

    Input
    3 1
    1 2 3
    3 1000
    1200 40 10
    3 10
    3 4 5
    5 10
    2 3 7 1 4
    0
    
    Expected output
    2
    0
    6
    30