using
System;
class
GFG
{
static
int
SIZE = 3;
static
int
N = 3;
static
int
minSum(
int
[]A,
int
[]B,
int
[]C,
int
i,
int
n,
int
curr,
int
[,]dp)
{
if
(n <= 0)
return
0;
if
(dp[n,curr] != -1)
return
dp[n,curr];
if
(curr == 0)
{
return
dp[n,curr]
= Math.Min(B[i] + minSum(A, B, C, i + 1, n - 1, 1, dp),
C[i] + minSum(A, B, C, i + 1, n - 1, 2, dp));
}
if
(curr == 1)
return
dp[n,curr]
= Math.Min(A[i] + minSum(A, B, C, i + 1, n - 1, 0, dp),
C[i] + minSum(A, B, C, i + 1, n - 1, 2, dp));
return
dp[n,curr]
= Math.Min(A[i] + minSum(A, B, C, i + 1, n - 1, 0, dp),
B[i] + minSum(A, B, C, i + 1, n - 1, 1, dp));
}
public
static
void
Main ()
{
int
[]A = { 1, 50, 1 };
int
[]B = { 50, 50, 50 };
int
[]C = { 50, 50, 50 };
int
[,]dp =
new
int
[SIZE,N];
for
(
int
i = 0; i < SIZE; i++)
for
(
int
j = 0; j < N; j++)
dp[i,j] = -1;
Console.WriteLine(Math.Min(A[0] + minSum(A, B, C, 1, SIZE - 1, 0, dp),
Math.Min(B[0] + minSum(A, B, C, 1, SIZE - 1, 1, dp),
C[0] + minSum(A, B, C, 1, SIZE - 1, 2, dp))));
}
}