#abc250g. G - Stonks

G - Stonks

Score : 600600 points

问题描述

你将在接下来的 NN 天内交易公司 X 的股票。

作为一名先知,你知道第 ii 天交易时的股价为每股 PiP_i 日元(日本货币单位)。

每天,你可以选择执行以下操作之一:

  • 花费 PiP_i 日元购买一股股票。
    • 你将获得一股股票,并且你的资金将减少 PiP_i 日元。
  • PiP_i 日元的价格卖出一股股票。
    • 你将失去一股股票,并且你的资金将增加 PiP_i 日元。
  • 什么都不做。

最初你拥有 1010010^{100} 日元,因此你永远不会缺钱。

计算当第 NN 天结束时,你能获得的最大金额。 即使在第 NN 天结束时你还持有公司 X 的一定数量的股票,这些股票也被认为价值为 00 日元。

以上为通义千问 qwen-max 翻译,仅供参考。

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.

Sample Input 1

8
2 5 4 3 7 1 8 6

Sample Output 1

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.

Sample Input 2

5
10000 1000 100 10 1

Sample Output 2

0

Sample Input 3

15
300 1 4000 1 50000 900000000 20 600000 50000 300 50000 80000000 900000000 7000000 900000000

Sample Output 3

2787595378

update @ 2024/3/10 10:41:56