#C8681. Longest Maximum Sum Subarray
Longest Maximum Sum Subarray
Longest Maximum Sum Subarray
You are given an array of integers. Your task is to find the contiguous subarray with the maximum sum. If there are multiple subarrays with the same maximum sum, choose the one with the longest length. If there is still a tie, choose the subarray that appears first.
In mathematical terms, given an array \(a = [a_1, a_2, \dots, a_n]\), find indices \(i\) and \(j\) (with \(1 \leq i \leq j \leq n\)) such that the sum \[ S = \sum_{k=i}^{j} a_k \] is maximized. In case two or more pairs \((i, j)\) yield the same sum, select the one with the maximum \(j-i+1\) (i.e. longest length), and if still ambiguous, the one with the smallest \(i\) should be chosen.
If the array is empty, simply output an empty result.
inputFormat
The first line contains a single integer \(n\) representing the number of elements in the array. If \(n > 0\), the second line contains \(n\) space-separated integers representing the elements of the array.
If \(n = 0\), there will be no second line.
outputFormat
Output the contiguous subarray (its elements separated by a single space) which has the maximum sum as described. If the array is empty, output an empty line.
## sample5
1 2 3 -2 5
1 2 3 -2 5