Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Codeforces 229A
n, m = map(int, raw_input().split())
table = [map(int, list(raw_input())) for _ in xrange(n)]
weighted_table = []
for r in table:
if sum(r) == 0:
print -1
exit()
weighted_row = []
for i in xrange(len(r)):
if r[i] == 1:
weighted_row.append(0)
continue
ii = i
ii += len(r)
r *= 3
a = [abs(j - i) for j in xrange(len(r)) if r[j] == 1]
b = [abs(j - i) for j in xrange(i, -1, -1) if r[j] == 1]
weight = None
if len(a) == 0:
weight = min(b)
elif len(b) == 0:
weight = min(a)
else:
weight = min(min(a), min(b))
weighted_row.append(weight)
weighted_table.append(weighted_row)
mini = None
for c in xrange(len(weighted_table[0])):
if mini == None:
mini = sum(r[c] for r in weighted_table)
mini = min(mini, sum(r[c] for r in weighted_table))
print mini
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment