#K47392. Maximize Stock Trading Profit

    ID: 28188 Type: Default 1000ms 256MiB

Maximize Stock Trading Profit

Maximize Stock Trading Profit

You are given a sequence of daily stock prices. Your task is to simulate a simplified stock trading system where you can buy and sell stocks in order to maximize your profit.

You can perform as many transactions as you like (i.e., you can buy one and sell one share of the stock multiple times), but you must sell the stock before you buy again.

The maximum profit is calculated as the sum of all positive differences between consecutive days. In mathematical terms, if the list of prices is \(prices[0], prices[1], \dots, prices[n-1]\), then the maximum profit is given by:

$$\text{Profit} = \sum_{i=1}^{n-1} \max(0, prices[i]-prices[i-1])$$

Make sure to read the input from STDIN and output the result to STDOUT.

inputFormat

The input is provided via STDIN and consists of two lines:

  1. The first line contains a single integer (n) representing the number of days.
  2. The second line contains (n) space-separated integers representing the daily stock prices.

outputFormat

Output a single integer to STDOUT, representing the maximum profit that can be achieved with the given stock prices.## sample

6
7 1 5 3 6 4
7