We need to understand the problem. We have array a indexed 0..n-1. We can reverse at most one subarray (continuous subsegment) (or maybe zero? "at most one" means we can also do nothing). After reversal, we compute sum of elements on even positions (0,2,4,...). We want to maximize that sum.
We need to find the best subarray reversal to achieve maximum sum of even-indexed elements.
Observation: Reversing a subarray [l, r] will affect the positions of elements within that subarray. The elements outside remain at same indices. Within the reversed segment, the element originally at position i moves to position l+r-i. So an element originally at index i (within [l,r]) ends up at index j = l+r-i. Its parity (even/odd) may change if (i and j have different parity). Since we sum only even positions, the contribution of an element originally at i will be counted after reversal if its new position is even; otherwise it won't be.
Thus reversal effectively can swap contributions: some elements that originally contributed (if they were on even positions) may no longer contribute, and some that originally didn't may now contribute. The net change in sum on even positions equals sum over i in [l,r] of (a_i * (1_{new pos even} - 1_{old pos even})). Let's denote delta_i = a_i * (parity(new) - parity(old)). But careful: because we sum only even positions, we can think of base sum even_sum0 = sum_{i even} a_i. After reversal, new even_sum = even_sum0 + sum_{i in [l,r]} a_i * (I[(l+r-i) even] - I[i even]).
Goal: maximize new even_sum.
We can consider that the operation is at most one reversal, so we need to pick l,r to maximize sum_{i=l..r} a_i * (c_i) where c_i = I[(l+r-i) even] - I[i even]. But note that c_i depends on l+r (the sum) and i parity. Let's denote s = l+r (the sum of boundaries). Then for i in [l,r], new parity = (s - i) mod 2 = (s mod 2) xor (i mod 2) if we think of parity addition: parity(s-i) = parity(s) XOR parity(i) because subtracting mod 2 is same as adding. Actually (s - i) mod 2 = (s mod 2) - (i mod 2) mod 2 = (s mod 2) + (i mod 2) mod 2 (since -i ≡ i mod 2). Wait careful: mod 2, subtraction is same as addition because -i ≡ i (mod 2) because i mod 2 is either 0 or 1, and -0=0, -1 ≡ 1 mod 2? Actually -1 mod2 =1 because 1 ≡ -1 mod2 (since 2 divides 2). So yes, (s-i) mod 2 = (s + i) mod 2. Because -i ≡ i (mod 2). Let's check: if i=1, -1≡1 (mod2); if i=0, -0≡0. So parity(s-i) = (s + i) % 2. Equivalent to parity(s) XOR parity(i) since addition mod2 is XOR. So new even means (s + i) % 2 == 0.
Thus I[(s-i) even] = I[(s+i) even] = 1 if parity(s) == parity(i) (since s+i even => s and i same parity). So new contributes if parity(i) = parity(s). Old contributes if i even. So delta_i = a_i * ( (parity(i) == parity(s) ? 1 : 0 ) - (i even ? 1 : 0) ).
Let p = s mod 2 (0 if s even, 1 if s odd). Then new contributes if i%2 == p. So delta_i = a_i * ( (i%2 == p) - (i%2 == 0) ).
But note: i%2 can be 0 (even) or 1 (odd). So we can define:
If i even (i%2=0):
old contributes 1. new contributes if p==0, else 0.
So delta = ( (p==0 ? 1:0) - 1 ) = if p==0 -> 0; if p==1 -> -1. So for even i: delta = 0 if p=0 (even sum s), -a_i if p=1 (odd s).
If i odd (i%2=1):
old contributes 0. new contributes if p==1 (since i%2=1, p must be 1 for new to be even? Wait new even if i%2 == p? Actually condition: new contributes if i%2 == p. If i odd (1) and p=1 then true; if p=0 false). So new contributes = 1 if p=1 else 0. So delta = ( (p==1 ? 1 : 0) - 0 ) = if p=1 -> +a_i; if p=0 -> 0.
So for odd i: delta = +a_i if p=1, 0 if p=0.
Thus delta_i depends only on parity of i and parity of s. Great!
So total change = sum_{i in [l,r]} delta_i =
If s even (p=0): delta = sum over even i in [l,r] of 0 + sum over odd i of 0 = 0? Actually for p=0: even i: delta=0, odd i: delta=0 -> total change 0. That suggests that reversing a subarray with even sum of endpoints (i.e., l+r even) results in no change to sum of even positions? Let's verify with small example.
Take array: [a0, a1, a2]. Even positions: 0,2. Base sum = a0+a2. Suppose we reverse subarray [0,1] (l=0,r=1). Then new array: [a1,a0,a2]. Even positions: 0->a1,2->a2. Sum = a1+a2. Change = (a1+a2) - (a0+a2) = a1 - a0. According to formula: s = l+r = 1, odd -> p=1. So change should be: even indices in [0,1]? indices: 0(even): delta = -a0 because p=1 gives -a0. index 1(odd): delta = +a1. Total a1-a0. Good. Now if we reverse [0,2] (l=0,r=2). s=2 even. Then reversal: [a2,a1,a0]. Even positions: 0->a2,2->a0 sum = a2+a0 same as original? Actually original even sum a0+a2, new even sum a2+a0 same. So change 0. Our formula: p=0, delta: even i in [0,2] (0,2) -> delta 0; odd i (1) -> delta 0 => total 0. So indeed reversal with even s doesn't change even sum. But is that always true? Let's test [1,2] (n=2) l=0,r=1 (s odd) changes. l=0,r=1 s=1 odd: original even sum a0, new even sum a1. Change a1-a0. Good. l=0,r=0 (single element reversal does nothing because reversing single element array yields same. s=2*0=0 even, change 0. So indeed for reversal to affect even sum sum, we need s odd (i.e., l+r odd). So only subarrays where l and r have opposite parity (one even, one odd) produce a nonzero effect. Because then the parity of positions of elements get swapped between even and odd in the reversed segment? Actually let's reason: reversal of segment length L. The parity pattern: For each element originally at position i, after reversal it goes to position l+r-i. The parity of i and l+r-i differ if (l+r) is odd, because parity(l+r-i) = (l+r) parity XOR i parity, so if l+r odd, they are opposite; if even, same. So when l+r even, elements maintain parity, so sum on even positions unchanged. When l+r odd, parity of each element flips, so even positions get contributions from original odd positions within segment and lose contributions from original even positions within segment. And the delta is exactly: for even i: -a_i, for odd i: +a_i. So total change = (sum of a_i for odd i in [l,r]) - (sum of a_i for even i in [l,r]).
Thus the problem reduces to: We can select any subarray [l, r] such that l and r have opposite parity (i.e., (l+r) % 2 = 1), and reverse it. The resulting even sum = base_even_sum + ( sum_odd(l..r) - sum_even(l..r) ). If we do nothing, change = 0. So we want to maximize base_even_sum + max(0, max_{l,r with l+r odd} (sum_odd(l..r) - sum_even(l..r)) ). Actually we can also possibly make negative change? But we can choose to not reverse if negative. So effectively the maximum even sum = base_even_sum + max(0, best_gain), where best_gain is the maximum possible value of (sum_odd - sum_even) over all subarrays with l+r odd.
But wait, is that always valid? Are there any constraints? We are allowed to reverse at most one subarray. So indeed we can pick any segment to reverse, and the change is exactly that expression. So the problem is to compute the maximum possible value of gain = sum_{i in [l,r]} c_i * a_i, where c_i = +1 if i odd, -1 if i even, but only for segments where l and r have opposite parity. However, note that the condition l+r odd imposes a constraint on which subarrays we can pick. But we can also think of it as we need to consider segments that start and end with opposite parity. However, maybe the gain expression can be computed without explicitly enforcing the parity of endpoints? Let's derive more carefully.
Define an array b where b_i = a_i if i odd, and b_i = -a_i if i even. Then for any segment [l, r], the sum S(l,r) = sum_{i=l..r} b_i = sum_odd - sum_even. But is that always the gain? According to the formula, gain = sum_odd(l..r) - sum_even(l..r) = S(l,r). However, we found that this gain only applies when l+r is odd. What if l+r is even? Then reversal yields 0 gain. But if we consider S(l,r) for l+r even, does reversal yield S? No, it yields 0, but S(l,r) might be nonzero; that is not achievable. So we need to restrict to segments with l+r odd. So the achievable gains are S(l,r) for segments where l and r have opposite parity.
Thus we need to find maximum subarray sum (or maximum positive sum) over all contiguous subarrays where the parity of l and r are different. Since we can also do nothing, we take max(0, best). Also note that the segment length can be 0? No, at most one reversal, and subarray can be empty? "at most one subarray" probably means we may not reverse any; but we cannot reverse an empty subarray? Actually we can choose not to reverse, which gives gain 0. So we don't need to consider empty segment.
So the problem reduces to: given array b_i = (if i%2==1) a_i else -a_i, find maximum contiguous sum over subarrays [l, r] such that (l+r) % 2 == 1. Equivalent to: the length of subarray (r-l+1) is odd? Because if l and r have opposite parity, then the number of elements (r-l+1) is even? Let's check: if l even, r odd => l even, r odd => difference (r-l) odd => length = odd+1? For example, l=0(even), r=1(odd): length=2 (even). l=1(odd), r=2(even): length=2 (even). So l+r odd implies the segment contains an even number of elements? Actually parity of length: (r-l+1) mod2 = (r-l+1) mod2. Since r-l mod2 = (r mod2 - l mod2) mod2 = 1 (because they differ) so r-l is odd. Then odd+1 = even. So length is even. So we are only considering even-length subarrays. Also note: The gain S(l,r) for an even-length subarray (where l and r have opposite parity) equals sum b_i over that subarray. For odd-length subarrays (l and r same parity), the gain is 0 if we reverse them? But we could still reverse them? Yes, but gain is 0. So they are not beneficial; but we can consider them as not needed since gain 0 is not >0. So we only care about even-length subarrays where sum b_i is positive.
Thus the problem: max(0, maximum sum over contiguous even-length subarrays of b (where length even) of sum b_i). But careful: b_i defined as: for even i: -a_i, odd i: +a_i. Then sum over any contiguous segment of b gives sum_odd - sum_even for that segment. For an even-length segment, this corresponds to a valid reversal (since length even ensures endpoints opposite parity). For odd-length segment, endpoints have same parity, reversal would not change sum? Actually we derived that reversal with even s gives 0 gain, but what about odd-length segment with l+r even? For odd length, l and r have same parity, so l+r even, gain 0. So indeed only even-length segments are relevant.
But wait: Is there any possibility that we can reverse a subarray and get a gain that is not exactly sum b_i? For even-length segments, yes. But what about a segment with length 0? Not allowed. So indeed the maximum gain is the maximum subarray sum over all contiguous subarrays of b that have even length.
But we should double-check the parity reasoning: When l+r odd, we got gain = sum_odd - sum_even. But is there any additional effect from elements outside? No. And we confirmed with examples. So correct.
Thus the problem reduces to: compute base sum of a_i at even positions. Then compute max even-length subarray sum of b (with b_i = +a_i for odd i, -a_i for even i). The answer is base_even_sum + max(0, max_even_length_subarray_sum). Also note we can reverse any subarray, including the whole array? If whole array length even? If n is even, then l=0,r=n-1: l even? depends on n; n even => n-1 odd, so l+r = n-1 odd, valid; if n odd, l+r = n-1 even, gain 0 (so whole array reversal doesn't change even sum). So all good.
Now we need to efficiently compute max sum over contiguous subarrays of even length in b. Since n up to 2e5 per test case, total 2e5, we can do O(n) per test case.
We need to compute maximum sum of any subarray with even length. There is a known technique: we can consider pairs. Since even length means we can group into adjacent pairs? Actually we can think of dividing array into two interleaving sequences? Another approach: dynamic programming for maximum subarray with length parity constraint. Or we can consider transforming to prefix sums and enforce difference of prefix sums at indices with same parity.
Let p[i] = prefix sum of b from 0 to i-1 (like p[0]=0, p[k]= sum_{j=0}^{k-1} b_j). Then sum of subarray [l,r] = p[r+1] - p[l]. Length even means (r+1 - l) is even => (r+1) and l have same parity? Because length = (r+1) - l, even implies (r+1) and l have same parity. So condition: parity(l) == parity(r+1). So the subarray sum is p[j] - p[i] where i = l, j = r+1, and we require j > i, and parity(i) == parity(j). So we need max over i<j with parity(i)==parity(j) of (p[j] - p[i]). That's like we want the maximum difference between two prefix sums of the same parity. Since we want max(0, max_{i<j same parity} (p[j]-p[i])). So we can compute prefix sums p[0..n] (n+1 entries). Separate into two lists: even indices and odd indices. For each parity, we want the maximum difference where later minus earlier. That is essentially the maximum of (p[j] - min_prefix_before among same parity). So we can scan through indices in order, maintain for each parity the minimum prefix sum seen so far (for that parity), and compute current p[index] - min_prefix_sofar[parity]. Keep track of maximum.
Is that correct? Let's verify: indices are from 0 to n. But note: i corresponds to l (starting index of subarray), and j corresponds to r+1 (ending index+1). l can be from 0 to n-1, j from l+1 to n. Parity condition: parity(l) == parity(j). So indeed we can iterate j from 1 to n, for each j compute parity of j, then look at min_prefix among indices i with parity same as j and i < j. The maximum difference gives maximum sum for subarrays ending at j-1 (since r = j-1) with l having same parity as j. However, does this enforce length even? Yes because length = j - i, and if parity(i) == parity(j), then j-i is even. So all even-length subarrays correspond to such pairs. So we can compute the maximum p[j]-p[i] for i<j, parity(i)=parity(j). This gives maximum sum over even-length subarrays? Let's double-check: Is it possible that an even-length subarray sum is represented? Yes. Conversely, any i<j with same parity gives a subarray [i, j-1] of length j-i even. So indeed it's a bijection.
Thus the problem reduces to computing maximum difference of prefix sums with same parity. Then max_gain = that maximum (maybe negative). Since we can do nothing, we take max(0, max_gain).
Now we must also ensure that the reversal operation is exactly that. However, we derived that for even-length segments, gain = sum b_i = sum_odd - sum_even. But is there any hidden nuance about the reversal operation that might cause a different effect if we reverse an even-length subarray that doesn't satisfy l+r odd? Wait we derived that l+r odd is equivalent to length even. But let's double-check: l+r odd <=> l and r have opposite parity <=> length = r-l+1 is even (since even number of terms). So yes.
Thus the solution: For each test case:
- Compute base = sum_{i even} a_i.
- Compute array b of length n: b[i] = a[i] if i%2==1 else -a[i].
- Compute prefix sums p[0] = 0, p[i+1] = p[i] + b[i].
- Then find max_gain = maximum over i<j with parity(i)==parity(j) of p[j] - p[i]. We can compute by scanning j from 1..n, keep track of min_prefix_even and min_prefix_odd seen so far (from indices < j). At each j, parity = j%2 (since j is index of prefix sum). If j%2==0: we use min_even; if odd: use min_odd. Compute diff = p[j] - min_prefix[parity]. Update answer = max(answer, diff). Then update min_prefix[parity] = min(min_prefix[parity], p[j]).
- Since we can choose to do nothing, answer_gain = max(0, max_gain).
- Output base + answer_gain.
Check with sample.
Sample 1:
n=8
a: 1 7 3 4 7 6 2 9
Even positions: indices 0,2,4,6 => 1+3+7+2 = 13? Wait compute: 1+3=4, +7=11, +2=13. Base = 13.
b: index0: -1; 1:+7; 2:-3; 3:+4; 4:-7; 5:+6; 6:-2; 7:+9 => b = [-1,7,-3,4,-7,6,-2,9]
Prefix p: p0=0
i=0: b0=-1 => p1= -1
i=1: +7 => p2=6
i=2: -3 => p3=3
i=3: +4 => p4=7
i=4: -7 => p5=0
i=5: +6 => p6=6
i=6: -2 => p7=4
i=7: +9 => p8=13
Now compute max diff same parity indices:
Indices: 0..8. Parity of index: 0 even,1 odd,2 even,3 odd,4 even,5 odd,6 even,7 odd,8 even.
We need i<j same parity.
We scan j from 1 to 8.
Initialize min_even = p[0] = 0 (since index0 even). min_odd = INF? Actually we need to consider only indices < j. We'll set min_odd initially to something large, but we haven't seen any odd index yet. But we need to ensure we only consider i of same parity that have been seen. So we can initialize min_even = p[0] (index0). min_odd = some large number, and we won't use it until we have seen at least one odd index. Similarly update after each j.
Let's compute:
j=1 (odd). min_odd? So far odd indices seen? index1 hasn't been processed yet, only index0 (even). So no odd i<1. So we skip? Actually we need i<j same parity as j (odd). No i odd yet (since only index0 even). So we cannot compute diff. So we just update min_odd = p[1] after? Actually we need to update after evaluating j? Typically we compute diff using min_prefix for parity of j from indices < j. Since for j=1, min_odd not yet defined, we can't compute diff. So we don't compute. Then update min_odd = min(min_odd, p[1]) = p[1] = -1.
j=2 (even). min_even currently = p[0]=0. diff = p[2] - min_even = 6 - 0 = 6. Update answer=6. Then update min_even = min(0, p[2]=6) = 0.
j=3 (odd). min_odd currently = p[1]=-1. diff = p[3] - (-1) = 3 - (-1) = 4. answer = max(6,4)=6. Update min_odd = min(-1, p[3]=3) = -1.
j=4 (even). min_even = 0. diff = p[4] - 0 = 7 - 0 = 7. answer=7. update min_even = min(0,7)=0.
j=5 (odd). min_odd = -1. diff = p[5] - (-1) = 0 - (-1) = 1. answer stays 7. update min_odd = min(-1,0)= -1.
j=6 (even). min_even =0. diff = p[6] -0 =6-0=6. answer=7. update min_even = min(0,6)=0.
j=7 (odd). min_odd=-1. diff = p[7]-(-1)=4-(-1)=5. answer=7. update min_odd = min(-1,4)= -1.
j=8 (even). min_even=0. diff = p[8]-0 =13-0=13. answer=13. update min_even = min(0,13)=0.
Thus max_gain = 13. Then base + max_gain = 13+13=26. Output 26 matches sample.
Sample2:
n=5
a:1 2 1 2 1
Base: even indices 0,2,4 => 1+1+1=3? Actually index 0:1, index2:1, index4:1 sum=3. But sample output says 5. Let's compute: base = sum even positions = a0 + a2 + a4 = 1+1+1=3. So to reach 5, gain must be 2.
b: i0:-1; i1:+2; i2:-1; i3:+2; i4:-1 => b = [-1,2,-1,2,-1]
Prefix p: p0=0
p1=-1
p2=1
p3=0
p4=2
p5=1
Indices 0..5 parity: 0 even,1 odd,2 even,3 odd,4 even,5 odd.
Scan:
init min_even = p0=0, min_odd = INF.
j=1 odd: no odd i <1? Actually i=0 is even only, so skip. Update min_odd = min(INF, p1=-1) = -1.
j=2 even: diff = p2 - min_even = 1-0=1; answer=1; update min_even = min(0,1)=0.
j=3 odd: diff = p3 - min_odd = 0 - (-1) = 1; answer stays 1; update min_odd = min(-1,0) = -1.
j=4 even: diff = p4 - min_even = 2-0=2; answer=2; update min_even = min(0,2)=0.
j=5 odd: diff = p5 - min_odd = 1 - (-1) = 2; answer=2; update min_odd = min(-1,1) = -1.
max_gain=2, base+2=5. matches.
Sample3:
n=10
a:7 8 4 5 7 6 8 9 7 3
Base: even indices 0,2,4,6,8 => 7+4+7+8+7 = 33? Let's sum: 7+4=11, +7=18, +8=26, +7=33. So base=33. Output says 37 => gain=4.
b: i0:-7; i1:+8; i2:-4; i3:+5; i4:-7; i5:+6; i6:-8; i7:+9; i8:-7; i9:+3 => b = [-7,8,-4,5,-7,6,-8,9,-7,3]
Prefix p: compute:
p0=0
p1=-7
p2=1 (since -7+8=1)
p3=-3 (1 + (-4) = -3)
p4=2 (-3+5=2)
p5=-5 (2 + (-7) = -5)
p6=1 (-5+6=1)
p7=-7 (1+(-8)=-7)
p8=2 (-7+9=2)
p9=-5 (2+(-7)=-5)
p10=0 (-5+3= -2? Wait check: p9 = sum b0..b8 =? Let's compute carefully.
Better to compute stepwise:
b: index0=-7
1=8
2=-4
3=5
4=-7
5=6
6=-8
7=9
8=-7
9=3
p[0]=0
p[1]=0 + (-7) = -7
p[2]= -7 + 8 = 1
p[3]= 1 + (-4) = -3
p[4]= -3 + 5 = 2
p[5]= 2 + (-7) = -5
p[6]= -5 + 6 = 1
p[7]= 1 + (-8) = -7
p[8]= -7 + 9 = 2
p[9]= 2 + (-7) = -5
p[10]= -5 + 3 = -2
Hold on, p10 should be sum of all b =? Sum b = -7+8-4+5-7+6-8+9-7+3 = let's sum: (-7+8)=1, +(-4)=-3, +5=2, +(-7)=-5, +6=1, +(-8)=-7, +9=2, +(-7)=-5, +3=-2. Yes -2.
But previously I miscalculated p5 etc. Actually p5 is -5, p6=1, p7=-7, p8=2, p9=-5, p10=-2.
Now compute max diff same parity.
Indices 0..10 (0 even,1 odd,2 even,3 odd,4 even,5 odd,6 even,7 odd,8 even,9 odd,10 even)
Initialize min_even = p0=0, min_odd = INF.
j=1 (odd): no odd i<1, skip. Update min_odd = p1=-7.
j=2 (even): diff = p2 - min_even = 1-0=1, answer=1. min_even = min(0,1)=0.
j=3 (odd): diff = p3 - min_odd = -3 - (-7) = 4, answer=4. Update min_odd = min(-7, -3) = -7.
j=4 (even): diff = p4 - min_even = 2-0=2, answer remains 4. min_even = min(0,2)=0.
j=5 (odd): diff = p5 - min_odd = -5 - (-7) = 2, answer=4. min_odd = min(-7,-5) = -7.
j=6 (even): diff = p6 - min_even = 1-0=1, answer=4. min_even = min(0,1)=0.
j=7 (odd): diff = p7 - min_odd = -7 - (-7) = 0, answer=4. min_odd = min(-7,-7) = -7.
j=8 (even): diff = p8 - min_even = 2-0=2, answer=4. min_even = min(0,2)=0.
j=9 (odd): diff = p9 - min_odd = -5 - (-7) = 2, answer=4. min_odd = min(-7,-5) = -7.
j=10 (even): diff = p10 - min_even = -2-0 = -2, answer=4. min_even = min(0,-2)= -2? but careful: after processing j, we update min_even. But we might need to keep min_even as min of seen even indices, but now we have p10 negative, and we already had min_even=0. So update min_even = min(0, -2) = -2. But that's for future j? There is no future. So max_gain = 4. Base+4=33+4=37. Matches sample.
Sample4:
n=4
a:3 1 2 1
Base: even indices 0,2 => 3+2=5. Output 5 => gain=0. So max_gain should be ≤0.
b: i0:-3; i1:+1; i2:-2; i3:+1 => b = [-3,1,-2,1]
Prefix p: p0=0; p1=-3; p2=-2; p3=-4; p4=-3.
Indices 0 even,1 odd,2 even,3 odd,4 even.
Scan:
min_even=0, min_odd=INF.
j=1 odd: skip (no odd i). min_odd=-3.
j=2 even: diff = p2 - min_even = -2-0 = -2; answer? We keep track max; initially answer could be -inf? But we will later max with 0, so we can keep answer = -inf then take max. Or we can start answer = 0? But we need max_gain maybe negative, but we will take max(0, max_gain). So we can compute max_gain as the maximum among differences; but if all negative, max_gain will be negative, then we will take 0. So we should compute max_diff = -inf initially. Let's do that. For j=2: diff = -2, answer = max(-inf, -2) = -2.
Update min_even = min(0, -2) = -2? Actually min_even should be the minimum prefix among even indices seen so far. After j=2 (index2 even) we update min_even = min(prev min_even=0, p2=-2) = -2.
j=3 odd: diff = p3 - min_odd = -4 - (-3) = -1. answer = max(-2, -1) = -1.
Update min_odd = min(-3, -4) = -4.
j=4 even: diff = p4 - min_even = -3 - (-2) = -1. answer = max(-1, -1) = -1.
Update min_even = min(-2, -3) = -3.
max_gain = -1. So max(0, -1)=0. Base+0=5. Matches.
So this method works.
Thus the solution is straightforward O(n) per test case.
But wait, we must ensure that the reversal operation indeed exactly corresponds to choosing any even-length subarray, and the gain is sum b over that subarray. However, let's double-check edge cases: subarray of length 0? Not allowed. Subarray length 2 is smallest even length. Does reversing a subarray of length 2 always give gain = b_l + b_{l+1}? For l even, r odd, b_l = -a_l (since even), b_{l+1}=+a_{l+1} (since odd). So gain = -a_l + a_{l+1} = a_{l+1} - a_l. Indeed reversing [l, l+1] swaps positions: even position originally had a_l, after reversal gets a_{l+1}; odd position originally had a_{l+1} becomes odd? Actually after reversal, new even positions: index l (even) gets a_{l+1}, index l+1 (odd) gets a_l. So even sum change: at index l: gains a_{l+1} loses a_l => + (a_{l+1} - a_l). At index l+1 (odd), no effect because odd positions are not summed. So net gain = a_{l+1} - a_l, matches b_l + b_{l+1} = -a_l + a_{l+1}. Good.
For length 4, say l even, r = l+3 (odd). After reversal, elements: original: positions l (even) a_l, l+1(odd) a_{l+1}, l+2(even) a_{l+2}, l+3(odd) a_{l+3}. After reversal: index l (even) gets a_{l+3}, index l+1 (odd) gets a_{l+2}, index l+2 (even) gets a_{l+1}, index l+3 (odd) gets a_l. Even positions contributions: originally a_l + a_{l+2}. New: a_{l+3} + a_{l+1}. Gain = (a_{l+3} + a_{l+1}) - (a_l + a_{l+2}) = (a_{l+1} - a_l) + (a_{l+3} - a_{l+2}) = sum b over indices l..l+3: -a_l + a_{l+1} - a_{l+2} + a_{l+3}. Yes.
Thus correct.
Now we should also confirm that we are allowed to reverse a subarray of length 0? No, but our method includes the possibility of not doing anything via max(0, gain). If we can reverse a subarray of length 0 (do nothing), gain 0. So same.
But is there any other operation like reversing a subarray of odd length? Could it produce gain? According to our derivation, gain = 0. But we might double-check: Consider odd length segment, e.g., length 3: l=0 (even), r=2 (even). l+r even => no change? Let's compute manually: array [a0,a1,a2]. Even positions: 0,2. Reversing [0,2] gives [a2,a1,a0]. Even positions: a2 + a0 same as original. So gain 0. But what about odd length segment where l+r odd? That would imply l and r opposite parity, but length = r-l+1, if l even, r odd => length? Example l=0 even, r=1 odd -> length 2 even. So odd length implies same parity endpoints. So no odd length with l+r odd. So indeed only even length segments give non-zero gain.
Thus we've reduced to maximum subarray sum over even-length subarrays of b. But the prefix method gives maximum over all i<j with same parity. That includes subarrays of length 0? i=j would be length 0, but j>i ensures positive length. So fine.
But we must be careful: The indices i and j are prefix indices. When we compute p[j] - p[i] with i and j same parity, we get sum over subarray of length j-i (even). However, is there any restriction that the subarray must be contiguous? Yes, because prefix sums represent contiguous sums. So it's fine.
Now we should double-check if there is any possibility to get a better sum by not reversing exactly an even-length segment? For example, if we reverse a subarray, we might also affect elements outside? No, only inside.
Now, we should also consider the case where we reverse a subarray of length 0? That is not reversal, it's doing nothing. So our max(0, max_gain) covers that.
But wait: Could reversing a subarray cause a different effect if we consider that after reversal, indices of elements outside the reversed segment could shift? No, reversal does not shift indices; it just reorders within segment.
Thus solution stands.
Now we must implement for t test cases, sum n <= 200k.
Implementation steps:
For each test case:
- Read n, array a.
- Compute base = sum of a[i] for i%2==0.
- Compute prefix sums of b. We can compute p[0]=0; then for i from 0 to n-1: if i%2==1 then b = a[i] else b = -a[i]; p[i+1] = p[i] + b.
- Find max_gain = maximum over j from 1 to n of (p[j] - min_prefix_of_same_parity_before_j). But we must ensure we consider only i < j of same parity. So we can initialize min_even = p[0] (since index0 is even) and min_odd = +inf (or None). We'll also need to keep answer as -inf (or 0? but we can later take max with 0). Actually we want max_gain, which could be negative; we then take max(0, max_gain). But if we initialize answer as -inf, we might miss the case where all differences negative, max_gain negative, then we take 0. So we can set ans = -10**18, or we can directly compute max_gain variable and then final_gain = max(0, max_gain). But we must be careful that we consider at least one subarray? If we can do nothing, final gain can be 0. So it's fine if max_gain computed from subarrays is negative, we use 0. So we can compute max_gain as max over non-empty subarrays; if none exists? There is always at least one possible subarray if n>=1? For n=1, there is no even-length subarray (since min length 2 > n). So we cannot reverse any subarray that changes sum? Actually we could reverse subarray of length 1? But that yields no change because l+r even? For n=1, only possible subarray is [0,0] length 1, l+r=0 even, gain 0. So no positive gain; but we can also do nothing. So answer is base. In our prefix method, we consider subarrays of length >=2? Actually prefix method with i=1? Since i<j, min length 1? Wait if i and j have same parity, then j-i is even, and j-i >=1? If i and j same parity and j>i, the smallest difference is 2? Actually if i and j have same parity, the difference could be 1? No, parity same means j-i is even. The smallest positive even number is 2. So j-i >=2, which corresponds to subarray length >=2. So for n=1, there is no j from 1 to 1 such that j>i and i same parity? Let's see n=1: p[0]=0, p[1] computed. Indices: 0 even, 1 odd. For j=1 (odd), we need i odd <1, none. So we don't compute any diff. So ans remains -inf. So we need to handle that case: if no even-length subarray exists (i.e., n<2), then max_gain is 0 (since no reversal that changes sum). Actually could there be positive gain from a reversal of length 0? No. So we set max_gain = 0 if no subarray qualifies. But we can also just initialize ans = 0, and then take max(ans, computed diffs) because we can always choose not to reverse. However, if we set ans = 0 initially, then we might wrongly consider a situation where all computed diffs are negative, we still keep ans=0, which is correct because we can do nothing. But wait, would we ever want to consider a reversal that yields negative gain? No, because we can choose not to reverse. So we can safely start ans = 0, and only update ans when we find a diff > ans. However, we must ensure that we only consider diffs from valid subarrays. But if there are no valid subarrays, ans remains 0. That's correct. But what if there is a valid subarray that yields positive gain, we will update ans. And if there are valid subarrays but all negative, we don't update ans above 0, so we stick with 0. That matches taking max(0, max_gain). However, we must be careful that we are not missing the case where max_gain is positive but we never compute a diff? No, if there is any even-length subarray, it will be represented by some (i,j) with same parity, i<j. So we will compute diff for that j when scanning, provided we have stored min_prefix for parity i. But we need to ensure we consider all such subarrays. The scanning method as described: for each j, we use the min_prefix of same parity among indices < j to compute candidate diff. This gives the maximum p[j] - p[i] for that j with i of same parity and i<j. But if we start ans = 0, then we need to also consider if we might want to select a subarray with positive sum but our method might not capture it? Actually the standard method for maximum subarray sum uses scanning of min prefix; it gives the maximum over all i<j of p[j]-p[i]. If we restrict to same parity, we just maintain separate min_prefix per parity. Starting ans = 0 is fine; if the maximum diff is negative, we don't update, and we treat best as 0. That's exactly what we need. However, we must be careful: For the same parity condition, we need to ensure that we only use min_prefix that we have seen before j. That's fine.
But there is a nuance: The maximum diff for same parity might be achieved with i not necessarily the minimum prefix? Because we want max(p[j] - p[i]) = p[j] - min(p[i]) for i<j same parity, provided we take min p[i] to maximize diff for that j. But if we want the maximum over all j, we can compute for each j the candidate p[j] - min_prefix[parity] (where min_prefix is the minimum among earlier indices of that parity). This yields the maximum diff for that j. Then taking max over j gives the maximum overall. So it's correct.
Edge Cases: Need to consider that i can be 0 (even) and j even. That's allowed because subarray from 0 to j-1 with length j (even) is valid if j even. But note: for j even, i=0 gives sum b[0..j-1]. That's fine.
Now, about initialization: For parity even, we have p[0] (index 0) which is even. So we can set min_even = p[0] = 0. For parity odd, we have no odd index before j=1 (since index0 is even). So we should set min_odd = INF (or a large number) and not use it until we have at least one odd index encountered. In the loop, for j, we check if parity is odd, if min_odd is not INF then we can compute diff; else skip. After processing j (i.e., after computing diff for that j), we update min_odd with p[j] (if j odd). This ensures that for j odd we use min_odd from indices < j (which may include earlier odd indices). This is correct.
Alternatively, we can treat both parities symmetrically by initializing min_even = p[0], and min_odd = something like None, and update after computing diff? But careful: For j even, we always have min_even from at least index 0, so diff can be computed. For j odd, first odd index (j=1) has no previous odd index; we skip. Then after j=1, we update min_odd = p[1]. Then for j=3, we have min_odd from j=1 etc. Good.
But what about j=2? min_even currently includes p[0] and possibly p[2]? We compute diff before updating min_even, so we only use indices <2. That's correct.
Thus algorithm:
For each test case:
read n, a
base = sum(a[i] for i in range(n) if i%2==0)
p = [0]*(n+1)
for i in range(n):
if i%2==0:
b = -a[i]
else:
b = a[i]
p[i+1] = p[i] + b
min_even = p[0] # index 0 even
min_odd = float('inf')
max_gain = 0
for j in range(1, n+1):
if j % 2 == 0:
# even index
diff = p[j] - min_even
if diff > max_gain:
max_gain = diff
# update min_even
if p[j] < min_even:
min_even = p[j]
else:
# odd index
if min_odd != float('inf'):
diff = p[j] - min_odd
if diff > max_gain:
max_gain = diff
# update min_odd
if p[j] < min_odd:
min_odd = p[j]
answer = base + max_gain
print(answer)
Check with n=1: a[0] only. base = a[0]. p: p[0]=0, p[1] = -a[0] (since i=0 even => b=-a[0]). Loop j from 1 to 1. j=1 odd; min_odd = INF, so skip diff; update min_odd = p[1] = -a[0]. max_gain remains 0. answer = a[0] + 0 = a[0]. Correct: cannot reverse anything, so sum even positions = a[0].
n=2: a=[x,y]. base = x (since index0 even). b: [-x, y]. p: [0, -x, -x+y]. j=1 odd: min_odd INF skip, update min_odd = -x. j=2 even: diff = p[2] - min_even = (-x+y) - 0 = y - x. If y>x, max_gain positive; else remains 0. answer = x + max(0, y-x) = max(x,y). Indeed reversing subarray [0,1] yields even sum = y, so answer max(x,y). Good.
Edge Cases: n=0? Not possible (n>=1).
Complexities: O(n) per test case, total O(total n) = 2e5.
But wait, is there any subtlety? Our derivation assumed that reversing subarray [l,r] changes even sum by sum_{i=l}^{r} (if i odd? Actually we had delta_i = -a_i for even i, +a_i for odd i). But is it always exactly that? Let's double-check the parity conditions again carefully.
Original even sum: sum_{i even} a_i.
After reversal of [l,r], new even sum = sum_{i even, ir} a_i + sum_{i in [l,r], (l+r-i) even} a_i.
For i in [l,r], new index j = l+r-i. So contribution if j even.
Now j even <=> l+r-i even <=> (l+r) and i same parity. So condition: parity(i) == parity(l+r). So delta_i = a_i * ( I[ parity(i)==parity(l+r) ] - I[ parity(i)==0 ] ) because original contribution if i even. So if i even, original contributes, new contributes if parity(l+r)==0; else not. If i odd, original doesn't contribute, new contributes if parity(l+r)==1.
Thus delta_i =
- if i even: delta = ( (l+r even?1:0) - 1 ) * a_i.
- if i odd: delta = ( (l+r odd?1:0) - 0 ) * a_i.
Thus if l+r is even, delta_i = 0 for both (since for i even: (1-1)=0, for i odd: (0-0)=0). If l+r is odd, then for i even: (0-1) = -1 => -a_i; for i odd: (1-0)= +a_i. So indeed.
Thus only when l+r odd (i.e., l and r opposite parity) do we get change. So the requirement is that l and r have opposite parity. That's same as length even.
Thus the problem reduces as described.
Now we must also consider if we could do a reversal that is not contiguous? It says subarray (continuous subsegment). So we are good.
Now we need to confirm that the gain from reversal is exactly sum_{i=l}^{r} b_i. Since b_i = +a_i for odd i, -a_i for even i, sum b_i over segment = sum_odd - sum_even. That's the change. So maximizing gain is maximizing sum of b over even-length segments. However, we must also check if there is any possibility that we can achieve a larger sum by choosing a segment with l+r odd but the sum b_i is negative? That would decrease sum, so we wouldn't choose it. So we take max(0, max sum).
Now, one more nuance: Could we possibly achieve a gain by reversing multiple non-overlapping subarrays? The problem says "at most one subarray", so only one reversal allowed.
Thus solution is correct.
But wait, the problem statement says "You can reverse at most one subarray (continuous subsegment) of this array." So we can also choose not to reverse any.
Thus answer is base + max(0, max_even_length_subarray_sum(b)).
Now we have derived an O(n) solution using prefix sums. However, we should also think about alternative ways, but this is fine.
But we need to ensure that this maximum subarray sum for even length can be computed correctly with prefix sums. Edge cases: What if the maximum sum is from a segment that starts at an odd index and ends at an even index? That's allowed because l odd, r even gives l+r odd? Let's check: if l odd, r even, then l+r odd? odd+even = odd, yes. So such segment is also even length. So our condition i and j same parity covers it: For segment [l,r] length even, then l and r have opposite parity, so l parity != r parity. But we defined prefix indices i = l, j = r+1. Since l and r have opposite parity, what about parity of i and j? i = l, j = r+1. If l and r opposite parity, then r+1 has parity opposite of r? Because adding 1 flips parity. So if l and r opposite, then l and r+1 will have same parity? Let's test: l even, r odd => r+1 even, so i even, j even -> same parity. l odd, r even => r+1 odd, i odd, j odd -> same parity. So indeed, the condition l+r odd is equivalent to parity(l) == parity(r+1). So we use same parity of prefix indices. Good.
Thus the method stands.
Now verify sample 1 gave 13 gain, which is sum over whole array? Whole array length 8 even, sum b = -1+7-3+4-7+6-2+9 = 13. Yes. So the best was reverse the whole array, which yields gain 13 and final sum 26.
Now, is there any scenario where the maximum gain might be from a segment that doesn't correspond to an even-length subarray? Already covered.
Now, before coding, we must also consider potential overflow: a_i up to 1e9, n up to 2e5, total sum up to 2e14? base sum could be up to 1e9*1e5 = 1e14, and gains could be similar magnitude. So we need 64-bit integer (Python int handles big integers, but in C++ we would use long long). Python is fine.
Now, we need to compute the maximum difference with same parity. But is there any alternative where the maximum gain might be from a segment that starts at odd index and ends at even index? That is covered.
Now, we can also compute directly without prefix sums using DP: maximum subarray sum with even length. But prefix method is simpler.
Potential pitfalls: When we initialize min_odd = +inf, we must avoid using it before we have a value. Since j odd starts from 1, we skip diff for j=1. That's fine. However, what about j=3? By then min_odd would have been updated from j=1. So it's okay.
But we need to also consider if there could be multiple test cases, so we should reset variables accordingly.
Now, check with some additional examples:
Example: n=3, a=[1,100,1]. Base: indices 0,2 -> 1+1=2. b: [-1,100,-1]. Prefix p:0, -1, 99, 98. Even-length subarrays possible? length 2: [0,1] sum = -1+100=99; [1,2] sum = 100-1=99. Both even-length? length 2 is even. So maximum gain = 99. But can we reverse a subarray of length 2? Yes, either [0,1] or [1,2]. Let's test reversal [0,1]: original even sum = a0+a2 = 2. After reversal: array becomes [100,1,1], even indices: indices 0 and 2: 100+1=101, gain=99. So answer would be 101. But base + max_gain = 2+99=101. Good. Our algorithm: compute max diff same parity. p indices:0(even),1(odd),2(even),3(odd). We compute: min_even=p0=0; min_odd=inf.
j=1 odd: skip, update min_odd=-1.
j=2 even: diff = p2 - min_even = 99-0=99 -> max_gain=99, update min_even=0? p2=99>0 so min_even stays 0.
j=3 odd: diff = p3 - min_odd = 98 - (-1)=99 -> max_gain stays 99. So answer 101. Works.
Now consider n=4, a=[4,1,2,3]. Base: indices 0,2: 4+2=6. b:[-4,1,-2,3]. Even-length subarrays: [0,1]: -4+1=-3, [1,2]: 1-2=-1, [2,3]: -2+3=1, [0,3]: -4+1-2+3=-2. Max gain = 1. So answer = 7. Let's test reversal of [2,3] (l=2 even? Actually indices 2 even, 3 odd -> even length 2): original even sum = a0 + a2 = 4+2=6. After reversal of [2,3]: array becomes [4,1,3,2]. Even indices: 0:4, 2:3 => sum=7, gain=1. So answer 7. Good. Our prefix method: p:0, -4, -3, -5, -2. Indices:0e,1o,2e,3o,4e. Compute: min_even=0; min_odd=inf. j=1 odd: skip, min_odd=-4. j=2 even: diff = -3-0=-3, max_gain=0? if we set initial max_gain=0, remains 0. Update min_even = min(0, -3) = -3. j=3 odd: diff = -5 - (-4) = -1, max_gain stays 0. Update min_odd = min(-4, -5) = -5. j=4 even: diff = -2 - (-3) = 1, max_gain becomes 1. So answer=6+1=7. Works.
Now consider n=2, a=[5,1]. Base=5, b=[-5,1]. p:0,-5,-4. j=1 odd: skip; min_odd=-5; j=2 even: diff = -4 - 0 = -4, max_gain stays 0? Actually initial max_gain=0, diff=-4 not >0. So max_gain=0, answer=5. But can we reverse [0,1]? That would give even sum = a1 = 1, which is less than 5, so we wouldn't reverse. So answer 5 is correct.
But suppose a=[2,5], base=2, b=[-2,5], p:0,-2,3. j=1 odd: skip, min_odd=-2; j=2 even: diff = 3-0=3 >0, max_gain=3, answer=5. That matches reversal.
Thus algorithm works.
Now, we need to ensure that the max_gain we compute is indeed the maximum sum over even-length subarrays. Could there be a scenario where the max sum is achieved by a subarray that is not maximal in the sense of prefix diff? The prefix diff method gives the maximum sum over all subarrays (with no length restriction) but we restrict parity condition on i and j. That's correct.
Now, complexity: O(n) per test case, total O(2e5). Memory: O(n) for a, but we can compute on the fly; we need prefix sums maybe we can compute without storing all b? We can compute p incrementally and maintain min_even and min_odd. We can compute p as a running sum without storing the whole array. So we can read a, compute base and also update p. However, we need p values for each index to update min_even/min_odd. We can compute p in a loop while reading a: but we need to compute b_i which depends on a_i and index parity. So we can read the whole array, or we can read one by one and compute prefix sum on the fly. Since we need to compute base also, we can do both in one pass. Then we can compute max_gain in a second pass? Actually we can compute base and also build prefix sums p. But p is dependent on b, which depends on a_i and index parity. So we can do:
Initialize p=0.
min_even = 0 (since p0=0)
min_odd = INF
max_gain = 0
base = 0
For i from 0 to n-1:
read a_i
if i%2==0: base += a_i; b = -a_i
else: b = a_i (no addition to base)
p += b # p now becomes p[i+1]
j = i+1
if j%2==0:
diff = p - min_even
if diff > max_gain: max_gain = diff
if p < min_even: min_even = p
else:
if min_odd != INF:
diff = p - min_odd
if diff > max_gain: max_gain = diff
if p < min_odd: min_odd = p
But careful: We need to handle min_even update for j even after using it, and similarly for odd. But note that for j odd we need min_odd from previous odd indices. Since we haven't seen any odd before j=1, we need to set min_odd to INF initially. For j=1 (odd), we skip diff, then update min_odd with p (which is p1). That's fine.
But note that p before processing i=0 is p0=0. We've already set min_even = p0 =0. However, we also need to consider that we will update min_even for later j even, but we must not include p0? Actually p0 is index 0, which is even; we used it as min_even for j even. For j=2, diff = p2 - min_even where min_even is min(p0, p2?) But we update min_even after computing diff for j=2 using the previous min_even (which at that point is p0). That's correct. Then we update min_even with p2. So good.
Thus we can do it in one pass, reading a_i and updating prefix sum, without storing the entire array. That's efficient.
Edge Cases: n=1. Loop i=0: i even, base += a0; b = -a0; p = -a0; j=1 odd; min_odd INF => skip diff; then update min_odd = p = -a0. End loop. max_gain=0, answer = base = a0. Works.
n=2: i=0: base+=a0; b=-a0; p = -a0; j=1 odd: skip diff; update min_odd = -a0.
i=1: i odd: b=+a1; p = -a0 + a1; j=2 even: diff = p - min_even (0) = -a0 + a1; if > max_gain update; update min_even with p if lower. So works.
But careful: For j even, we used min_even = 0 (p0) initially. That's correct because we can start subarray at index 0. For j odd, we need min_odd from previous odd indices; at j=1 there is none, so skip. At j=3 (if exists) there would be.
Now, we need to consider that min_even is initially