


A subsequence is a sequence that can be derived from another sequence by deleting some or no elements without changing the order of the remaining elements
The first line of the input contains ‘T’ denoting the number of test cases.
The first line of each test case contains an integer ‘N’, representing the length of the array.
The second line of each test case contains 'N' space-separated integers of the array 'A'.
For each test case, print a single line containing a single integer denoting the maximum sum subsequence.
The output of each test case will be printed on a new line.
You do not need to print anything, it has already been taken care of. Just implement the given function.
1 <= T <= 10
1 <= N <= 10 ^ 4
1 <= A[i] <=10 ^ 5
Where ‘T’ denotes the number of test cases and N denotes the length of array 'A'.
Time limit: 1 sec.
Explanation:
The key idea is to build our current answer from previous answers, i.e. if we want to calculate the answer from the first ‘i’ elements (0...i), we can use answers from the prefix of i-1, i-2, i-3 elements.
solve(A,i) = max( solve(A,i-1), A[i] + solve(A,i-2), A[i]+A[i-1] +solve(A,i-3) );
To calculate the maximum subsequence sum of the first i elements we have 3 parts:
To make sure we do not use 3 consecutive elements from the array, we deliberately skip some elements.
Algorithm:
The key idea is to convert the recursive approach to the dp approach by caching the solution, i.e. If we have already calculated the answer we just return the stored answer and if we haven’t calculated then we recursively solve it by the above approach.