Optimal Strategy for a Game | Set 3 | Calculates the maximum score possible for P1 If only the bags from beg to ed were available ; Length of the game ; Which turn is being played ; Base case i . e last turn ; if it is P1 's turn ; if P2 's turn ; Player picks money from the left end ; Player picks money from the right end ; If it is player 1 's turn then  current picked score added to  his total. choose maximum of  the two scores as P1 tries to  maximize his score. ; If player 2 ' s ▁ turn ▁ don ' t add current picked bag score to total . choose minimum of the two scores as P2 tries to minimize P1 's score. ; Input array ; Function Calling
def maxScore ( money , beg , ed ) : NEW_LINE INDENT totalTurns = len ( money ) NEW_LINE turnsTillNow = beg + ( ( totalTurns - 1 ) - ed ) NEW_LINE if ( beg == ed ) : NEW_LINE INDENT if ( turnsTillNow % 2 == 0 ) : NEW_LINE INDENT return [ money [ beg ] , " L " ] NEW_LINE DEDENT else : NEW_LINE INDENT return [ 0 , " L " ] NEW_LINE DEDENT DEDENT scoreOne = maxScore ( money , beg + 1 , ed ) NEW_LINE scoreTwo = maxScore ( money , beg , ed - 1 ) NEW_LINE if ( turnsTillNow % 2 == 0 ) : NEW_LINE INDENT if ( money [ beg ] + scoreOne [ 0 ] > money [ ed ] + scoreTwo [ 0 ] ) : NEW_LINE INDENT return [ money [ beg ] + scoreOne [ 0 ] , " L " + scoreOne [ 1 ] ] NEW_LINE DEDENT else : NEW_LINE INDENT return [ money [ ed ] + scoreTwo [ 0 ] , " R " + scoreTwo [ 1 ] ] NEW_LINE DEDENT DEDENT else : NEW_LINE INDENT if ( scoreOne [ 0 ] < scoreTwo [ 0 ] ) : NEW_LINE INDENT return [ scoreOne [ 0 ] , " L " + scoreOne [ 1 ] ] NEW_LINE DEDENT else : NEW_LINE INDENT return [ scoreTwo [ 0 ] , " R " + scoreTwo [ 1 ] ] NEW_LINE DEDENT DEDENT DEDENT ar = [ 10 , 80 , 90 , 30 ] NEW_LINE arraySize = len ( ar ) NEW_LINE bags = ar NEW_LINE ans = maxScore ( bags , 0 , arraySize - 1 ) NEW_LINE print ( ans [ 0 ] , ans [ 1 ] ) NEW_LINE