// C# program for above approach
using System;
class GFG{
const int maxN = 201;
// Variables which represent
// the size of the array
static int n1, n2, n3;
// Stores the results
static int[,,] dp = new int[maxN, maxN, maxN];
// Function to return the
// maximum possible sum
static int getMaxSum(int i, int j,
int k, int []arr1,
int []arr2, int []arr3)
{
// Stores the count of
// arrays processed
int cnt = 0;
if (i >= n1)
cnt++;
if (j >= n2)
cnt++;
if (k >= n3)
cnt++;
// If more than two arrays
// have been processed
if (cnt >= 2)
return 0;
// If an already computed
// subproblem occurred
if (dp[i, j, k] != -1)
return dp[i, j, k];
int ans = 0;
// Explore all the possible pairs
if (i < n1 && j < n2)
// Recursive function call
ans = Math.Max(ans,
getMaxSum(i + 1, j + 1, k,
arr1, arr2, arr3) +
arr1[i] * arr2[j]);
if (i < n1 && k < n3)
ans = Math.Max(ans,
getMaxSum(i + 1, j, k + 1,
arr1, arr2, arr3) +
arr1[i] * arr3[k]);
if (j < n2 && k < n3)
ans = Math.Max(ans,
getMaxSum(i, j + 1, k + 1,
arr1, arr2, arr3) +
arr2[j] * arr3[k]);
// Memoize the maximum
dp[i, j, k] = ans;
// Returning the value
return dp[i, j, k];
}
static void reverse(int[] tmp)
{
int i, t;
int n = tmp.Length;
for(i = 0; i < n / 2; i++)
{
t = tmp[i];
tmp[i] = tmp[n - i - 1];
tmp[n - i - 1] = t;
}
}
// Function to return the maximum sum of
// products of pairs possible
static int maxProductSum(int []arr1, int []arr2,
int []arr3)
{
// Initialising the dp array to -1
for(int i = 0; i < maxN; i++)
for(int j = 0; j < maxN; j++)
for(int k = 0; k < maxN; k++)
dp[i, j, k] = -1;
// Sort the arrays in descending order
Array.Sort(arr1);
reverse(arr1);
Array.Sort(arr2);
reverse(arr2);
Array.Sort(arr3);
reverse(arr3);
return getMaxSum(0, 0, 0,
arr1, arr2, arr3);
}
// Driver Code
public static void Main (string[] args)
{
n1 = 2;
int []arr1 = { 3, 5 };
n2 = 2;
int []arr2 = { 2, 1 };
n3 = 3;
int []arr3 = { 4, 3, 5 };
Console.Write(maxProductSum(arr1, arr2, arr3));
}
}
// This code is contributed by rutvik_56