// C# implementation to find the
// maximum product of the digit
// sum of the consecutive pairs of
// the subsequence of the length K
using System;
class GFG{
static int MAX = 100;
static int [, ,]dp = new int[1000, MAX, MAX];
// Function to find the product
// of two numbers digit sum
// in the pair
static int productDigitSum(int x, int y)
{
int sumx = 0;
// Loop to find the digits
// of the number
while (x > 0)
{
sumx += (x % 10);
x /= 10;
}
int sumy = 0;
// Loop to find the digits
// of other number
while (y > 0)
{
sumy += (y % 10);
y /= 10;
}
return (sumx * sumy);
}
// Function to find the subsequence
// of the length K
static int solve(int []arr, int i, int len,
int prev, int n, int k)
{
// Base Case
if (len == k)
return 0;
// Condition when we didn't reach
// the length K, but ran out of
// elements of the array
if (i == n)
return Int32.MinValue;
// Condition if already calculated
if (dp[i, len, prev] != 0)
return dp[i, len, prev];
int inc = 0, exc = 0;
// If length upto this point is odd
if ((len & 1) != 0)
{
// If length is odd, it means we need
// second element of this current pair,
// calculate the product of digit sum of
// current and previous element and recur
// by moving towards next index
inc = (productDigitSum(arr[prev], arr[i]) +
solve(arr, i + 1, len + 1, 0, n, k));
}
// If length upto this point is even
else
{
inc = solve(arr, i + 1, len + 1, i, n, k);
}
// Exclude this current element
// and recur for next elements.
exc = solve(arr, i + 1, len, prev, n, k);
// Return by memoizing it, by selecting
// the maximum among two choices.
return dp[i, len, prev] = Math.Max(inc, exc);
}
// Driver Code
public static void Main()
{
int []arr = { 10, 5, 9, 101, 24, 2, 20, 14 };
int n = arr.Length;
int k = 6;
Console.Write(solve(arr, 0, 0, 0, n, k));
}
}
// This code is contributed by Nidhi_biet