#K44872. Maximum Profit from Stock Trading

    ID: 27628 Type: Default 1000ms 256MiB

Maximum Profit from Stock Trading

Maximum Profit from Stock Trading

You are given a sequence of stock prices over several days. Your task is to compute the maximum profit achievable by performing an arbitrary number of transactions (i.e., buying and selling the stock multiple times). Note that you must sell your stock before buying again.

For example, given a series of prices as [7, 1, 5, 3, 6, 4], one optimal strategy is to buy at price 1 and sell at price 5, then buy at price 3 and sell at price 6, resulting in a total profit of 7.

The profit is computed by summing up the positive differences between consecutive days, i.e., if the price on day \(i\) is greater than on day \(i-1\), then \(price[i] - price[i-1]\) contributes to the total profit. Formally, the maximum profit \(P\) can be calculated as:

\(P = \sum_{i=2}^{n} \max(0, price[i] - price[i-1])\)

inputFormat

The input is provided via standard input (stdin). The first line contains an integer \(n\) (where \(0 \le n \le 10^5\)) which represents the number of days. The second line contains \(n\) space-separated integers representing the stock prices for each day. If \(n = 0\), the second line will be empty.

outputFormat

Output a single integer to standard output (stdout) representing the maximum profit achievable through optimal transactions.

## sample
6
7 1 5 3 6 4
7