There is a pizza with 3n slices of varying size, you and your friends will take slices of pizza as follows:


	You will pick any pizza slice.
	Your friend Alice will pick the next slice in the anti-clockwise direction of your pick.
	Your friend Bob will pick the next slice in the clockwise direction of your pick.
	Repeat until there are no more slices of pizzas.


Given an integer array slices that represent the sizes of the pizza slices in a clockwise direction, return the maximum possible sum of slice sizes that you can pick.

&nbsp;
Example 1:

Input: slices = [1,2,3,4,5,6]
Output: 10
Explanation: Pick pizza slice of size 4, Alice and Bob will pick slices with size 3 and 5 respectively. Then Pick slices with size 6, finally Alice and Bob will pick slice of size 2 and 1 respectively. Total = 4 + 6.


Example 2:

Input: slices = [8,9,8,6,1,1]
Output: 16
Explanation: Pick pizza slice of size 8 in each turn. If you pick slice with size 9 your partners will pick slices of size 8.


&nbsp;
Constraints:


	3 * n == slices.length
	1 &lt;= slices.length &lt;= 500
	1 &lt;= slices[i] &lt;= 1000


 class Solution:
    def maxSizeSlices(self, slices: List[int]) -> int:
       ** #This solve function mainly on work on the idea of A Previous dp problem House Robber II 
		#If we take the first slice then we cant take the second slice and vice versa**
		def solve(slices,start,end,n,dp):
            if start>end or n==0:
                return 0
            if dp[start][n] !=-1:
                return dp[start][n]
            include = slices[start] + solve(slices,start+2,end,n-1,dp)
            
            exclude = 0 + solve(slices,start+1,end,n,dp)
            
            dp[start][n]= max(include,exclude)
            return dp[start][n]
        dp1=[[-1 for i in range(k+1)]for _ in range(k+1)]
        dp2=[[-1 for i in range(k+1)]for _ in range(k+1)]
        
        option1=solve(slices,0,k-2,k//3,dp1)#Taking the the first slice , now we cant take the last slice and next slice
        option2=solve(slices,1,k-1,k//3,dp2)#Taking the the second slice , now we cant take the second last slice and next slice
        
        return max(option1,option2)
class Solution {
    public int maxSizeSlices(int[] slices) {
        int n = slices.length;
        return Math.max(helper(slices, n/3, 0, n - 2), helper(slices, n/3, 1, n - 1));
    }
    
    private int helper(int[] slices, int rounds, int start, int end) {
        int n = end - start + 1, max = 0;
        int[][][] dp = new int[n][rounds+1][2];
        dp[0][1][1] = slices[start];
        for (int i = start + 1; i <= end; i++) {
            int x = i - start;
            for (int j = 1; j <= rounds; j++) {
                dp[x][j][0] = Math.max(dp[x-1][j][0], dp[x-1][j][1]);
                dp[x][j][1] = dp[x-1][j-1][0] + slices[i];
                if (j == rounds) {
                    max = Math.max(max, Math.max(dp[x][j][0], dp[x][j][1]));
                }
            }
        }
        return max;
    }
}
class Solution {
public:
    
    int dp[501][501];
    
    int solve(vector<int>&v , int i ,int count ){
        
        if( i >= v.size() || count > v.size()/3 ) return 0 ;
        
        if(dp[i][count] !=  -1 ) return dp[i][count]; 
        
        int pick = v[i] + solve(v,i+2,count+1) ;
        int notPick = solve(v,i+1,count) ;
        
        return  dp[i][count] = max(pick,notPick) ;
        
    }
    
    
    int maxSizeSlices(vector<int>& slices) { 
        
     vector<int>v1 ;
     vector<int>v2 ;
        
     for(int i = 0 ; i < slices.size() ;i++){
         
         if( i != slices.size()-1 ) v1.push_back(slices[i]) ;
         if( i != 0 ) v2.push_back(slices[i]);
         
     }
     memset(dp,-1,sizeof(dp)) ;   
     int ans1 = solve(v1,0,0);
     memset(dp,-1,sizeof(dp)) ; 
     int ans2 = solve(v2,0,0);
        
     return max(ans1,ans2) ;
        
    }
};
var maxSizeSlices = function(slices) {
    const numSlices = slices.length / 3;
    const len = slices.length - 1;
    
    const dp = new Array(len).fill(null).map(() => new Array(numSlices + 1).fill(0));
    const getMaxTotalSlices = (pieces) => {
	    // the max for 1 piece using only the first slice is itself
        dp[0][1] = pieces[0];
		// the max for 1 piece using the first 2 slices is the max of the first and second slice
        dp[1][1] = Math.max(pieces[0], pieces[1]);
		// start the max as the max of taking 1 slice from the first 2 slices
        let max = dp[1][1];
		
		// calculate the max value for taking x number of pieces using up to that piece
        for (let i = 2; i < pieces.length; i++) {
            for (let numPieces = 1; numPieces <= numSlices; numPieces++) {
                dp[i][numPieces] = Math.max(dp[i - 1][numPieces],                    // the max for not taking this piece
				                            dp[i - 2][numPieces - 1] + pieces[i]);   // the max for taking this piece
                if (max < dp[i][numPieces]) max = dp[i][numPieces];                  // update the max if it is greater
            }
        }
        return max;
    }
    
    return Math.max(getMaxTotalSlices(slices.slice(0, slices.length - 1)),    // get max without the last slice
                    getMaxTotalSlices(slices.slice(1)));                      // get max without the first slice
};
Pizza With 3n Slices