// C# implementation to maximize
// product of same-indexed elements
// of same size subsequences
using System;
class GFG{
static readonly int INF = 10000000;
// Utility function to find the maximum
static int maximum(int A, int B,
int C, int D)
{
return Math.Max(Math.Max(A, B),
Math.Max(C, D));
}
// Utility function to find the
// maximum scalar product from
// the equal length sub-sequences
// taken from the two given array
static int maxProductUtil(int X, int Y,
int []A, int[] B,
int [,]dp)
{
// Return a very small number
// if index is invalid
if (X < 0 || Y < 0)
return -INF;
// If the sub-problem is already
// evaluated, then return it
if (dp[X, Y] != -1)
return dp[X, Y];
// Take the maximum of all
// the recursive cases
dp[X, Y] = maximum(A[X] * B[Y] +
maxProductUtil(X - 1, Y - 1,
A, B, dp),
A[X] * B[Y],
maxProductUtil(X - 1, Y,
A, B, dp),
maxProductUtil(X, Y - 1,
A, B, dp));
return dp[X, Y];
}
// Function to find maximum scalar
// product from same size sub-sequences
// taken from the two given array
static int maxProduct(int []A, int N,
int []B, int M)
{
// Initialize a 2-D array
// for memoization
int [,]dp = new int[N, M];
for(int i = 0; i < N; i++)
{
for(int j = 0; j < M; j++)
{
dp[i, j] = -1;
}
}
return maxProductUtil(N - 1, M - 1,
A, B, dp);
}
// Driver Code
public static void Main(String[] args)
{
int []a = { -2, 6, -2, -5 };
int []b = { -3, 4, -2, 8 };
int n = a.Length;
int m = b.Length;
Console.Write(maxProduct(a, n, b, m));
}
}
// This code is contributed by amal kumar choubey