#K80647. Maximum Profit Calculation
Maximum Profit Calculation
Maximum Profit Calculation
You are given a sequence of transactions conducted during a single day. Each transaction has a value that could be positive (profit) or negative (loss). Your task is to determine the maximum possible profit, which is defined as the maximum sum of any contiguous subarray of transactions. Note that if all transactions result in a loss, the maximum profit is 0.
This is equivalent to finding the maximum subarray sum with the condition that the profit cannot be negative. In mathematical terms, if the transactions are represented by an array \(a_1, a_2, \ldots, a_n\), then the answer is given by:
\(\max(0, \max_{1 \le i \le j \le n} \sum_{k=i}^{j} a_k)\)
For example:
- If the transactions are
-3 5 -2 4 -1
, the maximum profit is 7 (obtained from the subarray5 -2 4
). - If all transactions are negative, the maximum profit is
0
.
You should implement your solution such that it reads input from stdin and outputs the result to stdout.
inputFormat
The input is given via standard input with two lines:
- The first line contains a single integer \(n\) representing the number of transactions.
- The second line contains \(n\) space-separated integers representing the transaction values.
outputFormat
Output a single integer which is the maximum possible profit of the day.
## sample5
-3 5 -2 4 -1
7