Harryxun/stf_run huggingface dataset:
import sys
input = lambda : sys.stdin.readline().rstrip()
sys.setrecursionlimit(max(1000, 10**9))
write = lambda x: sys.stdout.write(x+"\n")


n = int(input())
p = list(map(int, input().split()))
g1 = [None]*(n+10)
v = 1
g1[0] = 1
for i in range(1,len(g1)):
    v *= i
    v %= M
    g1[i] = v
    
def init(bit, values):
    for i,v in enumerate(values):
        add(bit,i+1,v)
#a1 ~ aiまでの和 O(logn)
def query(bit,i):
    res = 0
    while i > 0:
        res += bit[i]
        i -= i&(-i)
    return res

#ai += x(logN)
def add(bit,i,x):
    if i==0:
        raise RuntimeError
    while i <= len(bit)-1:
        bit[i] += x
        i += i&(-i)
    return


index = [i for i,num in enumerate(p) if num==0]
used = [False]*n
for i in range(n):
    if p[i]>0:
        used[p[i]-1] = True
nl = []
for i in range(n):
    if not used[i]:
        nl.append(i+1)
nl.sort()
scores = [0]*(n+1) # scores[i]: nlのうちiより大きいものの個数
v = len(nl)
cur = 0
for i in range(1,n+1):
    if nl and nl[cur]<i:
        cur += 1
        v -= 1
    scores[i] = v
ans = 0
bit = [0]*(n+1)
s = set(index)
val = 0
pp = len(index) # 欠損の個数
ss = sum(nl)
num = 0
inv2 = pow(2, M-2, M)
for i in range(n):
    if i in s:
        ans += (g1[pp-1] * (ss - pp - val - pp*(num)*inv2)) * g1[n-i-1]
        num += 1
    else:
        ans += (g1[pp] * (p[i]-query(bit, p[i])-1) - g1[pp-1]*num*(len(nl)-scores[p[i]])) * g1[n-i-1]
        add(bit, p[i], 1)
        val += scores[p[i]]
    ans %= M
#     print(ans, val)
print((ans + g1[pp])%M)

### FIXED CODE ###

import sys
input = lambda : sys.stdin.readline().rstrip()
sys.setrecursionlimit(max(1000, 10**9))
write = lambda x: sys.stdout.write(x+"\n")


M = 10**9+7
n = int(input())
p = list(map(int, input().split()))
g1 = [None]*(n+10)
v = 1
g1[0] = 1
for i in range(1,len(g1)):
    v *= i
    v %= M
    g1[i] = v
    
def init(bit, values):
    for i,v in enumerate(values):
        add(bit,i+1,v)
#a1 ~ aiまでの和 O(logn)
def query(bit,i):
    res = 0
    while i > 0:
        res += bit[i]
        i -= i&(-i)
    return res

#ai += x(logN)
def add(bit,i,x):
    if i==0:
        raise RuntimeError
    while i <= len(bit)-1:
        bit[i] += x
        i += i&(-i)
    return


index = [i for i,num in enumerate(p) if num==0]
used = [False]*n
for i in range(n):
    if p[i]>0:
        used[p[i]-1] = True
nl = []
for i in range(n):
    if not used[i]:
        nl.append(i+1)
nl.sort()
scores = [0]*(n+1) # scores[i]: nlのうちiより大きいものの個数
v = len(nl)
cur = 0
for i in range(1,n+1):
    if nl and cur<len(nl) and nl[cur]<i:
#     if nl and nl[cur]<i:
        cur += 1
        v -= 1
    scores[i] = v
ans = 0
bit = [0]*(n+1)
s = set(index)
val = 0
pp = len(index) # 欠損の個数
ss = sum(nl)
num = 0
inv2 = pow(2, M-2, M)
for i in range(n):
    if p[i]==0:
        ans += (g1[pp-1] * (ss - pp - val - pp*(num)*inv2)) * g1[n-i-1]
        num += 1
    else:
        ans += (g1[pp] * (p[i]-query(bit, p[i])-1) - g1[pp-1]*num*(len(nl)-scores[p[i]])) * g1[n-i-1]
        add(bit, p[i], 1)
        val += scores[p[i]]
    ans %= M
#     print(ans, val)
print((ans + g1[pp])%M)
def popcount(n):
    bit_count = bin(n).count('1')
    n = n % bit_count 
    return n

if __name__ == "__main__":
    import sys 

    readline = sys.stdin.readline 

    N = int(input())
    X_str= readline()
    X = list(X_str)
    bit_count = X_str.count('1')
    X_int = int(X_str, 2)


    for i in range(N):
        # ビットの反転
        x_copy = X.copy()
        X_int_copy = X_int
        bit_count_n = bit_count
        if x_copy[i] == '0':
            x_copy[i] = '1'
            bit_count_n += 1
            X_int_copy += pow(2, N - 1 - i)
        else:
            x_copy[i] = '0'
            bit_count_n -= 1
            X_int_copy -= pow(2, N - 1 - i)

        # print("反転後{}".format(X_int_copy))
        # ビット反転後に0になった場合
        if X_int_copy == 0:
            print(0)

        # 1回操作しておく
        popcount_X = X_int_copy % bit_count_n 
        counter = 1
        while popcount_X != 0:
            popcount_X = popcount(popcount_X)
            counter += 1

        print(counter)






### FIXED CODE ###

def popcount(n):
    bit_count = bin(n).count('1')
    n = n % bit_count 
    return n

if __name__ == "__main__":
    import sys 

    readline = sys.stdin.readline 

    N = int(input())
    X = readline()
    bit_count = X.count('1')
    mod1 = int(X, 2) % (bit_count + 1)
    mod2 = int(X, 2) % (bit_count - 1) if (bit_count - 1) != 0 else 0

    for i in range(N):
        if X[i] == '0':
            X_rest = (mod1 + pow(2, N - 1 - i, bit_count + 1)) % (bit_count + 1)
        else:
            if bit_count - 1 != 0:
                X_rest = (mod2 - pow(2, N - 1 - i, bit_count - 1)) % (bit_count - 1)
            else:
                print(0)
                continue

        counter = 1
        while X_rest != 0:
            X_rest = popcount(X_rest)
            counter += 1
        print(counter)
n, m = map(int, input().split())
q = list()
for i in range(m):
    q.append(list(map(int, input().split())))
p = list(map(int, input().split()))

ans = 0

for s in range(2**n):
    judge = True
    for i in range(m):
        cnt = 0
        for j in range(1, q[i][0]):
            w = q[i][j]
            if s >>(w-1) & 1 == 1:
                cnt += 1
        if cnt % 2 != p[i]:
            judge = False
    if judge:
        res += 1
print(res)

### FIXED CODE ###

n, m = map(int, input().split())
q = list()
for i in range(m):
    q.append(list(map(int, input().split())))
p = list(map(int, input().split()))

ans = 0

for s in range(2**n):
    judge = True
    for i in range(m):
        cnt = 0
        for j in range(1, len(q[i])):
            w = q[i][j]
            if s >>(w-1) & 1 == 1:
                cnt += 1
        if cnt % 2 != p[i]:
            judge = False
    if judge:
        ans += 1
print(ans)
from heapq import heappop, heappush
N, M = map(int, input().split())
node_num = N
route_list = [[] for _ in range(N)]
for i in range(M):
    L, R, C = map(int, input().split())
    route_list[L-1].append((R-1, C))
for i in range(N-1):
    route_list[i+1].append(i)

distance = [10**9] * node_num
distance[0] = 0  # 初期のノードの距離は0とする
q = [(0, 0)]
 
while q:
    _, u = heappop(q)
    for des, cost in route_list[u]:
        if distance[des] > distance[u] + cost:
            distance[des] = distance[u] + cost
            heappush(q, (distance[des], des))
 
print(distance[-1] if distance[-1] != 10**9 else -1)

### FIXED CODE ###

from heapq import heappop, heappush
N, M = map(int, input().split())
route_list = [[] for _ in range(N)]
for i in range(M):
    L, R, C = map(int, input().split())
    route_list[L-1].append((R-1, C))
for i in range(N-1):
    route_list[i+1].append((i, 0))

distance = [float("inf")] * N
distance[0] = 0  # 初期のノードの距離は0とする
q = [(0, 0)]

while q:
    _, u = heappop(q)
    for des, cost in route_list[u]:
        if distance[des] > distance[u] + cost:
            distance[des] = distance[u] + cost
            heappush(q, (distance[des], des))

print(distance[-1] if distance[-1] != float("inf") else -1)
X, Y, Z, K = map(int, input().split())

A_list = list(map(int, input().split()))
B_list = list(map(int, input().split()))
C_list = list(map(int, input().split()))

val_list = []


A_list = sorted(A_list, reverse=True)
B_list = sorted(B_list, reverse=True)
C_list = sorted(C_list, reverse=True)

for a in range(len(A_list)):
    if a + 1 > K:
        break
    for b in range(len(B_list)):
        if b + 1 > K:
            break
        if (a+1) * (b+1) > K:
            break
            
        for c in range(len(C_list)):
            if c + 1 > K:
                break
            if (a+1) * (b+1) * (c+1) > K:
                break
        
            val_list.append(A_list[a] + B_list[b] + C_list[c])

val_list = sorted(val_list, reverse=True)
for i in range(K):
    print(val_list[i])

    


            


### FIXED CODE ###

antennas = []
for _ in range(5):
    x = int(input())
    antennas.append(x)
k = int(input())

import itertools

ans = True
for x, y in list(itertools.combinations(antennas,2)):
    dist = abs(x - y)
    if dist > k:
        ans = False
        break

if ans == True:
    print('Yay!')
else:
    print(':(')
def rec(d, outer = False):
  head, tail = d[-1], d[0]
  if len(d) == 0:
    return 1
  if len(d) == 1:
    return 10-outer if tail == 0 else 0

  # (10-tail) ways: (0,tail)-(9-tail,9) -> (-tail, tail)
  # (tail) ways:    (10-tail,0)-(9,tail-1) -> (10-tail, -10 + tail)

  t = head + tail
  if len(d) == 2:
    if t == 0:
      return 10-tail
    if t == 9:
      return tail-outer
    else:
      return 0
    
  else:
    sub = d[1:-1]
    if -1 <= t <= 0:
      sub[-1] += t*10
      return (10-tail)*rec(sub)
    t -= 10
    if -1 <= t <= 0:
      sub[-1] += t*10
      sub[0] += 1
      return (tail-outer)*rec(sub)


D = list(map(int,input()))
D = D[::-1]

leading_zeros = 0
while D[0] == 0:
  leading_zeros += 1
D = D[leading_zeros:]

r = rec(D, outer=True)
if r != 0 and leading_zeros > 0:
  r *= 9 * (10**(leading_zeros-1))
print(r)

### FIXED CODE ###

# (10-x) ways: (0,x)-(9-x,9) -> (x, -x)           <- This should never happens when D > 0
# (x) ways:    (10-x,0)-(9,x-1) -> (x-10, 10 - x)

def solve(d, k, outer):
  if k <= 1:
    return 10**k if d == 0 else 0
  # if d >= 10**k:
  #   return 0

  t = (-d)%10 # 10-d%10 doesn't work when d = 10
  d -= t*10**(k-1) - t
  return (10-t-outer)*solve(abs(d//10), k-2, 0)

D = int(input())
result = 0
for k in range(1,20):
  result += solve(D, k, 1)

print(result)
a,s,d=map(int, input().split())
print('Yes' if a+b>=c else 'No')

### FIXED CODE ###

a,s,d=map(int, input().split())
print('Yes' if a+s>=d else 'No')
N, K = map(int, input().split())

# N, K = 3, 2

if K % 2 != 0:
    ans = (N // K)**3
else:
    full = []
    half = []
    for i in range(int(K/2), N+1, int(K/2)):
        if i % K == 0:
            full.append(i)
        else:
            half.append(i)

    ans += len(half) ** 3 + len(full) ** 3

print(ans)

### FIXED CODE ###

N, K = map(int, input().split())

# N, K = 3, 2

if K % 2 != 0:
    ans = (N // K)**3
else:
    full = []
    half = []
    for i in range(int(K/2), N+1, int(K/2)):
        if i % K == 0:
            full.append(i)
        else:
            half.append(i)

    ans = len(half) ** 3 + len(full) ** 3

print(ans)
A,B=map(int,input().split())
min_a=12.5*A;max_a=(100*a+1)/8
min_b=10*b;max_b=min_b+1
if(max_b<min_a or max_a<min_b):
  print(-1)
else:
  print(int(round(max(12.5*A,10*B)+0.4999999,0)))

### FIXED CODE ###

A, B =map(int, input().split())
max_A=(A+1)*25/2
max_B=10*(B+1)
p=int(round(max([A*12.5,B*10])+0.4999999999,0))
print(p if(p<min([(A+1)*25/2, 10*(B+1)]) and not(4<5*A-4*B) and not(5*A-4*B<-5)) else -1)
N,A,B=map(int,input().split())
List=""
while len(List) < N:
    List=+="b"*A+"r"*B
if len(List) >= N:
  ans=List[:N]
  print(ans.count("b"))


### FIXED CODE ###

S=input()
if S == "AAA" or S == "BBB":
  print("No")
else:
  print("Yes")
#O(ElogV)
import heapq
def dijkstra_heap(s,g):
    used = [True] * n #True:未確定
    used[s] = False
    edgelist = []
    for e in edge[s]:
        heapq.heappush(edgelist,[[0,e[0]],e[1]])
    while len(edgelist):
        minedge = heapq.heappop(edgelist)
        #まだ使われてない頂点の中から最小の距離のものを探す
        if not used[minedge[1]]:
            continue
        v = minedge[1]
        if v==g:
            return (minedge[0][0]+1)*k
        nl = minedge[0][1]
        kai=minedge[0][0]
        used[v] = False
        for e in edge[v]:
            if used[e[1]]:
                if e[0]==nl:
                    heapq.heappush(edgelist,[[kai,e[0]],e[1]])
                else:
                    heapq.heappush(edgelist,[[kai+1,e[0]],e[1]])
                    
    return -1

################################
n,m = map(int,input().split()) #n:頂点数　w:辺の数

edge = [[] for i in range(n)]
#edge[i] : iから出る道の[重み,行先]の配列
l=[]
for i in range(m):
    x,y,z = map(int,input().split())
    edge[x-1].append([z,y-1])
    edge[y-1].append([z,x-1])
    l.append([x-1,y-1])
zeroedge=[0]*n
l.sort()
l.append([10**10,10**10+1])
i=0
nl=l[i][0]
r=l[i][1]
while i<n+1:
    if l[i][0]>r:
        for j in range(nl,r):
            edge[j+1].append([0,j])
        nl=l[i][0]
        r=l[i][1]
        i+=1
    else:
        r=max(r,l[i][1])
        i+=1       
print(dijkstra_heap(0,n-1))

### FIXED CODE ###

import heapq
def dijkstra_heap(s,g):
    used = [True] * n #True:未確定
    used[s] = False
    edgelist = []
    for e in edge[s]:
        heapq.heappush(edgelist,[e[0],e[1]])
    while len(edgelist):
        minedge = heapq.heappop(edgelist)
        #まだ使われてない頂点の中から最小の距離のものを探す
        if not used[minedge[1]]:
            continue
        v = minedge[1]
        if v==g:
            return minedge[0]
        kai=minedge[0]
        used[v] = False
        for e in edge[v]:
            if used[e[1]]:
                heapq.heappush(edgelist,[kai+e[0],e[1]])                   
    return -1

################################
n,m = map(int,input().split()) #n:頂点数　w:辺の数

edge = [[] for i in range(n)]
#edge[i] : iから出る道の[重み,行先]の配列
l=[]
for i in range(m):
    x,y,z = map(int,input().split())
    edge[x-1].append([z,y-1])
    edge[y-1].append([z,x-1])
    l.append([x-1,y-1])
zeroedge=[0]*n
l.sort()
l.append([10**10,10**10+1])
i=0
nl=l[i][0]
r=l[i][1]
while i<m+1:
    if l[i][0]>r:
        for j in range(nl,r):
            edge[j+1].append([0,j])
        nl=l[i][0]
        r=l[i][1]
        i+=1
    else:
        r=max(r,l[i][1])
        i+=1       
print(dijkstra_heap(0,n-1))
pattern = {
    0: ["#", "."],
    1: [".", "#"],
}

while True:
    (H, W) = [int(i) for i in input().split())]
    if H == W == 0:
        break
for j in range(H):
        p = pattern[j % 2]
        print(.join([p[i % 2] for i in range(W)]))
        print()

### FIXED CODE ###

while True:
    H, W = [int(i) for i in input().split()]
    if H == W == 0:
        break

    for h in range(H):
        for w in range(W):
            if (h + w) % 2 == 0:
                print('#', end='')
            else:
                print('.', end='')

        print()
    print()
K, X = [int(K), int(X) for K, X in input().split()]

if K*500 >= X:
  print("Yes")
else:
  print("No")

### FIXED CODE ###

K, X = [int(x) for x in input().split()]

if K*500 >= X:
  print("Yes")
else:
  print("No")
import math


def solve():
    A, B, N = map(float, input().split())

    ans = 0
    div_ab = A/B
    div_1b = 1/B
    init_1 = (max(1, int(N)-100000)*A)/B
    init_2 = max(1, int(N)-100000)/B
    start = max(1, int(N)-100000)

    for i in range(max(1, int(N)-100000), int(N) + 1):
        ans = max(math.floor(init_1+div_ab*(i-start)) - A*math.floor(init_2+div_1b*(i-start)),ans)
    start = max(B-1000,1)
    init_1 = (max(1, int(B-100000)) * A) / B
    init_2 = max(1, int(B-100000)) / B
    for i in range(max(B-100000,1), int(B)+1):
        ans = max(math.floor(init_1 + div_ab * (i - start)) - A * math.floor(init_2 + div_1b * (i - start)), ans)

    print(int(ans))


if __name__ == '__main__':
    solve()


### FIXED CODE ###

def solve():
    A,B,N = map(int, input().split())
    if N < B-1:
        print((A*N)//B - A * (N//B))
    else:
        print(((A*(B-1))//B) - A* ((B-1)//B))


if __name__ == "__main__":
    solve()
n,m = map(int,input().split())
a = list(map(int, input().split()))
a.sort()
s = []
for i in range(m):
    b,c = map(int,input().split())
    for j in range(b):
        s.append(c)
s.sort()
s.reverse()
d = max(n,len(s))

for i in range(n):
    if s[0] > a[i]:
        a[i] = s[0]
        s.remove(s[0])
    else:
        break
    if i == d-1:
        break


print(sum(a))


### FIXED CODE ###

n,m = map(int,input().split())
a = list(map(int, input().split()))
a.sort()
e =0
bc = []
for i in range(m):
    d = list(map(int,input().split()))
    bc.append([d[1],d[0]])
    e += d[0] 
#print(a)  
 
 
bc.sort()
bc.reverse()
#print(bc)
 
#print(e)
#print(a[n-1])
j = 0
for i in range(n):
    if j == bc[0][1] :
        bc.remove(bc[0])
        j = 0
    if bc[0][0] > a[i]:
        a[i] = bc[0][0]
        j += 1
    else:
        break
    if i == e-1:
        break
 
 
#print(a)
print(sum(a))
import math
import copy

N = int(input())
A = list(map(int,input().split()))

result = 0

def gcd_list(nums):
    x = math.gcd(nums[0],nums[1])
    for i in range(2,len(nums)):
        x = math.gcd(nums[i],x)
    return x

for i in range(N):
    a = copy.deepcopy(A)
    del a[i]
    result = max(gcd_list(a),result)

print(result)

### FIXED CODE ###

from fractions import gcd

N = int(input())
A = list(map(int, input().split()))

gcdl = [1]*N
gcdl[0] = A[0]

gcdr = [1]*N
gcdr[-1] = A[-1]

for i in range(1, N):
    gcdl[i] = gcd(gcdl[i-1], A[i])
    gcdr[N-i-1] = gcd(gcdr[N-i], A[N-i-1])

res = max(gcdl[N-2], gcdr[1])

for i in range(1, N-1):
    res = max(gcd(gcdl[i-1], gcdr[i+1]), res)

print(res)
def mark(s, x):
    for i in range(x + x, len(s), x):
        s[i] = False

def sieve(n):
    s = [True] * n
    for x in range(2, int(n**0.5) + 1):
        if s[x]: mark(s, x)
    return [i for i in range(0,n) if s[i] and i > 1]

n = []
while True:
    tmp = int(input())
    if tmp == 0:
        break
    n.append(tmp)
m = max(n)
p = sieve(10000)

for i in range(len(n)):
    sum = 0
    if n[i] == 1:
        print(2)
    else:
        for j in range(n[i]):
            sum += p[j]
        print(sum)

### FIXED CODE ###

def mark(s, x):
    for i in range(x + x, len(s), x):
        s[i] = False

def sieve(n):
    s = [True] * n
    for x in range(2, int(n**0.5) + 1):
        if s[x]: mark(s, x)
    return [i for i in range(0,n) if s[i] and i > 1]

n = []
while True:
    tmp = int(input())
    if tmp == 0:
        break
    n.append(tmp)
m = max(n)
p = sieve(500000)

for i in range(len(n)):
    sum = 0
    if n[i] == 1:
        print(2)
    else:
        for j in range(n[i]):
            sum += p[j]
        print(sum)
n, m = map(int, input().split())

fact = []
for i in range(1, int(m**0.5)):
    if m%i==0:
        fact.append(i)
        fact.append(m//i)
fact.sort()

for i in fact:
    if n*i<=m: ans = i
print(ans)

### FIXED CODE ###

def prime_factor(a):
    ret = []
    for i in range(1, int(a**0.5)):
        if a%i==0:
            ret.append(i)
            ret.append(a//i)
    return ret

n, m = map(int, input().split())
ans = 1
for i in prime_factor(m):
    if i<=m/n: ans = max(ans, i)
print(ans)
H,W=map(int,input().split())

board=[]
check=[[None]*W for i in range(H)]
answer=0

for i in range(H):
  S=list(input())
  board.append(S)
  
def dfs(i,j,color):
  if not (0<=i<H and 0<=j<W):
    return 0,0
  if board[i][j]==color:
    return 0,0
  if check[i][j]:
    return 0,0
  
  check[i][j]=True
  black,white=0,0
  
  if board[i][j]=='#':
  	black+=1
  else:
    white+=1
 
  tempblack,tempwhite=dfs(i+1,j,board[i][j])
  black+=tempblack
  white+=tempwhite
  
  tempblack,tempwhite=dfs(i-1,j,board[i][j])
  black+=tempblack
  white+=tempwhite
  
  tempblack,tempwhite=dfs(i,j+1,board[i][j])
  black+=tempblack
  white+=tempwhite

  tempblack,tempwhite=dfs(i,j-1,board[i][j])
  black+=tempblack
  white+=tempwhite
  return black,white

for i in range(H):
  for j in range(W):
    black,white=dfs(i,j,"!")
    answer+=black*white
print(answer)

### FIXED CODE ###

import sys
sys.setrecursionlimit(1000000)

H,W=map(int,input().split())

board=[]
check=[[None]*W for i in range(H)]
answer=0

for i in range(H):
  S=list(input())
  board.append(S)
  
def dfs(i,j,color):
  if not (0<=i<H and 0<=j<W):
    return 0,0
  if board[i][j]==color:
    return 0,0
  if check[i][j]:
    return 0,0
  
  check[i][j]=True
  black,white=0,0
  
  if board[i][j]=='#':
  	black+=1
  else:
    white+=1
 
  tempblack,tempwhite=dfs(i+1,j,board[i][j])
  black+=tempblack
  white+=tempwhite
  
  tempblack,tempwhite=dfs(i-1,j,board[i][j])
  black+=tempblack
  white+=tempwhite
  
  tempblack,tempwhite=dfs(i,j+1,board[i][j])
  black+=tempblack
  white+=tempwhite

  tempblack,tempwhite=dfs(i,j-1,board[i][j])
  black+=tempblack
  white+=tempwhite
  return black,white

for i in range(H):
  for j in range(W):
    black,white=dfs(i,j,"!")
    answer+=black*white
print(answer)
def solve(a, b):
    pdp = {a[0]:0}
    ndp = {}
    for ai in a[1:] + [0]:
        for k, v in pdp.items():
            for bi in b:
                vb = v + st(k, bi)
                kb = k & (65535 ^ bi) | ai
                try:
                    ndp[kb] = max(ndp[kb], vb)
                except KeyError:
                    ndp[kb] = vb
        pdp, ndp = ndp, {}
        
    return max(pdp.values())

@memoize
def st(i, j):
    return bin(i).count('1') - bin(i & (65535 ^ j)).count('1')

def memoize(f):
    cache = {}
    def helper(x, y):
        if (x, y) not in cache:
            cache[(x, y)] = f(x, y)
        return cache[(x, y)]
    return helper

import sys
f = sys.stdin

while True:
    n, c = map(int, f.readline().split())
    if n == c == 0:
        break
    a = [int(''.join(f.readline().split()), 2) for i in range(n)]
    b = [int(''.join(f.readline().split()), 2) for i in range(c)]
    print(solve(a, b))

### FIXED CODE ###

bitcount = [bin(i).count('1') for i in range(0x10000)]
def solve(a, b):
    global bitcount
    b = [65535 ^ bi for bi in b]
    pdp = {a[0]:0}
    ndp = {}
    for ai in a[1:] + [0]:
        for k, v in pdp.items():
            v += bitcount[k]
            for bi in b:
                bi &= k
                vb = v - bitcount[bi]
                bi |= ai
                try:
                    if ndp[bi] < vb:
                        ndp[bi] = vb
                except KeyError:
                    ndp[bi] = vb
        pdp, ndp = ndp, {}
        
    return max(pdp.values())

import sys
f = sys.stdin


while True:
    n, c = map(int, f.readline().split())
    if n == c == 0:
        break
    a = [int(''.join(f.readline().split()), 2) for i in range(n)]
    b = [int(''.join(f.readline().split()), 2) for i in range(c)]
    print(solve(a, b))
N=int(input())
sisterN=[]
SISTERn=[]
for n in range(N):
    _=int(input())
    if _<50000:
        _= s_
        if sisterN.count(s_)==1:
            sisterN.remove(_s)
        else:
            sisterN.append(_s)

    else:
        _= b_
        if SISTERn.count(_b)==1:
            SISTERn.remove(_b)
        else:
            SISTERn.append(_b)
print(len(sisterN)+len(SISTERn))

### FIXED CODE ###

N=int(input())
S=set()

for n in range(N):
    _=int(input())
    if _ in S:
        S.remove(_)
    else:
        S.add(_)

print(len(S))
import sys
sys.setrecursionlimit(10 ** 7)

sr = lambda: sys.stdin.readline().rstrip()
ir = lambda: int(sr())
lr = lambda: list(map(int, sr().split()))

N, D, A = lr()
XH = [lr() for _ in range(N)]
XH.sort(key=lambda x:x[0])

over = [N] * (N+1) # over[i]はi番目のモンスターを攻撃した時影響のないモンスターの最小値（最大はNで対象なし）
cur = 1
for i in range(N):
    while cur <= N and XH[i][0] + D + D >= XH[cur][0]:
        cur += 1
    over[i] = cur

damage = 0 # 今までの累計のダメージ数　(影響のないダメージは引く)
reset = [0] * (N+1) # 対象のモンスターの影響しなかったダメージ
answer = 0
for i in range(N):
    x, h = XH[i]
    damage -= reset[i]
    h = h - damage
    time = (h+A-1)//A
    answer += time
    damage += A * time
    reset[over[i]] += A * time

print(answer)
# 00


### FIXED CODE ###

import sys
sys.setrecursionlimit(10 ** 7)

sr = lambda: sys.stdin.readline().rstrip()
ir = lambda: int(sr())
lr = lambda: list(map(int, sr().split()))

N, D, A = lr()
XH = [lr() for _ in range(N)]
XH.sort()

over = [N] * N # over[i]はi番目のモンスターを攻撃した時影響のないモンスターの最小値（最大はNで対象なし）
cur = 1
for i in range(N):
    while cur < N and XH[i][0] + D + D >= XH[cur][0]:
        cur += 1
    over[i] = cur

damage = 0 # 今までの累計のダメージ数　(影響のないダメージは引く)
reset = [0] * (N+1) # これ以降関係のないダメージ
answer = 0
for i in range(N):
    x, h = XH[i]
    damage -= reset[i]
    h -= damage
    if h > 0:
        time = (h+A-1)//A
        answer += time
        damage += A * time
        reset[over[i]] += A * time

print(answer)
7
nikoandsolstice

### FIXED CODE ###

K=int(input())
S=input()
if len(S)<=K:print(S)
else:print(S[0:K]+'...')