You are given an array of N non-negative integers: A1, A2, ..., AN. An alternating subsequence is a subsequence in which the indices of any two consecutive elements differ by exactly two in the original array. That is, if Ai1, Ai2, ..., Aik is some subsequence, then for it to be an alternating subsequence, (i2 - i1 = 2), (i3 - i2 = 2), and so on should all hold true. Among all alternating subsequences, find the one which has maximum sum of elements, and output that sum.
Input
The first line of the input contains an integer T denoting the number of test cases.
The first line of each test case contains an integer N denoting the number of elements in the array.
The second line contains N space-separated integers A1, A2, ..., AN denoting the array A.
Output
For each test case, output a single line containing the answer.
Note
A subsequence with only a single integer is also an alternating subsequence.
Constraints
1 ≤ T ≤ 10
1 ≤ N ≤ 105
0 ≤ Ai ≤ 105
t = int(input("number of test cases: "))
while t > 0:
n = int(input("number of elements: "))
arr = [int(i) for i in input("space-separated integers denoting the array:\n").split()]
if n == 1:
print(arr[0])
else:
m_sum, curr_sum = arr[0], arr[0]
for i in range(n-1):
if arr[i+1] - arr[i] == 2:
curr_sum += arr[i+1]
else:
if curr_sum > m_sum:
m_sum = curr_sum
curr_sum = arr[i+1]
if curr_sum > m_sum:
m_sum = curr_sum
print(m_sum)
t -= 1
Comments
Leave a comment