#K8911. Best Time to Trade with Cooldown
Best Time to Trade with Cooldown
Best Time to Trade with Cooldown
You are given stock prices for n consecutive days, and you want to maximize your profit by buying and selling shares. After you sell a share, you have a mandatory cooldown period of one day before you can buy again.
Formally, let \(prices[i]\) be the price on day \(i\) (0-indexed). You may complete as many transactions as you like (i.e. buy one and sell one share of the stock multiple times) with the following restriction: after selling a share, you cannot buy on the next day (i.e. cooldown for 1 day).
Your task is to compute the maximum profit achievable. If no profit can be achieved, output 0.
Example: For prices [1, 2, 3, 0, 2]
, the maximum profit is 3.
inputFormat
The first line contains an integer \(n\), representing the number of days.
The second line contains \(n\) space-separated integers, where the \(i\)-th integer denotes the stock price on day \(i\).
If \(n = 0\), the second line will be absent.
outputFormat
Output a single integer that denotes the maximum profit achievable under the cooldown constraint.
## sample5
1 2 3 0 2
3
</p>