#K78567. Maximum Stock Profit Calculation

    ID: 35115 Type: Default 1000ms 256MiB

Maximum Stock Profit Calculation

Maximum Stock Profit Calculation

Given an array representing the daily stock prices, your task is to determine the maximum profit that can be achieved by buying on one day and selling on a later day. You must buy before you sell. If no profit is possible (i.e., if the prices are in non-increasing order), return 0.

The problem can be mathematically formulated as solving for \(\max_{i<j}(price_j - price_i)\), where \(price_i\) and \(price_j\) are the stock prices on days \(i\) and \(j\) respectively.

inputFormat

The input consists of two lines. The first line contains an integer n representing the number of days. The second line contains n space-separated integers where each integer represents the stock price on that day.

outputFormat

Output a single integer representing the maximum profit that can be achieved by performing one buy and one sell transaction. If no profitable transaction exists, output 0.

## sample
6
7 1 5 3 6 4
5