

1 . Ninja will pick a slice of pizza.
2 . One of his friends will pick the next slice in the anti-clockwise direction.
3 . the Second friend will pick the next slice in the clockwise direction of Ninja’s pick.
If the ARR is [4,3,11,12,2,4] , so the optimal answer will be as follows:
Ninja will pick the slice having weight 12, so his friends will pick the slices having weights 11 and 2. After that Ninja will pick the slice with weight 4 and his friends will pick 4 and 3.
Ninja’s sum will be 16 which is the maximum.
The first line of the input contains an integer, 'T,’ denoting the number of test cases.
The first line of each test case contains a single integer, ‘N’, denoting the number of elements in array ‘ARR’.
The following line contains ‘N’ values corresponding to elements of ‘ARR’.
For each test case, print the maximum sum of the weight of slices.
Print the output of each test case in a separate 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 <= 1000.
1 <=ARR[i] <=10^6.
Time limit: 1 sec
In this problem, we can observe a pattern that Ninja can’t take any adjacent slices and the array is a circular array, so if Ninja takes the first slice he can’t take the last slice and vise-versa.
So,we will define a recursive function REC(‘ARR’,N,M) which will return the maximum sum if we choose M slices from first N slices of ‘ARR’.
The value of REC(‘ARR’,N,M) will be maximum of REC(‘ARR’,N-1,M) {Not taking the Nth slice} and ARR[N-1] + REC(‘ARR’,N-2,M-1) {Taking the Nth slice.}
But as we can’t first and last element together,we will create two arrays ARR1(from index 0 to N-2) and ‘ARR2’(from index 1 to index N-1.)
We will run the REC() function for both the array and return the maximum.
In this approach, we will use the same recursive functions, we used in approach 1 but we will use memoization to reduce the complexity as the answer for each state will be calculated only once.
We will define a 2D array ‘DP’ to store the answers.
In this approach, we will use dynamic programming and find the answer to the problem. We will prepare 2D array ‘DP’.
DP[i][j] will return the maximum sum if we pick only j slices from the first i slices of the list.
DP[i][j] can be computed as maximum of ( DP[i-1][j] and DP[i-2][j-1] + ARR[i-1]).
So,we iterate over all pairs of i and j and compute the values of DP table for ‘ARR1’ and ‘ARR2’ both and the compare their DP[N-1][M] and return the maximum among them.