using
System;
using
System.Collections.Generic;
public
class
GFG
{
static
bool
Solve(
int
N,
int
K,
int
target, List<
int
> coins,
bool
[,] dp)
{
if
(K == 0 && target == 0)
return
true
;
if
(K <= 0 || target <= 0)
return
false
;
if
(dp[K, target])
return
true
;
bool
ans =
false
;
for
(
int
i = 0; i < N; i++)
{
if
(target - coins[i] >= 0)
ans |= Solve(N, K - 1, target - coins[i], coins, dp);
}
dp[K, target] = ans;
return
ans;
}
static
bool
MakeChanges(
int
N,
int
K,
int
target, List<
int
> coins)
{
bool
[,] dp =
new
bool
[K + 1, target + 1];
for
(
int
i = 0; i <= K; i++)
{
for
(
int
j = 0; j <= target; j++)
{
dp[i, j] =
false
;
}
}
return
Solve(N, K, target, coins, dp);
}
public
static
void
Main(
string
[] args)
{
int
n1 = 4;
int
k1 = 2;
int
target1 = 9;
List<
int
> coins1 =
new
List<
int
> { 2, 3, 5, 7 };
Console.WriteLine(Convert.ToInt32(MakeChanges(n1, k1, target1, coins1)));
int
n2 = 3;
int
k2 = 4;
int
target2 = 12;
List<
int
> coins2 =
new
List<
int
> { 1, 1, 6 };
Console.WriteLine(Convert.ToInt32(MakeChanges(n2, k2, target2, coins2)));
}
}