atcoder#ABC250G. [ABC250G] Stonks

[ABC250G] Stonks

Score : 600600 points

Problem Statement

You are going to trade stocks of Company X for the next NN days.

As a precognitive, you know that the stock price on the ii-th day of trading will be PiP_i yen (the currency in Japan) per unit.

Every day, you can choose to do exactly one of the following.

  • Buy one unit of stock for PiP_i yen.- You will obtain one unit of stock and your money will decrease by PiP_i yen.
  • Sell one unit of stock for PiP_i yen.- You will lose one unit of stock and your money will increase by PiP_i yen.
  • Do nothing.

You initially have 1010010^{100} yen, so you will never be short of money.

Find the maximum possible amount of money you will have gained when the NN-th day has ended. Even if you still possess some amount of stocks of Company X when the NN-th day has ended, it is considered that they are worth 00 yen.

Constraints

  • All values in input are integers.
  • 1N2×1051 \le N \le 2 \times 10^5
  • 1Pi1091 \le P_i \le 10^9

Input

Input is given from Standard Input in the following format:

NN

P1P_1 P2P_2 \dots PNP_N

Output

Print the answer as an integer.

8
2 5 4 3 7 1 8 6
16

By acting as follows, your money will increase by 1616 yen, which is the maximum possible.

  • On the 11-th day, you buy 11 unit of stock. You now have 11 unit of stock, and your money has increased by 2-2 yen so far.
  • On the 22-nd day, you sell 11 unit of stock. You now have 00 units of stocks, and your money has increased by 33 yen so far.
  • On the 33-rd day, you buy 11 unit of stock. You now have 11 unit of stock, and your money has increased by 1-1 yen so far.
  • On the 44-th day, you buy 11 unit of stock. You now have 22 units of stocks, and your money has increased by 4-4 yen so far.
  • On the 55-th day, you sell 11 unit of stock. You now have 11 unit of stock, and your money has increased by 33 yen so far.
  • On the 66-th day, you buy 11 unit of stock. You now have 22 units of stocks, and your money has increased by 22 yen so far.
  • On the 77-th day, you sell 11 unit of stock. You now have 11 unit of stock, and your money has increased by 1010 yen so far.
  • On the 88-th day, you sell 11 unit of stock. You now have 00 units of stocks, and your money has increased by 1616 yen so far.
5
10000 1000 100 10 1
0
15
300 1 4000 1 50000 900000000 20 600000 50000 300 50000 80000000 900000000 7000000 900000000
2787595378