Why Work?
Time limit1.122sMemory limit1122 MB
Given N coins whose prices follow a binary-counter-like update rule, find the maximum profit from trading one unit per coin per day over T days.
- Level
Medium7 of 10
- Topics
- Math, Greedy, Simulation
- Solved
- No attempts yet
문제
준성 is unwilling to work and only wants a quick score, so he starts analyzing the prices of cryptocurrencies! Here is what he found. is the price of the -th cryptocurrency.
- is either 1 or 2.
- changes every day. That is, if is 1, it becomes 2, and if it is 2, it becomes 1.
- changes every day if were all 2 yesterday. Otherwise, it does not change.
- Apart from the conditions above, never changes, and each cryptocurrency has an unlimited supply.
Because a huge investor could enter the market and make the analysis useless,준성 trades only one unit of each cryptocurrency per day, either buying or selling.
Junseong is diligent, so he starts trading as soon as prices are updated. Trading takes negligibly little time. None of his intended trades fail. If he wants to buy, he can always buy, and if he wants to sell, he can sell immediately.
Assume the analysis is correct at all times and his capital is always large enough to buy. Find the maximum profit he can earn by trading over days.
입력
The first line contains integers and , separated by a space.
The second line contains the values of on the day Junseong starts trading, given in order with no spaces.
출력
Print the answer to the problem on the first line.
힌트
Cryptocurrencies that Junseong still holds when trading ends are not counted in the profit.