#C10247. Maximum Profit from Price Fluctuations
Maximum Profit from Price Fluctuations
Maximum Profit from Price Fluctuations
You are given a sequence of numbers representing the prices of a commodity on consecutive days. You can perform as many transactions as you like (i.e., buy one day and sell on a later day). However, you are not allowed to hold more than one unit at a time.
Your task is to compute the maximum achievable profit. Formally, given an array \(prices\) of length \(n\), you need to accumulate the sum of all positive differences between consecutive days, i.e., compute the value:
\(\sum_{i=1}^{n-1} \max(0, \text{prices}[i] - \text{prices}[i-1])\)
If there is no profitable transaction, output 0.
inputFormat
The input is read from standard input (stdin) and consists of two lines:
- The first line contains an integer \(n\) indicating the number of days.
- If \(n > 0\), the second line contains \(n\) space-separated integers representing the prices on each day.
outputFormat
Output the maximum profit as a single integer on standard output (stdout).
## sample6
7 1 5 3 6 4
7