Invest Master
InterviewTime limit2sMemory limit512 MB
Given predicted prices for n stocks over d days, buy and sell units to maximize cash on the last day, with no fees and no intraday price change.
- Level
Medium5 of 10
- Topics
- Dynamic programming, Greedy, Implementation, Math
- Solved
- No attempts yet
Problem
After years of research, Ikuta has gained the power to see the future! The time and money he spent on this research were enormous, but the day he is rewarded has finally come. To start by recovering his money, Ikuta decides to begin investing in stocks.
Ikuta currently owns no stocks at all and has yen. The stocks he has chosen as investment targets are of kinds, and for them he has succeeded in predicting the stock prices for days starting today. As a result, it turned out that, surprisingly, there is no intraday price fluctuation at all during the days starting today. That is, when today is day 1, he knows the price yen of stock () on day (). Ikuta can freely buy and sell stocks on each day. In other words, at any point he can perform the following operations (buy and sell) in any order and any number of times. However, before and after each operation, his cash and the number of stock units he holds must be nonnegative integers.
-
Buy: on day , choose one stock kind (), pay yen of cash, and obtain 1 unit of stock .
-
Sell: on day , choose one stock kind (), give up 1 unit of stock , and obtain yen.
(While he was absorbed in research, the securities trading system made great progress, and transaction fees are no longer charged.)
Ikuta studied information science at university, but after devoting himself to future prediction research, he forgot everything he learned at university. Please write a program that maximizes his cash on the final day on his behalf.
Input
The input is given in the following format.
...
...
...
-
: the number of stock kinds
-
: the number of days
-
: cash on day 1
-
: the price of stock on day (today is day 1)
Output
Print the cash on the final day when investing optimally, on one line.
Constraints
Each variable in the input is an integer satisfying the following constraints.
-
-
-
-
The cash on the final day is guaranteed to be at most .