Given N number of piles arranged in a row, and each pile has a positive integer number of coins and two players A and B play a game. The objective of the game is to end with the most coins. Player A and B take turns, with A starting first. On each player’s turn, that player can take all the coins in the first X remaining piles, where 1 <= X <= 2M. Initially M = 1. Then, we set M = max(M, X). The game continues until all the coins have been taken. Assuming both player play optimally, the task is to print maximum number of coins player A can get.

Examples :
Input : {2, 7, 9, 4, 4}
Output : 10
Explanation :
First case, if player A takes one pile at the beginning, player B
takes two piles, then player A takes 2 piles again.
Player A can get 2 + 4 + 4 = 10 coins in total.
Second case, if player A takes two piles at the beginning, then player
B can take all three piles left.
In this case, player A get 2 + 7 = 9 coins in total.
So the answer is 10 as it is larger.
Approach :
The idea is to use Dynamic Programming. We use 2-dimensional dynamic programming where DP[i][m] is the maximal number of coins a player can get when the 0, 1, …, i-1 piles have already been taken and the current M is m. Let’s assume the total number of coins in piles i, i+1, …, n-1 is Si. Before calculating DP[i][m] first we understand following intuition.
- If the player takes only the first pile (piles[i], x = 1), the other player can get up to DP[i+1][max(m, 1)]. So the current player can get Si – DP[i+1][max(m, 1)].
- If the player takes the first two piles (piles[i], piles[i+1], x = 2), the other player can get up to DP[i+2][max(m, 2)]. So the current player can get Si – DP[i+2][max(m, 2)].
- If the player takes the first 2m piles (piles[i], piles[i+1], …, piles[i+2m], x = 2m), the other player can get up to DP[i+2m][max(m, 2m)]. So the current player can get Si – DP[i+2m][max(m, 2m)].
We can calculate all these options and pick the maximum. Inorder to efficiently calculate Si we calculate suffix sum array.
Implementation :
// Java implementation of the above approach
import java.util.*;
public class Main
{
// Helper function to calculate the
// maximum coins taken by player A
public static int coinGame(int[] piles, int len)
{
// DP array to store the maximal number
// of coins a playe can get when
// the 0, 1, ..., i-1 piles have already been taken
// and the current M is m.
int dp[][] = new int[len][(len + 1) / 2 + 1];
// A loop to calculate suffix array
for (int i = len - 2; i >= 0; i--)
piles[i] += piles[i + 1];
// Base case is that if the number of piles
// are less than or equal to 2 then player
// will take all the coins
if (len <= 2)
return piles[0];
for (int i = len - 1; i >= 0; i--)
{
// Calculating maximum possible M for
// the current range i.e. i to len
int m = (len - i + 1) / 2;
// If maximum M is 1 i.e. the range is of length <= 2
// then current player can take all the coins, hence
// covering that case
dp[i][m] = piles[i];
// If M is greater than 1 we check all
// the options and take maximum
while (--m > 0)
{
dp[i][m] = 0;
// Running a loop over the available range of
// 1 <= X <= 2*M for each possible M
for (int x = 1; x <= 2 * m && i + x < len; x++)
{
// Calculating maximum M for the current range
int mx = Math.min((len - i - x + 1) / 2, Math.max(x, m));
// Taking maximum of all options
dp[i][m] = Math.max(dp[i][m], piles[i] - dp[i + x][mx]);
}
}
}
// Returning the result
return dp[0][1];
}
// Driver Code
public static void main(String args[])
{
// Input array
int A[] = {2, 7, 9, 4, 4};
// Size of array
int size = A.length;
// Calling helper function
// and printing result
System.out.print("Maximum coins player A can get is: " + coinGame(A, size));
}
}
Time Complexity : O(N x M x M)