using System;
class Program
{
// Declare a dp
static int[,,] dp = new int[105, 105, 105];
// Function to iterate in dp
static int Rec(string A, string B, int i1, int i2, int k)
{
// If length of lcs string is reached
if (k == 0)
return 0;
// if any of the string is completed
if (i1 < 0 || i2 < 0)
{
if (k == 0)
return 0;
return int.MaxValue;
}
// If value of that is already found
if (dp[i1, i2, k] != -1)
return dp[i1, i2, k];
// If values are equal
if (A[i1] == B[i2])
dp[i1, i2, k] = Rec(A, B, i1 - 1, i2 - 1, k - 1);
else
{
// Otherwise check by adding cost
int cost = Math.Abs((int)A[i1] - (int)B[i2]);
dp[i1, i2, k] = Math.Min(Rec(A, B, i1 - 1, i2, k),
Math.Min(cost + Rec(A, B, i1 - 1, i2 - 1, k - 1),
Rec(A, B, i1, i2 - 1, k)));
}
// Return the value
return dp[i1, i2, k];
}
// Function to find minimum cost to get lcs of length
// k
static int GenerateSub(string A, string B, int N, int M, int K)
{
// Fill the dp with -1
for (int i = 0; i < 105; i++)
for (int j = 0; j < 105; j++)
for (int l = 0; l < 105; l++)
dp[i, j, l] = -1;
// Iterate in dp
return Rec(A, B, N - 1, M - 1, K);
}
// Driver code
static void Main()
{
int K = 3;
string A = "abcba";
string B = "acyx";
int N = A.Length;
int M = B.Length;
// Function call
Console.WriteLine(GenerateSub(A, B, N, M, K));
}
}