// C# program for the above approach
using System;
class GFG{
// Initialize dp array
static int [,,]dp = new int[101, 101, 101];
// Function that removes elements
// from array to maximize the cost
static int helper(int []arr, int left, int right,
int count, int m)
{
// Base case
if (left > right)
return 0;
// Check if an answer is stored
if (dp[left, right, count] != -1)
{
return dp[left, right, count];
}
// Deleting count + 1 i.e. including
// the first element and starting a
// new sequence
int ans = (count + 1) * m +
helper(arr, left + 1,
right, 0, m);
for(int i = left + 1; i <= right; ++i)
{
if (arr[i] == arr[left])
{
// Removing [left + 1, i - 1]
// elements to continue with
// previous sequence
ans = Math.Max(ans,
helper(arr, left + 1,
i - 1, 0, m) +
helper(arr, i, right,
count + 1, m));
}
}
// Store the result
dp[left, right, count] = ans;
// Return answer
return ans;
}
// Function to remove the elements
static int maxPoints(int []arr, int n, int m)
{
int len = n;
for(int i = 0; i < 101; i++)
{
for(int j = 0; j < 101; j++)
{
for(int k = 0; k < 101; k++)
dp[i, j, k] = -1;
}
}
// Function call
return helper(arr, 0, len - 1, 0, m);
}
// Driver Code
public static void Main(String[] args)
{
// Given array
int []arr = { 1, 3, 2, 2, 2, 3, 4, 3, 1 };
int M = 3;
int N = arr.Length;
// Function call
Console.Write(maxPoints(arr, N, M));
}
}
// This code is contributed by Amit Katiyar