Problem:


 おせんべい 

問題

IOI製菓では，創業以来の伝統の製法で煎餅（せんべい）を焼いている．この伝統の製法は，炭火で一定時間表側を焼き，表側が焼けると裏返して，炭火で一定時間裏側を焼くというものである．この伝統を守りつつ，煎餅を機械で焼いている．この機械は縦 R (1 ≤ R ≤ 10) 行, 横 C (1 ≤ C ≤ 10000) 列の長方形状に煎餅を並べて焼く．通常は自動運転で，表側が焼けたら一斉に煎餅を裏返し裏側を焼く．

ある日，煎餅を焼いていると，煎餅を裏返す直前に地震が起こり何枚かの煎餅が裏返ってしまった．幸いなことに炭火の状態は適切なままであったが，これ以上表側を焼くと創業以来の伝統で定められている焼き時間を超えてしまい，煎餅の表側が焼けすぎて商品として出荷できなくなる．そこで，急いで機械をマニュアル操作に変更し，まだ裏返っていない煎餅だけを裏返そうとした．この機械は，横の行を何行か同時に裏返したり縦の列を何列か同時に裏返したりすることはできるが，残念なことに，煎餅を１枚ごと裏返すことはできない．

裏返すのに時間がかかると，地震で裏返らなかった煎餅の表側が焼けすぎて商品として出荷できなくなるので，横の何行かを同時に１回裏返し，引き続き，縦の何列かを同時に１回裏返して，表側を焼きすぎずに両面を焼くことのできる煎餅，つまり，「出荷できる煎餅」の枚数をなるべく多くすることにした．横の行を１行も裏返さない，あるいは，縦の列を１列も裏返さない場合も考えることにする．出荷できる煎餅の枚数の最大値を出力するプログラムを書きなさい．

地震の直後に，煎餅が次の図のような状態になったとする．黒い丸が表側が焼ける状態を，白い丸が裏側が焼ける状態を表している． 

1行目を裏返すと次の図のような状態になる． 

さらに， 1列目と5列目を裏返すと次の図のような状態になる．この状態では，出荷できる煎餅は9枚である． 

ヒント

R の上限 10 は C の上限 10000 に比べて小さいことに注意せよ．

入力

入力は複数のデータセットからなる．各データセットは以下の形式で与えられる．

入力の1行目には2つの整数 R, C (1 ≤ R ≤ 10, 1 ≤ C ≤ 10 000) が空白を区切りとして書かれている．続く R 行は地震直後の煎餅の状態を表す． (i+1) 行目 (1 ≤ i ≤ R) には， C 個の整数 ai,1, ai,2, ……, ai,C が空白を区切りとして書かれており， ai,j は i 行 j 列 の煎餅の状態を表している. ai,j が 1 なら表側が焼けることを， 0 なら裏側が焼けることを表す．

C, R がともに 0 のとき入力の終了を示す.　データセットの数は 5 を超えない．

出力

データセットごとに，出荷できる煎餅の最大枚数を1行に出力する．

入出力例

入力例

2 5
0 1 0 1 0
1 0 0 0 1
3 6
1 0 0 0 1 0
1 1 1 0 1 0
1 0 1 1 0 1  
0 0

出力例

9
15

上記問題文と自動審判に使われるデータは、情報オリンピック日本委員会が作成し公開している問題文と採点用テストデータです。



Sample input:
2 5
0 1 0 1 0
1 0 0 0 1
3 6
1 0 0 0 1 0
1 1 1 0 1 0
1 0 1 1 0 1
0 0


Sample output:
9
15

Submission status: Accepted

Solution code:
import sys
e = sys.stdin.readlines()
while 1:
 r=int(e[0].split()[0])
 if r<1:break
 d=[int(''.join(x),2)for x in zip(*map(lambda x:x.split(),e[1:r+1]))]
 a=[]
 for i in range(1<<r):
  b=0
  for j in d:
   c=bin(i^j).count('1')
   b+=c if c>r//2 else r-c
  a+=[b]
 print(max(a))
 del e[:r+1]

Submission status: Accepted

Solution code:
for e in iter(input,'0 0'):
 r=int(e.split()[0])
 d=[int(''.join(x),2)for x in zip(*[input().split()for _ in[0]*r])]
 a=0
 b=1<<r
 f=[1]*b
 for m in range(b):
  if f[m]:
   f[~m]=0
   t=0
   for s in d:
    c=bin(m^s).count('1')
    t+=c if c>r//2 else r-c
   if a<t:a=t
 print(a)

Submission status: Accepted

Solution code:
for e in iter(input,'0 0'):
 r=int(e.split()[0])
 d=[int(''.join(x),2)for x in zip(*[input().split()for _ in[0]*r])]
 a=0
 for m in range(1<<~-r):
  t=0
  for s in d:
   c=bin(m^s).count('1')
   t+=c if c>r//2 else r-c
  if a<t:a=t
 print(a)

Submission status: Accepted

Solution code:
def v():
 for e in iter(input,'0 0'):
  r=int(e.split()[0])
  d=[int(''.join(x),2)for x in zip(*[input().split()for _ in[0]*r])]
  a=0
  for m in range(1<<~-r):
   t=0
   for s in d:
    c=bin(m^s).count('1')
    t+=c if c>r//2 else r-c
   if a<t:a=t
  print(a)
if'__main__'==__name__:v()

Submission status: Accepted

Solution code:
def v():
 for e in iter(input,'0 0'):
  r=int(e.split()[0])
  d=[int(''.join(x),2)for x in zip(*[input().split()for _ in[0]*r])]
  a=0
  for m in range(1<<~-r):
   t=0
   for s in d:
    c=bin(m^s).count('1')
    t+=c if c>r/2 else r-c
   if a<t:a=t
  print(a)
if'__main__'==__name__:v()

Submission status: Accepted

Solution code:
def v():
 for e in iter(input,'0 0'):
  r=int(e.split()[0])
  d=[int(''.join(x),2)for x in zip(*[input().split()for _ in[0]*r])]
  a=0
  for m in range(1<<~-r):
   t=0
   for s in d:
    c=bin(m^s).count('1')
    t+=max(c,r-c)
   if a<t:a=t
  print(a)
if'__main__'==__name__:v()

Submission status: Accepted

Solution code:
while True:
  r,c = map(int,input().split())
  if not r:
    break
  lst = [list(map(int,input().split())) for i in range(r)]
  dic = [0 for i in range(2 ** r)]
  for i in range(c):
    num = 0
    for j in range(r):
      num *= 2
      num += lst[j][i]
    dic[num] += 1
  ans = 0
  for i in range(2 ** r):
    ret = 0
    for j in range(2 ** r):
      num = (i ^ j)
      cnt = 0
      for k in range(r):
        cnt += num % 2
        num //= 2
      ret += max(cnt, r - cnt) * dic[j]
    ans = max(ans, ret)
  print(ans)

Submission status: Accepted

Solution code:
def f():
  while True:
    r,c = map(int,input().split())
    if not r:
      break
    lst = [list(map(int,input().split())) for i in range(r)]
    dic = [0 for i in range(2 ** r)]
    for i in range(c):
      num = 0
      for j in range(r):
        num *= 2
        num += lst[j][i]
      dic[num] += 1
    ans = 0
    for i in range(2 ** r):
      ret = 0
      for j in range(2 ** r):
        num = (i ^ j)
        cnt = 0
        for k in range(r):
          cnt += num % 2
          num //= 2
        ret += max(cnt, r - cnt) * dic[j]
      ans = max(ans, ret)
    print(ans)
f()

Submission status: Accepted

Solution code:
while True:
    r,c=map(int,raw_input().split())
    if r==c==0:break
    state=[raw_input().split() for i in range(r)]
    state=map(list,zip(*state))
    new_state=[int("".join(ls),2) for ls in state]
    ans=0
    h=r/2
    for i in range(pow(2,r)):
        cnt=0
        for j in new_state:
            n=bin(i^j).count("1")
            cnt+=n if n>h else r-n
        ans=max(ans,cnt)
    print ans

Submission status: Accepted

Solution code:
# AOJ 0525

while True:
    R, C = map(int, input().split())
    if R == C == 0:
        break
    osenbei = [input().split() for i in range(R)]
    osenbei = list(zip(*osenbei))  # create a list of columns
    osenbei = [int(''.join(c), 2) for c in osenbei]  # create binary representation of each column

    ans = 0
    for mask in range(1 << R):
        count = 0
        for c in osenbei:
            count_c = bin(c ^ mask).count('1')  # flip specified row
            count += max(count_c, R - count_c)
        ans = max(ans, count)

    print(ans)

Submission status: Accepted

Solution code:
# -*- coding: utf-8 -*-
"""
Osenbei
http://judge.u-aizu.ac.jp/onlinejudge/description.jsp?id=0525

"""
import sys
from itertools import product


def solve(osenbei, row, col):
    ans = 0
    for bits in product([0, 1], repeat=row):
        res = 0
        sen = [[] for _ in range(row)]
        for i, b in enumerate(bits):
            sen[i] = [[1, 0][o] for o in osenbei[i]] if b else osenbei[i]
        for z in zip(*sen):
            t = sum(z)
            res += max(t, row-t)
        ans = max(ans, res)
    return ans


def main(args):
    while True:
        r, c = map(int, input().split())
        if r == 0 and c == 0:
            break
        osenbei = [[int(j) for j in input().split()] for i in range(r)]
        ans = solve(osenbei, r, c)
        print(ans)


if __name__ == '__main__':
    main(sys.argv[1:])

Submission status: Runtime Error

Solution code:
import copy
import itertools
import numpy
def solve():
    max = 0
    for i in range(-1,H):
        for rs in itertools.combinations(range(H),i+1):
            tmpMap = copy.deepcopy(osenbei)
            tmpMap = reverseR(tmpMap, rs)
            #print(numpy.array(tmpMap),'=>')
            cs = []
            for j in range(W):
                if needReverse(tmpMap,j):
                    cs.append(j)
            tmpMap = reverseC(tmpMap, cs)
            #print(numpy.array(tmpMap))
            tmp = countZero(tmpMap)
            if max < tmp:
                max = tmp
    return max

def needReverse(tmpMap,j):
    #?????£???????????????????????????????????????
    sum = 0
    for a in range(H):
        sum += int(tmpMap[a][j])
    #print('sum is :', sum)
    return sum>(H/2)

def countZero(tmpO):
    c = 0
    for i in range(H):
        c += tmpO[i].count('0')
    return c

def countReverse(rs,cs):
    tmpO = copy.deepcopy(osenbei)
    tmpO = reverseR(tmpO, rs)
    tmpO = reverseC(tmpO, cs)
    return countZero(tmpO)

def reverseR(tmpO, rs):
    for r in rs:
        for i in range(W):
            if tmpO[r][i] == '0': tmpO[r][i] = '1'
            elif tmpO[r][i] == '1': tmpO[r][i] = '0'
    return tmpO

def reverseC(tmpO, cs):
    for c in cs:
        for i in range(H):
            if tmpO[i][c] == '0': tmpO[i][c] = '1'
            elif tmpO[i][c] == '1': tmpO[i][c] = '0'
    return tmpO

H = -1
W = -1
osenbei = []
counter = -1

line = input()
while line!='0 0':
    if H == -1:
        H,W = map(int,line.split())
        osenbei = [[-1 for i in range(W)] for j in range(H)]
        counter = 0
    else:
        osenbei[counter] = line.split()
        counter += 1
    if counter == H:
        print(solve())
        H = -1
        W = -1
        
    line = input()

Submission status: Runtime Error

Solution code:
while 1:
????????r, c = map(int, raw_input().split())
????????if r == 0: break
????????old = [raw_input().split() for i in range(r)]
????????old = map(list, zip(*old))
????????new = [int("".join(lst),2) for lst in old]
????????ans = 0
????????for i in range(1<<(r-1)):
????????????????ret = 0
????????????????for j in new:
????????????????????????cnt = bin(i^j).count('1')
????????????????????????ret += cnt if cnt > r/2 else r-cnt
????????????????ans = max(ans, ret)
????????print ans

Submission status: Runtime Error

Solution code:
while 1:
????????r, c = map(int, raw_input().split())
????????if r == 0: break
????????old = [raw_input().split() for i in range(r)]
????????old = map(list, zip(*old))
????????new = [int("".join(lst),2) for lst in old]
????????ans = 0
????????for i in range(1<<(r)):
????????????????ret = 0
????????????????for j in new:
????????????????????????cnt = bin(i^j).count('1')
????????????????????????ret += cnt if cnt > r/2 else r-cnt
????????????????ans = max(ans, ret)
????????print ans

Submission status: Runtime Error

Solution code:
import sys

def solve(A, R, C):
    result = 0
    for i in range(2 ** (R-1)):
        s = 0
        for j in range(C):
            number_of_0s = 0
            for k in range(R):
                a = A[k][j]
                flip_kth_row = (i >> k) & 1
                if a ^ flip_kth_row: # XOR
                    number_of_0s += 1
            number_of_1s = R - number_of_0s
            max_column += max(number_of_0s, number_of_1s)
        result = max(result, max_column)
    print(result)

while True:
    R, C = map(int, sys.stdin.readline().split())
    if R == 0 and C == 0:
        break

    A = []
    for _ in range(R):
        A.append(list(map(int, sys.stdin.readline().split())))
    solve(A, R, C)

Submission status: Runtime Error

Solution code:
for e in iter(input,'0 0'):
 H,W=map(int,e.split())
 s=[[*map(int,input().split())]for _ in[0]*H]
 print(max(sum(max(sum(c),H-sum(c))for c in zip([c^int(b)for c in r]for r,b in zip(s,bin(x)[2:].zfill(H)))))for x in range(2**H)))

Submission status: Runtime Error

Solution code:
for e in iter(input,'0 0'):
 H,W=map(int,e.split())
 s=[[*map(int,input().split())]for _ in[0]*H]
 print(max(sum(max(sum(c),H-sum(c))for c in zip([c^int(b)for c in r]for r,b in zip(s,bin(x)[2:].zfill(H)))for x in range(2**H))))

Submission status: Runtime Error

Solution code:
for e in iter(input,'0 0'):
 H,W=map(int,e.split())
 s=[[*map(int,input().split())]for _ in[0]*H]
 print(max(sum(max(sum(c),H-sum(c))for x in range(2**H)for c in zip([c^int(b)for c in r]for r,b in zip(s,bin(x)[2:].zfill(H))))))

Submission status: Runtime Error

Solution code:
for e in iter(input,'0 0'):
 H,W=map(int,e.split())
 s=[[*map(int,input().split())]for _ in[0]*H]
 print(max(sum(max(sum(c),H-sum(c))for x in range(2**H))for c in zip([c^int(b)for c in r]for r,b in zip(s,bin(x)[2:].zfill(H)))))

Submission status: Runtime Error

Solution code:
for e in iter(input,'0 0'):
 H,W=map(int,e.split())
 s=[[*map(int,input().split())]for _ in[0]*H]
 print(max(sum(max(sum(c),H-sum(c))for c in zip([c^int(b)for c in r]for r,b in zip(s,bin(x)[2:].zfill(H))))for x in range(2**H)))

Submission status: Runtime Error

Solution code:
for e in iter(input,'0 0'):
 r=int(e.split()[0])
 d=[int(''.join(x),2)for x in zip(*[input().split()for _ in[0]*r])]
 a=0
 b=1<<r
 c=[1]*b
 for m in range(b):
  if c[m]:
   c[~m]=0
   t=0
   for s in d:
    c=bin(m^s).count('1')
    t+=c if c>r//2 else r-c
   if a<t:a=t
 print(a)