#K42772. Maximum Sum Contiguous Subsequence with Tie-breaking
Maximum Sum Contiguous Subsequence with Tie-breaking
Maximum Sum Contiguous Subsequence with Tie-breaking
You are given a sequence of N integers. Your task is to find a contiguous subsequence of the sequence with the maximum possible sum. In case there are multiple subsequences with the same maximum sum, choose the one with the smallest length. If there is still a tie, choose the subsequence that appears first.
Formally, given a sequence \(a_1, a_2, \ldots, a_N\), find indices \(i\) and \(j\) (with \(1 \le i \le j \le N\)) such that the sum \[ S = a_i + a_{i+1} + \cdots + a_j \] is maximized. Subject to this, if there are multiple such \((i, j)\) pairs, choose the pair for which \(j-i+1\) is minimum. If there is still a tie, choose the pair that appears earliest in the sequence.
inputFormat
The input is given from standard input (stdin). It consists of two lines:
- The first line contains an integer N (\(1 \le N \le 10^5\)) representing the number of elements in the sequence.
- The second line contains N space-separated integers representing the sequence.
outputFormat
Output to standard output (stdout) two integers separated by a single space. These integers represent the 1-indexed starting and ending indices of the contiguous subsequence with the maximum sum, following the specified tie-breaking rules.
## sample8
-2 -3 4 -1 -2 1 5 -3
3 7
</p>