using
System;
using
System.Collections.Generic;
class
GFG
{
readonly
static
int
MAX = 1001;
static
int
[,]dp =
new
int
[MAX, MAX];
static
int
minCost(String s,
int
k,
int
cost,
int
i,
int
n)
{
int
count_0 = 0;
if
(dp[i, k] != -1)
return
dp[i, k];
if
(k == 1)
{
int
total = n - i;
while
(i < n)
if
(s[i++] ==
'0'
)
count_0++;
dp[i, k] = cost + (count_0
* (total - count_0));
return
dp[i, k];
}
int
curr_cost =
int
.MaxValue;
count_0 = 0;
for
(
int
j = i; j < n - k + 1; j++)
{
if
(s[j] ==
'0'
)
count_0++;
int
curr_partlength = j - i + 1;
int
part_cost = (count_0
* (curr_partlength - count_0));
part_cost += minCost(s, k - 1, 0, j + 1, n);
curr_cost = Math.Min(curr_cost, part_cost);
}
dp[i, k] = (cost + curr_cost);
return
(cost + curr_cost);
}
public
static
void
Main (String[] args)
{
String s =
"110101"
;
int
n = s.Length;
int
k = 3;
for
(
int
i = 0; i < MAX; i++)
{
for
(
int
j = 0; j < MAX; j++)
dp[i, j] = -1;
}
Console.WriteLine(minCost(s, k, 0, 0, n));
}
}