Ivan is a student at Berland State University (BSU). There are n days in Berland week, and each of these days Ivan might have some classes at the university.

There are m working hours during each Berland day, and each lesson at the university lasts exactly one hour. If at some day Ivan's first lesson is during i-th hour, and last lesson is during j-th hour, then he spends j - i + 1 hours in the university during this day. If there are no lessons during some day, then Ivan stays at home and therefore spends 0 hours in the university.

Ivan doesn't like to spend a lot of time in the university, so he has decided to skip some lessons. He cannot skip more than k lessons during the week. After deciding which lessons he should skip and which he should attend, every day Ivan will enter the university right before the start of the first lesson he does not skip, and leave it after the end of the last lesson he decides to attend. If Ivan skips all lessons during some day, he doesn't go to the university that day at all.

Given n, m, k and Ivan's timetable, can you determine the minimum number of hours he has to spend in the university during one week, if he cannot skip more than k lessons?


-----Input-----

The first line contains three integers n, m and k (1 ≤ n, m ≤ 500, 0 ≤ k ≤ 500) — the number of days in the Berland week, the number of working hours during each day, and the number of lessons Ivan can skip, respectively.

Then n lines follow, i-th line containing a binary string of m characters. If j-th character in i-th line is 1, then Ivan has a lesson on i-th day during j-th hour (if it is 0, there is no such lesson).


-----Output-----

Print the minimum number of hours Ivan has to spend in the university during the week if he skips not more than k lessons.


-----Examples-----
Input
2 5 1
01001
10110

Output
5

Input
2 5 0
01001
10110

Output
8



-----Note-----

In the first example Ivan can skip any of two lessons during the first day, so he spends 1 hour during the first day and 4 hours during the second day.

In the second example Ivan can't skip any lessons, so he spends 4 hours every day.

Difficulty: MEDIUM_HARD
Tags: Dynamic programming
Skill types: Dynamic programming

Starter code:

Solutions:
def min_sub_array(day, k):
	if not day:
		return [0] * (k + 1)
	n = len(day)
	best = [float('inf')] * (n + 1)
	best[0] = 0
	best[1] = 1
	for size in range(2, n + 1):
		for i in range(n + 1 - size):
			best[size] = min(best[size], day[i + size - 1] - day[i] + 1)
	output = [0] * (k + 1)
	for i in range(k + 1):
		if n - i > 0:
			output[i] = best[n - i]
	return output
(N, M, K) = list(map(int, input().split()))
day = [i for (i, val) in enumerate(input()) if val == '1']
best = min_sub_array(day, K)
for _ in range(N - 1):
	day = [i for (i, val) in enumerate(input()) if val == '1']
	new_day_best = min_sub_array(day, K)
	new_best = [float('inf')] * (K + 1)
	for i in range(K + 1):
		for j in range(i + 1):
			new_best[i] = min(new_best[i], new_day_best[j] + best[i - j])
	best = new_best
print(best[K])


import sys
(n, m, k) = map(int, input().split())
table = [input() for _ in range(n)]
dp = [0] * (k + 1)
for a in table:
	one = []
	for i in range(m):
		if a[i] == '1':
			one.append(i)
	if not one:
		continue
	ni = len(one)
	subdp = [10 ** 9] * (ni + 1)
	subdp[-1] = 0
	for i in range(ni):
		for j in range(i, ni):
			subdp[ni - (j - i + 1)] = min(subdp[ni - (j - i + 1)], one[j] - one[i] + 1)
	next_dp = [10 ** 9] * (k + 1)
	for i in range(k, -1, -1):
		for j in range(ni + 1):
			if i + j > k:
				break
			next_dp[i + j] = min(next_dp[i + j], dp[i] + subdp[j])
	dp = next_dp
print(min(dp))


import queue
intput = lambda : map(int, input().split())
(N, M, K) = intput()
ht = [[] for _ in range(N)]
for _ in range(N):
	day = input()
	ht[_] = [i for i in range(M) if day[i] == '1']
tc = [1, 2, 3, 8, 9]
req = [[0 for _ in range(M + 1)] for __ in range(N)]
dp = [[0 for _ in range(K + 1)] for __ in range(N)]
for i in range(N):
	for j in range(len(ht[i])):
		req[i][j] = ht[i][-1] - ht[i][0] + 1
		for first in range(j + 1):
			last = first + len(ht[i]) - j - 1
			cost = ht[i][last] - ht[i][first] + 1
			if last >= first:
				req[i][j] = min(req[i][j], cost)
for i in range(min(len(req[0]), len(dp[0]))):
	dp[0][i] = req[0][i]
for i in range(1, N):
	for j in range(K + 1):
		dp[i][j] = dp[i - 1][j] + req[i][0]
		for z in range(1 + min(j, len(ht[i]))):
			dp[i][j] = min(dp[i][j], dp[i - 1][j - z] + req[i][z])
print(dp[-1][-1])


import math
import os
import sys
from io import BytesIO, IOBase
BUFSIZE = 8192

class FastIO(IOBase):
	newlines = 0

	def __init__(self, file):
		self._fd = file.fileno()
		self.buffer = BytesIO()
		self.writable = 'x' in file.mode or 'r' not in file.mode
		self.write = self.buffer.write if self.writable else None

	def read(self):
		while True:
			b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
			if not b:
				break
			ptr = self.buffer.tell()
			(self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr))
		self.newlines = 0
		return self.buffer.read()

	def readline(self):
		while self.newlines == 0:
			b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
			self.newlines = b.count(b'\n') + (not b)
			ptr = self.buffer.tell()
			(self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr))
		self.newlines -= 1
		return self.buffer.readline()

	def flush(self):
		if self.writable:
			os.write(self._fd, self.buffer.getvalue())
			(self.buffer.truncate(0), self.buffer.seek(0))

class IOWrapper(IOBase):

	def __init__(self, file):
		self.buffer = FastIO(file)
		self.flush = self.buffer.flush
		self.writable = self.buffer.writable
		self.write = lambda s: self.buffer.write(s.encode('ascii'))
		self.read = lambda : self.buffer.read().decode('ascii')
		self.readline = lambda : self.buffer.readline().decode('ascii')
(sys.stdin, sys.stdout) = (IOWrapper(sys.stdin), IOWrapper(sys.stdout))
input = lambda : sys.stdin.readline().rstrip('\r\n')

def calc(st, j):
	ans = 9999999999999999999999
	if j >= len(st):
		return 0
	j = len(st) - j
	for i in range(j - 1, len(st)):
		ans = min(ans, st[i] - st[i - j + 1] + 1)
	return ans
(n, m, k) = map(int, input().split())
s = []
for i in range(n):
	s.append(input())
inf = 99999999999999999999
dp = [[inf for i in range(k + 1)] for j in range(n + 1)]
for i in range(k + 1):
	dp[0][i] = 0
for i in range(1, n + 1):
	st = []
	for ik in range(len(s[i - 1])):
		if s[i - 1][ik] == '1':
			st.append(ik)
	for j in range(k + 1):
		no = calc(st, j)
		for t in range(k + 1 - j):
			dp[i][t + j] = min(dp[i][t + j], no + dp[i - 1][t])
print(dp[n][k])


R = lambda : map(int, input().split())
(n, m, k) = R()
cls = [list((i for (i, x) in enumerate(map(int, input())) if x)) for _ in range(n)]
dp = [[n * m] * (k + 1) for i in range(n + 1)]
dp.append([0] * (k + 1))
for i in range(n):
	row = cls[i]
	c2l = [m + 1] * (m + 1)
	c2l[0] = row[-1] - row[0] + 1 if row else 0
	c2l[len(row)] = 0
	for r in range(len(row)):
		for l in range(r + 1):
			c2l[len(row) - (r - l + 1)] = min(c2l[len(row) - (r - l + 1)], row[r] - row[l] + 1)
	for j in range(k + 1):
		for (c, l) in enumerate(c2l):
			if j + c <= k and l < m + 1:
				dp[i][j] = min(dp[i][j], dp[i - 1][j + c] + l)
print(min(dp[n - 1]))


import sys
input = sys.stdin.readline

def int_array():
	return list(map(int, input().strip().split()))

def float_array():
	return list(map(float, input().strip().split()))

def str_array():
	return input().strip().split()
from collections import Counter
import math
import bisect
from collections import deque
(n, m, lesson) = int_array()
dp = [[250005 for i in range(lesson + 2)] for j in range(n + 1)]
days = [[] for i in range(n)]
for i in range(n):
	s = input()
	for j in range(m):
		if s[j] == '1':
			days[i].append(j + 1)
m = [[250005 for i in range(lesson + 2)] for j in range(n + 1)]
for i in range(n):
	for j in range(lesson + 1):
		if j <= len(days[i]):
			if j == len(days[i]):
				m[i][j] = 0
				continue
			else:
				for k in range(0, j + 1):
					var = days[i][0 + k]
					var1 = days[i][-1 * max(1, 1 + (j - k))]
					m[i][j] = min(m[i][j], var1 - var + 1)
for i in range(lesson + 1):
	dp[0][i] = m[0][i]
for i in range(1, n):
	for j in range(lesson + 1):
		for k in range(j + 1):
			dp[i][j] = min(dp[i][j], dp[i - 1][j - k] + m[i][k])
print(min(dp[n - 1]))


(n, m, k) = input().split(' ')
n = int(n)
m = int(m)
k = int(k)
ind = []
pre = []
for _ in range(n):
	s = input()
	ind.append([])
	for (i, c) in enumerate(s):
		if c == '1':
			ind[-1].append(i)
for i in range(n):
	pre.append([])
	for j in range(k + 1):
		pre[i].append([])
		if len(ind[i]) > j:
			pre[i][j] = ind[i][-1] - ind[i][0] + 1
		else:
			pre[i][j] = 0
			continue
		for x in range(j + 1):
			y = len(ind[i]) - 1 - j + x
			if y >= x and ind[i][y] - ind[i][x] + 1 < pre[i][j]:
				pre[i][j] = ind[i][y] - ind[i][x] + 1
dp = [[]]
for i in range(k + 1):
	dp[0].append(pre[0][i])
for i in range(1, n):
	dp.append([])
	for j in range(0, k + 1):
		dp[i].append(pre[i][j] + dp[i - 1][0])
		for z in range(j + 1):
			dp[i][j] = min(dp[i][j], dp[i - 1][z] + pre[i][j - z])
print(dp[n - 1][k])


(n, m, k) = map(int, input().split())
DATA = [input() for i in range(n)]
INF = 1 << 60
dp = [[INF] * (k + 10) for i in range(n + 10)]
dp[0][0] = 0
COST = [[INF] * (k + 10) for i in range(n + 10)]
for (i, string) in enumerate(DATA):
	stack = []
	for j in range(m):
		if string[j] == '1':
			stack.append(j)
	L = len(stack)
	for j in range(k + 10):
		if j >= L:
			COST[i + 1][j] = 0
			continue
		else:
			for pos in range(j + 1):
				l = pos
				r = pos + L - 1 - j
				COST[i + 1][j] = min(COST[i + 1][j], stack[r] - stack[l] + 1)
for day in range(1, n + 1):
	for used_cost in range(k + 1):
		dp[day][used_cost] = min((dp[day - 1][prev_cost] + COST[day][used_cost - prev_cost] for prev_cost in range(used_cost + 1)))
ans = min((dp[n][used_cost] for used_cost in range(k + 1)))
print(ans)