#C1661. Maximum Subarray Sum with Positive Constraint
Maximum Subarray Sum with Positive Constraint
Maximum Subarray Sum with Positive Constraint
You are given an array of integers and you need to find the maximum sum of any contiguous subarray that contains at least one positive number. If the array does not contain any positive numbers, then output the largest element in the array.
Note: A contiguous subarray is a sequence of consecutive elements from the original array.
Mathematical Formulation:
Given an array \(A = [a_1, a_2,\dots,a_n]\), find a subarray \(A[i\dots j]\) such that \(\sum_{k=i}^{j} a_k\) is maximized under the condition that \(\exists\, k,\, a_k > 0\). If no such \(a_k>0\) exists, return \(\max_{1 \le k \le n} a_k\).
inputFormat
The input begins with an integer \(T\) denoting the number of test cases. Each test case consists of two lines:
- The first line contains an integer \(n\) representing the number of elements in the array.
- The second line contains \(n\) space-separated integers.
\(1 \leq T \leq 10\) and \(1 \leq n \leq 10^5\) (per test case).
outputFormat
For each test case, output a single line which contains the maximum sum of a contiguous subarray that includes at least one positive number. If no positive number exists in the test case, output the largest number from the array.
## sample3
8
-2 -3 4 -1 -2 1 5 -3
5
-3 -2 -1 -4 -5
7
0 1 2 3 -3 4 5
7
-1
12
</p>