atcoder#ABC250G. [ABC250G] Stonks
[ABC250G] Stonks
Score : points
Problem Statement
You are going to trade stocks of Company X for the next days.
As a precognitive, you know that the stock price on the -th day of trading will be 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 yen.- You will obtain one unit of stock and your money will decrease by yen.
- Sell one unit of stock for yen.- You will lose one unit of stock and your money will increase by yen.
- Do nothing.
You initially have yen, so you will never be short of money.
Find the maximum possible amount of money you will have gained when the -th day has ended. Even if you still possess some amount of stocks of Company X when the -th day has ended, it is considered that they are worth yen.
Constraints
- All values in input are integers.
Input
Input is given from Standard Input in the following format:
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 yen, which is the maximum possible.
- On the -th day, you buy unit of stock. You now have unit of stock, and your money has increased by yen so far.
- On the -nd day, you sell unit of stock. You now have units of stocks, and your money has increased by yen so far.
- On the -rd day, you buy unit of stock. You now have unit of stock, and your money has increased by yen so far.
- On the -th day, you buy unit of stock. You now have units of stocks, and your money has increased by yen so far.
- On the -th day, you sell unit of stock. You now have unit of stock, and your money has increased by yen so far.
- On the -th day, you buy unit of stock. You now have units of stocks, and your money has increased by yen so far.
- On the -th day, you sell unit of stock. You now have unit of stock, and your money has increased by yen so far.
- On the -th day, you sell unit of stock. You now have units of stocks, and your money has increased by 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