x, y, z, k = map(int, input().split())
a = map(int, input().split())
b = list(map(int, input().split()))
c = map(int, input().split())
d = sorted((ai + bi for ai in a for bi in b), reverse=True)[:k]
e = sorted((ci + di for ci in c for di in d), reverse=True)
i = 0
while i != k:
    print(e[i])
    i += 1

x, y, z, k = map(int, input().split())
a = map(int, input().split())
b = list(map(int, input().split()))
c = map(int, input().split())
d = sorted((ai + bi for ai in a for bi in b), reverse=True)[:k]
e = sorted((ci + di for ci in c for di in d), reverse=True)[:k]
print(*e)


x, y, z, k = map(int, input().split())
a = tuple(map(int, input().split()))
b = tuple(map(int, input().split()))
c = tuple(map(int, input().split()))

ab = [i + j for i in a for j in b]
ab.sort(reverse=True)
abc = [i + j for i in ab[:k] for j in c]

abc.sort(reverse=True)
print("\\n".join(map(str, abc[:k])))

x, y, z, k = map(int, input().split())
a = list(map(int, input().split()))
b = list(map(int, input().split()))
c = list(map(int, input().split()))

a.sort(reverse=True)
b.sort(reverse=True)
c.sort(reverse=True)
ans = []
for i in range(x):
    for j in range(y):
        for m in range(z):
            if (i + 1) * (j + 1) * (m + 1) > k:
                break
            ans.append(a[i] + b[j] + c[m])

ans.sort(reverse=True)
for i in ans[:k]:
    print(i)


X, Y, Z, K = map(int, input().split())
A = sorted(list(map(int, input().split())), reverse=True)
B = sorted(list(map(int, input().split())), reverse=True)
C = sorted(list(map(int, input().split())), reverse=True)

AB = []
for a in A[:K]:
    for b in B[:K]:
        AB.append(a + b)
AB.sort(reverse=True)

ABC = []
for ab in AB[:K]:
    for c in C[:K]:
        ABC.append(ab + c)
ABC.sort(reverse=True)

for abc in ABC[:K]:
    print(abc)

X, Y, Z, K = map(int, input().split())
A = sorted(list(map(int, input().split())), reverse=True)
B = sorted(list(map(int, input().split())), reverse=True)
C = sorted(list(map(int, input().split())), reverse=True)

AB = [a + b for a in A for b in B]
AB.sort(reverse=True)
ABC = [ab + c for ab in AB[:K] for c in C[:K]]
ABC.sort(reverse=True)
[print(abc) for abc in ABC[:K]]


X, Y, Z, K = map(int, input().split())
A = sorted(map(int, input().split()), reverse=True)[:K]
B = sorted(map(int, input().split()), reverse=True)[:K]
C = sorted(map(int, input().split()), reverse=True)[:K]

AB = sorted([a + b for a in A for b in B], reverse=True)[:K]
ABC = sorted([ab + c for ab in AB for c in C], reverse=True)[:K]
for v in ABC:
    print(v)

from heapq import heappush, heappop

X, Y, Z, K = map(int, input().split())
A = sorted(list(map(int, input().split())), reverse=True)
B = sorted(list(map(int, input().split())), reverse=True)
C = sorted(list(map(int, input().split())), reverse=True)

cake_heap = []
for i in range(len(A)):
    if i + 1 > K:
        break
    for j in range(len(B)):
        if (i + 1) * (j + 1) > K:
            break
        for k in range(len(C)):
            if (i + 1) * (j + 1) * (k + 1) > K:
                break
            heappush(cake_heap, -(A[i] + B[j] + C[k]))
for i in range(K):
    print(-heappop(cake_heap))


from itertools import product

x, y, z, k = list(map(int, input().split()))
a = list(map(int, input().split()))
b = list(map(int, input().split()))
c = list(map(int, input().split()))

ab = [ai + bi for (ai, bi) in product(a, b)]
ab.sort(reverse=True)
abc = [abi + ci for (abi, ci) in product(ab[: min(k, x * y * z)], c)]
abc.sort(reverse=True)

for abci in abc[: min(k, x * y * z)]:
    print(abci)

from itertools import product

x, y, z, k = list(map(int, input().split()))
a = sorted(list(map(int, input().split())), reverse=True)
b = sorted(list(map(int, input().split())), reverse=True)
c = sorted(list(map(int, input().split())), reverse=True)

ab = [ai + bi for (ai, bi) in product(a[:k], b[:k])]
ab.sort(reverse=True)
abc = [abi + ci for (abi, ci) in product(ab[: min(k, x * y * z)], c[:k])]
abc.sort(reverse=True)

for abci in abc[: min(k, x * y * z)]:
    print(abci)


# -*- coding: utf-8 -*-

X, Y, Z, K = map(int, input().split())
# A = sorted(map(int, input().split()), reverse=True)
# B = sorted(map(int, input().split()), reverse=True)
# C = sorted(map(int, input().split()), reverse=True)
A = list(map(int, input().split()))
B = list(map(int, input().split()))
C = list(map(int, input().split()))

AB = []

for a in A:
    for b in B:
        AB.append(a + b)

AB = sorted(AB, reverse=True)

if K < len(AB):
    AB = AB[:K]

ABC = []
for ab in AB:
    for c in C:
        ABC.append(ab + c)

ABC = sorted(ABC, reverse=True)

ABC = ABC[:K]
ABC = [str(abc) for abc in ABC]

print("\\n".join(ABC))

# -*- coding: utf-8 -*-

X, Y, Z, K = map(int, input().split())
A = sorted(map(int, input().split()), reverse=True)
B = sorted(map(int, input().split()), reverse=True)
C = sorted(map(int, input().split()), reverse=True)

ABC = []

for x in range(X):
    for y in range(Y):
        if (x + 1) * (y + 1) > K:
            break
        for z in range(Z):
            if (x + 1) * (y + 1) * (z + 1) > K:
                break
            ABC.append(A[x] + B[y] + C[z])

ABC = sorted(ABC, reverse=True)
ABC = ABC[:K]

print("\\n".join(map(str, ABC)))


from heapq import heappop, heappush

X, Y, Z, K = [int(s) for s in input().split()]
A = [int(s) for s in input().split()]
B = [int(s) for s in input().split()]
C = [int(s) for s in input().split()]
h = []
for i in range(X):
    for j in range(Y):
        for k in range(Z):
            heappush(h, -(A[i] + B[j] + C[k]))
for _ in range(K):
    print(-heappop(h))

from heapq import heappop, heappush

X, Y, Z, K = [int(s) for s in input().split()]
A = [int(s) for s in input().split()]
B = [int(s) for s in input().split()]
C = [int(s) for s in input().split()]
A.sort(reverse=True)
B.sort(reverse=True)
C.sort(reverse=True)
counted = set()
h = [(-(A[0] + B[0] + C[0]), 0, 0, 0)]
for _ in range(K):
    s, i, j, k = heappop(h)
    print(-s)
    if X > i + 1 and (i + 1, j, k) not in counted:
        heappush(h, (-(A[i + 1] + B[j] + C[k]), i + 1, j, k))
        counted.add((i + 1, j, k))
    if Y > j + 1 and (i, j + 1, k) not in counted:
        heappush(h, (-(A[i] + B[j + 1] + C[k]), i, j + 1, k))
        counted.add((i, j + 1, k))
    if Z > k + 1 and (i, j, k + 1) not in counted:
        heappush(h, (-(A[i] + B[j] + C[k + 1]), i, j, k + 1))
        counted.add((i, j, k + 1))


x, y, z, k = map(int, input().split())
a = list(map(int, input().split()))
b = list(map(int, input().split()))
c = list(map(int, input().split()))
c.sort(reverse=True)

p = [0 for _ in range(x * y)]
t = 0
for ai in a:
    for bi in b:
        p[t] = ai + bi
        t += 1
p.sort(reverse=True)
q = [0 for _ in range(k * z)]
t = 0
for pi in p[:k]:
    for ci in c[:k]:
        q[t] = pi + ci
        t += 1
q.sort(reverse=True)
for qi in q[:k]:
    print(qi)

x, y, z, kk = map(int, input().split())
a = list(map(int, input().split()))
b = list(map(int, input().split()))
c = list(map(int, input().split()))
a.sort(reverse=True)
b.sort(reverse=True)
c.sort(reverse=True)

p = []
for i in range(x):
    for j in range(y):
        for k in range(z):
            if (i + 1) * (j + 1) * (k + 1) <= kk:
                p.append(a[i] + b[j] + c[k])
            else:
                break
p.sort(reverse=True)
for i in range(kk):
    print(p[i])


from itertools import product, chain

x, y, z, k = list(map(int, input().split(" ")))
a = sorted(list(map(int, input().split(" "))))
b = sorted(list(map(int, input().split(" "))))
c = sorted(list(map(int, input().split(" "))))

apb_sorted = sorted([a + b for a, b in product(a, b)])
ans_list = sorted(
    list(chain.from_iterable([[_c + ab for ab in apb_sorted] for _c in c[:k]]))
)
it = reversed(ans_list)
for _ in range(k):
    print(next(it))

from itertools import product, chain

x, y, z, k = list(map(int, input().split(" ")))
a = sorted(list(map(int, input().split(" "))))
b = sorted(list(map(int, input().split(" "))))
c = sorted(list(map(int, input().split(" "))))

apb_sorted = sorted([a + b for a, b in product(a, b)])[-k:]
ans_list = sorted(
    list(chain.from_iterable([[_c + ab for ab in apb_sorted] for _c in c[:k]]))
)
it = reversed(ans_list)
for _ in range(k):
    print(next(it))


# -*- coding: utf-8 -*-

X, Y, Z, K = map(int, input().split())
A = list(map(int, input().split()))
B = list(map(int, input().split()))
C = list(map(int, input().split()))

E = []
for i in range(X):
    for j in range(Y):
        E.append(A[i] + B[j])
E.sort(reverse=True)

F = []
for i in range(min(X * Y, K)):
    for j in range(Z):
        F.append(E[i] + C[j])
F.sort(reverse=True)

for i in range(K):
    print(F[i])

# -*- coding: utf-8 -*-

X, Y, Z, K = map(int, input().split())
A = sorted(list(map(int, input().split())), reverse=True)
B = sorted(list(map(int, input().split())), reverse=True)
C = sorted(list(map(int, input().split())), reverse=True)

Comb = []
for i in range(X):
    for j in range(Y):
        if (i + 1) * (j + 1) > K:
            break
        for k in range(Z):
            if (i + 1) * (j + 1) * (k + 1) > K:
                break
            Comb.append(A[i] + B[j] + C[k])

Comb.sort(reverse=True)
for i in range(K):
    print(Comb[i])


from itertools import product, chain
from collections import deque

def solver1(a, b, c, k):
    apb_sorted = sorted([a + b for a, b in product(a, b)])[-k:]
    ans_list = sorted(
        list(chain.from_iterable([[_c + ab for ab in apb_sorted] for _c in c[:k]]))
    )
    it = reversed(ans_list)
    for _ in range(k):
        print(next(it))

def solver2(a, b, c, k):
    d = deque()
    n = len(a)
    m = min(k, n)
    for p in range(m):
        for q in range(m):
            for r in range(m):
                if (p + 1) * (q + 1) * (r + 1) > k:
                    continue
                d.append(a[-p - 1] + b[-q - 1] + c[-r - 1])
    it = reversed(sorted(d))
    for _ in range(k):
        print(next(it))

if __name__ == "__main__":
    x, y, z, k = list(map(int, input().split(" ")))
    a = sorted(list(map(int, input().split(" "))))
    b = sorted(list(map(int, input().split(" "))))
    c = sorted(list(map(int, input().split(" "))))
    solver2(a, b, c, k)

from itertools import product, chain
from collections import deque

def solver1(a, b, c, k):
    apb_sorted = sorted([a + b for a, b in product(a, b)])[-k:]
    ans_list = sorted(
        list(chain.from_iterable([[_c + ab for ab in apb_sorted] for _c in c[:k]]))
    )
    it = reversed(ans_list)
    for _ in range(k):
        print(next(it))

def solver2(a, b, c, k):
    n = len(a)
    m = min(k, n)
    pqr = deque()
    for p in range(m):
        for q in range(m):
            for r in range(m):
                if (p + 1) * (q + 1) * (r + 1) > k:
                    break
                else:
                    pqr.append(a[-p - 1] + b[-q - 1] + c[-r - 1])
    it = reversed(sorted(pqr))
    for _ in range(k):
        print(next(it))

if __name__ == "__main__":
    x, y, z, k = list(map(int, input().split(" ")))
    a = sorted(list(map(int, input().split(" "))))
    b = sorted(list(map(int, input().split(" "))))
    c = sorted(list(map(int, input().split(" "))))
    solver2(a, b, c, k)


X, Y, Z, K = map(int, input().split())
A = list(map(int, input().split()))
B = list(map(int, input().split()))
C = list(map(int, input().split()))

A.sort(reverse=True)
B.sort(reverse=True)
C.sort(reverse=True)

ABC = []
for i in range(X):
    for j in range(Y):
        if i + 1 + j + 1 > K:
            break
        for k in range(Z):
            if i + 1 + j + 1 + k + 1 > K:
                break
            ABC.append(A[i] + B[j] + C[k])

ABC.sort(reverse=True)
for i in range(K):
    print(ABC[i])

X, Y, Z, K = map(int, input().split())
A = list(map(int, input().split()))
B = list(map(int, input().split()))
C = list(map(int, input().split()))

A.sort(reverse=True)
B.sort(reverse=True)
C.sort(reverse=True)

ABC = []
for i in range(X):
    for j in range(Y):
        if (i + 1) * (j + 1) > K:
            break
        for k in range(Z):
            if (i + 1) * (j + 1) * (k + 1) > K:
                break
            ABC.append(A[i] + B[j] + C[k])

ABC.sort(reverse=True)
for i in range(K):
    print(ABC[i])


import itertools

X, Y, Z, K = list(map(int, input().split()))
A = list(map(int, input().split()))
B = list(map(int, input().split()))
C = list(map(int, input().split()))

AB = [a + b for (a, b) in list(itertools.product(A, B))]
AB = sorted(AB)[::-1]
ans = [ab + c for (ab, c) in list(itertools.product(AB[:3000], C))]
ans = sorted(ans)[::-1]
for i in range(K):
    print(ans[i])

import itertools

X, Y, Z, K = list(map(int, input().split()))
A = list(map(int, input().split()))
B = list(map(int, input().split()))
C = list(map(int, input().split()))

AB = [a + b for (a, b) in list(itertools.product(A, B))]
AB.sort(reverse=True)
ans = [ab + c for (ab, c) in list(itertools.product(AB[: min(3000, X * Y * Z)], C))]
ans.sort(reverse=True)
for i in range(K):
    print(ans[i])


from heapq import heappush, heappop

X, Y, Z, K = map(int, input().split())
A = [int(s) for s in input().split()]
B = [int(s) for s in input().split()]
C = [int(s) for s in input().split()]
A.sort(reverse=True)
B.sort(reverse=True)
C.sort(reverse=True)
h = []

maxv = A[0] + B[0] + C[0]

def inv(v):
    return maxv - v

pushed = []
heappush(h, (0, 0, 0, 0))
for _ in range(K):
    invv, i, j, k = heappop(h)
    print(inv(invv))
    if i + 1 < X and (i + 1, j, k) not in pushed:
        heappush(h, (inv(A[i + 1] + B[j] + C[k]), i + 1, j, k))
        pushed.append((i + 1, j, k))
    if j + 1 < Y and (i, j + 1, k) not in pushed:
        heappush(h, (inv(A[i] + B[j + 1] + C[k]), i, j + 1, k))
        pushed.append((i, j + 1, k))
    if k + 1 < Z and (i, j, k + 1) not in pushed:
        heappush(h, (inv(A[i] + B[j] + C[k + 1]), i, j, k + 1))
        pushed.append((i, j, k + 1))

from heapq import heappush, heappop

X, Y, Z, K = map(int, input().split())
A = [int(s) for s in input().split()]
B = [int(s) for s in input().split()]
C = [int(s) for s in input().split()]
A.sort(reverse=True)
B.sort(reverse=True)
C.sort(reverse=True)
h = []

maxv = A[0] + B[0] + C[0]

def inv(v):
    return maxv - v

pushed = [[[] for _ in range(Y)] for _ in range(X)]
heappush(h, (0, 0, 0, 0))
for _ in range(K):
    invv, i, j, k = heappop(h)
    print(inv(invv))
    if i + 1 < X and k not in pushed[i + 1][j]:
        heappush(h, (inv(A[i + 1] + B[j] + C[k]), i + 1, j, k))
        pushed[i + 1][j].append(k)
    if j + 1 < Y and k not in pushed[i][j + 1]:
        heappush(h, (inv(A[i] + B[j + 1] + C[k]), i, j + 1, k))
        pushed[i][j + 1].append(k)
    if k + 1 < Z and k + 1 not in pushed[i][j]:
        heappush(h, (inv(A[i] + B[j] + C[k + 1]), i, j, k + 1))
        pushed[i][j].append(k + 1)


X, Y, Z, K = map(int, input().split())
A = list(map(int, input().split()))
B = list(map(int, input().split()))
C = list(map(int, input().split()))
tmp = []
for a in A:
    for b in B:
        tmp.append(a + b)
ans = []
for ab in tmp:
    for c in C:
        ans.append(ab + c)
ans.sort(reverse=True)
for i in range(K):
    print(ans[i])

X, Y, Z, K = map(int, input().split())
A = list(map(int, input().split()))
B = list(map(int, input().split()))
C = list(map(int, input().split()))

# A+Bの上位K個
tmp = []
for a in A:
    for b in B:
        tmp.append(a + b)
tmp.sort(reverse=True)
tmp = tmp[:K]

ans = []
for ab in tmp:
    for c in C:
        ans.append(ab + c)
ans.sort(reverse=True)

for i in range(K):
    print(ans[i])


X, Y, Z, K = map(int, input().split())
A = sorted(list(map(int, input().split())), reverse=True)
K1 = min(K, len(A))

B = sorted(list(map(int, input().split())), reverse=True)
K2 = min(K, len(B))

C = sorted(list(map(int, input().split())), reverse=True)
K3 = min(K, len(C))

K12 = min(K1 * K2, K)

M1 = [0] * (K1 * K2)
k = 0
for i in range(0, K1):
    for j in range(0, K2):
        M1[k] = A[i] + B[j]
        k += 1

M1.sort(reverse=True)

M = [0] * (K12 * K3)
k = 0
for i in range(0, K12):
    for j in range(0, K3):
        M[k] = M1[i] + C[j]
        k += 1

M.sort(reverse=True)
for i in range(0, K):
    print(M[i])

X, Y, Z, K = map(int, input().split())
A = sorted(list(map(int, input().split())), reverse=True)
B = sorted(list(map(int, input().split())), reverse=True)
C = sorted(list(map(int, input().split())), reverse=True)

M = []
for i in range(0, X):
    for j in range(0, Y):
        for k in range(0, Z):
            if (i + 1) * (j + 1) * (k + 1) <= K:
                M.append(A[i] + B[j] + C[k])
            else:
                break

M.sort(reverse=True)
for i in range(0, K):
    print(M[i])


# pypyでギリ。
import sys

read = sys.stdin.buffer.read
readline = sys.stdin.buffer.readline
readlines = sys.stdin.buffer.readlines

x, y, z, k = map(int, readline().split())
A = list(map(int, readline().split()))
B = list(map(int, readline().split()))
C = list(map(int, readline().split()))
goukei = []
for a in A:
    for b in B:
        goukei.append(a + b)
goukei.sort()
goukei2 = goukei[-k:]
goukei3 = []
for c in C:
    for ab in goukei2:
        goukei3.append(ab + c)
goukei3.sort(reverse=True)
print(*goukei3[:k], sep="\\n")

from heapq import heappush, heappop

x, y, z, k = map(int, input().split())
A = list(map(int, input().split()))
B = list(map(int, input().split()))
C = list(map(int, input().split()))
# used = [[[False] * (z + 1) for _ in range(y + 1)] for _ in range(x + 1)]
used = set()
A.sort(reverse=True)
B.sort(reverse=True)
C.sort(reverse=True)
kouho = [(-(A[0] + B[0] + C[0]), 0, 0, 0)]

def calc(a, b, c):
    if not (a, b, c) in used:
        used.add((a, b, c))
        heappush(kouho, ((-A[a] - B[b] - C[c]), a, b, c))

used.add((0, 0, 0))
for i in range(k):
    temp, p, q, r = heappop(kouho)
    print(-temp)
    if p + 1 < x:
        calc(p + 1, q, r)
    if q + 1 < y:
        calc(p, q + 1, r)
    if r + 1 < z:
        calc(p, q, r + 1)


def main():
    X, Y, Z, K = map(int, input().split())
    A = sorted(list(map(int, input().split())), reverse=True)
    B = sorted(list(map(int, input().split())), reverse=True)
    C = sorted(list(map(int, input().split())), reverse=True)
    L = []
    for a in A:
        for b in B:
            L.append(a + b)
    LL = []
    for ab in L:
        for c in C:
            LL.append(ab + c)
    for x in sorted(LL, reverse=True)[:K]:
        print(x)

main()

def main():
    X, Y, Z, K = map(int, input().split())
    A = sorted(list(map(int, input().split())), reverse=True)
    B = sorted(list(map(int, input().split())), reverse=True)
    C = sorted(list(map(int, input().split())), reverse=True)
    AB = []
    for a in A:
        for b in B:
            AB.append(a + b)
    AB.sort(reverse=True)
    ABC = []
    for i in range(min(K, len(AB))):
        for c in C:
            ABC.append(AB[i] + c)
    ABC.sort(reverse=True)
    for x in ABC[:K]:
        print(x)

main()


x, y, z, k = map(int, input().split())
a = list(map(int, input().split()))
b = list(map(int, input().split()))
c = list(map(int, input().split()))

ab = []
for i in range(x):
    for j in range(y):
        ab.append(a[i] + b[j])

ab.sort(reverse=True)
c.sort(reverse=True)

abc = []
for i in range(min(k, x * y)):
    for j in range(z):
        abc.append(ab[i] + c[j])

abc.sort(reverse=True)
for i in range(k):
    print(abc[i])

import heapq

x, y, z, k = map(int, input().split())
a = list(map(int, input().split()))
b = list(map(int, input().split()))
c = list(map(int, input().split()))
a.sort(reverse=True)
b.sort(reverse=True)
c.sort(reverse=True)

cnt = 0
q = []
arg = (0, 0, 0)
visited = {}
visited[arg] = True
heapq.heappush(q, (-(a[arg[0]] + b[arg[1]] + c[arg[2]]),) + arg)

while cnt < k:
    score, ia, ib, ic = heapq.heappop(q)
    print(-score)
    cnt += 1
    arg_a = (ia + 1, ib, ic)
    arg_b = (ia, ib + 1, ic)
    arg_c = (ia, ib, ic + 1)
    if arg_a[0] <= x - 1 and arg_a not in visited:
        heapq.heappush(q, (-(a[arg_a[0]] + b[arg_a[1]] + c[arg_a[2]]),) + arg_a)
        visited[arg_a] = True
    if arg_b[1] <= y - 1 and arg_b not in visited:
        heapq.heappush(q, (-(a[arg_b[0]] + b[arg_b[1]] + c[arg_b[2]]),) + arg_b)
        visited[arg_b] = True
    if arg_c[2] <= z - 1 and arg_c not in visited:
        heapq.heappush(q, (-(a[arg_c[0]] + b[arg_c[1]] + c[arg_c[2]]),) + arg_c)
        visited[arg_c] = True


x, y, z, k = map(int, input().split())
a_array = sorted([int(x) for x in input().split()], reverse=True)
b_array = sorted([int(x) for x in input().split()], reverse=True)
c_array = sorted([int(x) for x in input().split()], reverse=True)

def solver1():
    ab_array = [x + y for x in a_array for y in b_array]
    abc_array = sorted([ab + c for ab in ab_array for c in c_array], reverse=True)
    for abc in abc_array[:k]:
        print(abc)

solver1()

x, y, z, k = map(int, input().split())
a_array = sorted([int(x) for x in input().split()], reverse=True)
b_array = sorted([int(x) for x in input().split()], reverse=True)
c_array = sorted([int(x) for x in input().split()], reverse=True)

def solver1():
    ab_array = sorted([x + y for x in a_array for y in b_array], reverse=True)[:3000]
    abc_array = sorted([ab + c for ab in ab_array for c in c_array], reverse=True)
    for abc in abc_array[:k]:
        print(abc)

solver1()


from heapq import heappush, heappushpop

def main():
    x, y, z, k = map(int, input().split())
    aa = sorted(map(int, input().split()), reverse=True)
    bb = sorted(map(int, input().split()), reverse=True)
    cc = sorted(map(int, input().split()), reverse=True)

    heap = []
    for a in aa:
        if len(heap) == k and a + bb[0] + cc[0] < heap[0]:
            break
        for b in bb:
            if len(heap) == k and a + b + cc[0] < heap[0]:
                break
            for c in cc:
                v = a + b + c
                if len(heap) < k:
                    heappush(heap, v)
                    continue
                if v < heap[0]:
                    break
                heappushpop(heap, v)

    for v in sorted(heap, reverse=True):
        print(v)

if __name__ == "__main__":
    main()

from heapq import heappop, heappush

def main():
    x, y, z, k = map(int, input().split())
    aa = sorted(map(int, input().split()))
    bb = sorted(map(int, input().split()))
    cc = sorted(map(int, input().split()))
    v = aa[-1] + bb[-1] + cc[-1]
    t = (len(aa) - 1, len(bb) - 1, len(cc) - 1)
    q = [(-v, t)]
    vs = []
    ts = set()
    while len(vs) < k:
        nv, t = heappop(q)
        if t in ts:
            continue
        vs.append(-nv)
        ts.add(t)
        if t[0] > 0:
            heappush(q, (-(aa[t[0] - 1] + bb[t[1]] + cc[t[2]]), (t[0] - 1, t[1], t[2])))
        if t[1] > 0:
            heappush(q, (-(aa[t[0]] + bb[t[1] - 1] + cc[t[2]]), (t[0], t[1] - 1, t[2])))
        if t[2] > 0:
            heappush(q, (-(aa[t[0]] + bb[t[1]] + cc[t[2] - 1]), (t[0], t[1], t[2] - 1)))
    for v in vs:
        print(v)

if __name__ == "__main__":
    main()


x, y, z, k = map(int, input().split())
A = sorted(list(map(int, input().split())), reverse=True)
B = sorted(list(map(int, input().split())), reverse=True)
C = sorted(list(map(int, input().split())), reverse=True)

AB = []
for a in A:
    for b in B:
        AB.append(a + b)

AB.sort(reverse=True)

ABC = []
for i in range(min(a * b, k)):
    for c in C:
        ABC.append(AB[i] + c)

ABC.sort(reverse=True)

for i in range(k):
    print(ABC[i])

x, y, z, k = map(int, input().split())
A = sorted(list(map(int, input().split())), reverse=True)
B = sorted(list(map(int, input().split())), reverse=True)
C = sorted(list(map(int, input().split())), reverse=True)

AB = []
for a in A:
    for b in B:
        AB.append(a + b)

AB.sort(reverse=True)

ABC = []
for i in range(min(x * y, k)):
    for c in C:
        ABC.append(AB[i] + c)

ABC.sort(reverse=True)

for i in range(k):
    print(ABC[i])


X, Y, Z, K = [int(i) for i in input().split()]
ABCList = [sorted([int(i) for i in input().split()], reverse=True) for _ in range(3)]

ABSum = sorted([i + j for i in ABCList[0] for j in ABCList[1]], reverse=True)[:K]
for ans in sorted([i + j for i in ABSum for j in ABCList[2]], reverse=True)[:K]:
    print(ans)

X, Y, Z, K = [int(i) for i in input().split()]
ABCList = [sorted([int(i) for i in input().split()], reverse=True) for _ in range(3)]

ansList = []
for i, A in enumerate(ABCList[0]):
    for j, B in enumerate(ABCList[1]):
        for k, C in enumerate(ABCList[2]):
            if (i + 1) * (j + 1) * (k + 1) <= K:
                ansList.append(A + B + C)
            else:
                break

for ans in sorted(ansList, reverse=True)[:K]:
    print(ans)


X, Y, Z, K = map(int, input().split())

A = sorted(map(int, input().split()), reverse=True)
B = sorted(map(int, input().split()), reverse=True)
C = sorted(map(int, input().split()), reverse=True)

ABC = []
for a in range(X):
    for b in range(Y):
        if (a + 1) + (b + 1) > K:
            break
        for c in range(Z):
            if (a + 1) + (b + 1) * (c + 1) > K:
                break
            ABC.append(A[a] + B[b] + C[c])

ABC.sort(reverse=True)
for i in range(K):
    print(ABC[i])

X, Y, Z, K = map(int, input().split())

A = sorted(map(int, input().split()), reverse=True)
B = sorted(map(int, input().split()), reverse=True)
C = sorted(map(int, input().split()), reverse=True)

ABC = []
for a in range(X):
    for b in range(Y):
        if (a + 1) * (b + 1) > K:
            break
        for c in range(Z):
            if (a + 1) * (b + 1) * (c + 1) > K:
                break
            ABC.append(A[a] + B[b] + C[c])

ABC.sort(reverse=True)
for i in range(K):
    print(ABC[i])


x, y, z, k = map(int, input().split())
A = list(map(int, input().split()))
B = list(map(int, input().split()))
C = list(map(int, input().split()))
goukei = []
for i in range(x):
    for j in range(y):
        goukei.append(A[i] + B[j])
goukei.sort()
goukei2 = goukei[-k:]
goukei3 = []
for c in C:
    for ab in goukei2:
        goukei3.append(ab + c)
goukei3.sort(reverse=True)
print(*goukei3[:k], sep="\\n")

x, y, z, k = map(int, input().split())
A = list(map(int, input().split()))
B = list(map(int, input().split()))
C = list(map(int, input().split()))
goukei = []
for a in A:
    for b in B:
        goukei.append(a + b)
goukei.sort()
goukei2 = goukei[-k:]
goukei3 = []
for c in C:
    for ab in goukei2:
        goukei3.append(ab + c)
goukei3.sort(reverse=True)
print(*goukei3[:k], sep="\\n")


import sys
import heapq

input = sys.stdin.readline

def main():
    X, Y, Z, K = map(int, input().split())
    A = list(map(lambda x: -int(x), input().split()))
    B = list(map(lambda x: -int(x), input().split()))
    C = list(map(lambda x: -int(x), input().split()))

    A.sort()
    B.sort()
    C.sort()

    p_queue = [(A[0] + B[0] + C[0], 0, 0, 0)]
    appear = [[[False for _ in range(Z)] for _ in range(Y)] for _ in range(X)]
    appear[0][0][0] = True
    ans = []
    while len(ans) < K:
        s, i_A, i_B, i_C = heapq.heappop(p_queue)
        ans.append(-s)
        if i_A < X - 1 and not appear[i_A + 1][i_B][i_C]:
            appear[i_A + 1][i_B][i_C] = True
            t = s - A[i_A] + A[i_A + 1]
            heapq.heappush(p_queue, (t, i_A + 1, i_B, i_C))
        if i_B < Y - 1 and not appear[i_A][i_B + 1][i_C]:
            appear[i_A][i_B + 1][i_C] = True
            t = s - B[i_B] + B[i_B + 1]
            heapq.heappush(p_queue, (t, i_A, i_B + 1, i_C))
        if i_C < Z - 1 and not appear[i_A][i_B][i_C + 1]:
            appear[i_A][i_B][i_C + 1] = True
            t = s - C[i_C] + C[i_C + 1]
            heapq.heappush(p_queue, (t, i_A, i_B, i_C + 1))

    print("\\n".join(map(str, ans)))

if __name__ == "__main__":
    main()

import sys
import heapq

input = sys.stdin.readline

def main():
    X, Y, Z, K = map(int, input().split())
    A = list(map(lambda x: -int(x), input().split()))
    B = list(map(lambda x: -int(x), input().split()))
    C = list(map(lambda x: -int(x), input().split()))

    A.sort()
    B.sort()
    C.sort()

    p_queue = [(A[0] + B[0] + C[0], 0, 0, 0)]
    visited = set()
    visited.add((0, 0, 0))
    ans = []
    while len(ans) < K:
        s, i_A, i_B, i_C = heapq.heappop(p_queue)
        ans.append(-s)
        if i_A < X - 1 and (i_A + 1, i_B, i_C) not in visited:
            visited.add((i_A + 1, i_B, i_C))
            t = s - A[i_A] + A[i_A + 1]
            heapq.heappush(p_queue, (t, i_A + 1, i_B, i_C))
        if i_B < Y - 1 and (i_A, i_B + 1, i_C) not in visited:
            visited.add((i_A, i_B + 1, i_C))
            t = s - B[i_B] + B[i_B + 1]
            heapq.heappush(p_queue, (t, i_A, i_B + 1, i_C))
        if i_C < Z - 1 and (i_A, i_B, i_C + 1) not in visited:
            visited.add((i_A, i_B, i_C + 1))
            t = s - C[i_C] + C[i_C + 1]
            heapq.heappush(p_queue, (t, i_A, i_B, i_C + 1))

    print("\\n".join(map(str, ans)))

if __name__ == "__main__":
    main()


a, b, c = input()
if a == b == c:
    print("Yes")
else:
    print("No")

a, b, c = input().split()
if a == b == c:
    print("Yes")
else:
    print("No")

Traceback (most recent call last):
  File "/home/alex/Documents/bug-detection/input/Project_CodeNet/data/p03079/Python/s108336051.py", line 1, in <module>
    a, b, c = input()
ValueError: too many values to unpack (expected 3)


N, Q = map(int, input().split())
s = input()

Query = [input().split() for i in range(Q)]

left, right = 0, N - 1

for t, d in reversed(Query):
    if d == "L":
        if t == s[left]:
            left += 1
        if right < N - 1 and t == s[right + 1]:
            right = min(right + 1, N - 1)
    else:
        if left > 0 and t == s[left - 1]:
            left = max(left - 1, 0)
        if t == s[right]:
            right -= 1
print(max(right - left + 1, 0))

# N, Q = map(int, input().split())
import sys

input = sys.stdin.readline

N, Q = tuple(map(int, input().split()))
s = input()

Query = [input().split() for i in range(Q)]

left, right = 0, N - 1

for t, d in reversed(Query):
    if d == "L":
        if t == s[left]:
            left += 1
        if right < N - 1 and t == s[right + 1]:
            right = min(right + 1, N - 1)
    else:
        if left > 0 and t == s[left - 1]:
            left = max(left - 1, 0)
        if t == s[right]:
            right -= 1
print(max(right - left + 1, 0))


import logging
import sys

logger = logging.getLogger(__name__)
logger.setLevel(logging.INFO)
h = logging.FileHandler("logtest.log")
logger.addHandler(h)

input = sys.stdin.readline

N, Q = map(int, input().split())
logger.info("N: {}, Q: {}".format(N, Q))

s = input()
logger.info("s: {}".format(s))

Query = [input().split() for i in range(Q)]

left, right = 0, N - 1

# 二分探索: boundaryを求める
for t, d in reversed(Query):
    logger.info("t: {}, d: {}".format(t, d))
    logger.info("[before] left: {}, right: {}".format(left, right))
    if d == "L":
        if t == s[left]:
            left += 1
        if right < N - 1 and t == s[right + 1]:
            right = min(right + 1, N - 1)
    else:
        if left > 0 and t == s[left - 1]:
            left = max(left - 1, 0)
        if t == s[right]:
            right -= 1
    logger.info("[after] left: {}, right: {}\\n".format(left, right))
print(max(right - left + 1, 0))

import sys

input = sys.stdin.readline

N, Q = map(int, input().split())
s = input()

Query = [input().split() for i in range(Q)]

left, right = 0, N - 1

for t, d in reversed(Query):
    if d == "L":
        if t == s[left]:
            left += 1
        if right < N - 1 and t == s[right + 1]:
            right = min(right + 1, N - 1)
    else:
        if left > 0 and t == s[left - 1]:
            left = max(left - 1, 0)
        if t == s[right]:
            right -= 1
print(max(right - left + 1, 0))


import sys

N, Q = map(int, sys.stdin.readline().split())
s = input()

Query = [input().split() for i in range(Q)]

left, right = 0, N - 1

for t, d in reversed(Query):
    if d == "L":
        if t == s[left]:
            left += 1
        if right < N - 1 and t == s[right + 1]:
            right = min(right + 1, N - 1)
    else:
        if left > 0 and t == s[left - 1]:
            left = max(left - 1, 0)
        if t == s[right]:
            right -= 1
print(max(right - left + 1, 0))

import sys

input = sys.stdin.readline
N, Q = map(int, input().split())

s = input()

input = sys.stdin.readline
Query = [input().split() for i in range(Q)]

left, right = 0, N - 1

for t, d in reversed(Query):
    if d == "L":
        if t == s[left]:
            left += 1
        if right < N - 1 and t == s[right + 1]:
            right = min(right + 1, N - 1)
    else:
        if left > 0 and t == s[left - 1]:
            left = max(left - 1, 0)
        if t == s[right]:
            right -= 1
print(max(right - left + 1, 0))


Traceback (most recent call last):
  File "/home/alex/Documents/bug-detection/input/Project_CodeNet/data/p03085/Python/s332767566.py", line 1, in <module>
    input("ATCG"["TAGC".find(input())])
EOFError: EOF when reading a line


b = int(input())
if b == "A":
    print("T")
elif b == "C":
    print("G")
elif b == "T":
    print("A")
else:
    print("C")

b = input()
if b == "A":
    print("T")
elif b == "C":
    print("G")
elif b == "T":
    print("A")
else:
    print("C")

Traceback (most recent call last):
  File "/home/alex/Documents/bug-detection/input/Project_CodeNet/data/p03085/Python/s167969735.py", line 1, in <module>
    b = int(input())
ValueError: invalid literal for int() with base 10: 'A'


s = input()
ans = 0
li = ["A", "C", "G", "T"]
count = 0
for i in s:
    if i in li:
        count += 1
    else:
        if count > ans:
            ans = count
        count = 0
if count == len(ans):
    print(count)
else:
    print(ans)

s = input()
ans = 0
li = ["A", "C", "G", "T"]
count = 0
for i in s:
    if i in li:
        count += 1
    else:
        if count > ans:
            ans = count
        count = 0

if count > ans:
    ans = count
print(ans)

Traceback (most recent call last):
  File "/home/alex/Documents/bug-detection/input/Project_CodeNet/data/p03086/Python/s143153057.py", line 12, in <module>
    if count == len(ans):
TypeError: object of type 'int' has no len()


S = input()
L = ["A", "C", "G", "T"]

arr = []
st = ""

for s in S:
    if st and s not in L:
        arr.append(st)
        st = ""
    if s in L:
        st += s

print(max(map(len, arr)))

S = input()
L = ["A", "C", "G", "T"]

arr = []
st = ""

for s in S:
    if st and s not in L:
        st = ""
    if s in L:
        st += s
        arr.append(st)

print(max(map(len, arr)) if arr else 0)


import re

s = input()
pat = r"[ACGT]+"
atgc = re.findall(pat, s)
ress = []
for i in atgc:
    ress.append(len(i))

print(max(ress))

import re

s = input()
pat = r"[ACGT]+"
atgc = re.findall(pat, s)
ress = []
for i in atgc:
    ress.append(len(i))
if ress == [""] or ress == []:
    print(0)
else:
    print(max(ress))


S = input()
AA = ["A", "C", "G", "T"]

ans = []
ll = 0
for i in range(len(S)):
    if S[i] in AA:
        ll += 1
    else:
        ans.append(ll)
        ll = 0

print(max(ans))

S = input()
AA = ["A", "C", "G", "T"]

ans = []
ll = 0
for i in range(len(S)):
    if S[i] in AA:
        ll += 1
    else:
        ans.append(ll)
        ll = 0
ans.append(ll)

print(max(ans))


b = input()
ans = 0
ls = []
for i in range(len(b)):
    if b[i] == "A" or b[i] == "T" or b[i] == "C" or b[i] == "G":
        ans += 1
    else:
        ls.append(ans)
        ans = 0
print(max(ls))

b = input()
ans = ""

for i in range(len(b)):
    if b[i] == "A" or b[i] == "T" or b[i] == "C" or b[i] == "G":
        ans += "1"
    else:
        ans += "0"
# print(ans)

ls = ans.split("0")
# print(ls)
print(len(max(ls)))


string = input()

char = ["A", "C", "G", "T"]

count = 0
lengths = []
for s in string:
    if s in char:
        count += 1
    else:
        lengths.append(count)
        count = 0
else:
    print(max(lengths))

s = input()
n = len(s)

ans = 0
for i in range(n):
    for j in range(i, n):
        if all("ACGT".count(l) for l in s[i : j + 1]):
            ans = max(ans, j - i + 1)

print(ans)


s = input()
ans = []
acgt = ["A", "C", "G", "T"]

cnt = 0
for i in range(len(s)):
    if s[i] in acgt:
        cnt += 1
    else:
        ans.append(cnt)
        cnt = 0
print(max(ans))

s = input()
ans = []
acgt = ["A", "C", "G", "T"]

cnt = 0
for i in range(len(s)):
    if s[i] in acgt:
        cnt += 1
    else:
        ans.append(cnt)
        cnt = 0
ans.append(cnt)
print(max(ans))


s = input()

s.append("X")
pat = ["A", "C", "G", "T"]
mx = 0
count = 0

for i in range(len(s)):
    if s[i] in pat:
        count += 1
    else:
        if count > mx:
            mx = count
        count = 0

print(mx)

s = input()

s += "X"
pat = ["A", "C", "G", "T"]
mx = 0
count = 0

for i in range(len(s)):
    if s[i] in pat:
        count += 1
    else:
        if count > mx:
            mx = count
        count = 0

print(mx)

Traceback (most recent call last):
  File "/home/alex/Documents/bug-detection/input/Project_CodeNet/data/p03086/Python/s975280686.py", line 3, in <module>
    s.append("X")
AttributeError: 'str' object has no attribute 'append'


s = input()
ans = 0
ans_l = []

if len(s) == 1:
    print(1)
    quit()
for i in range(len(s)):
    if s[i] == "A" or s[i] == "C" or s[i] == "G" or s[i] == "T":
        ans += 1
    else:
        ans_l.append(ans)
        ans = 0
print(max(ans_l))

s = input()
ans = 0
ans_l = [0] * len(s)

for i in range(len(s)):
    if s[i] == "A" or s[i] == "C" or s[i] == "G" or s[i] == "T":
        ans_l[ans] += 1
    else:
        ans += 1
print(max(ans_l))


N = input()
answer_list = []
answer = 0

for i in range(len(N)):
    if N[i] == "A" or N[i] == "T" or N[i] == "C" or N[i] == "G":
        answer += 1
    else:
        answer_list.append(answer)
        answer = 0
print(max(answer_list))

N = input()
answer_list = []
answer = 0

for i in range(len(N)):
    if N[i] == "A" or N[i] == "T" or N[i] == "C" or N[i] == "G":
        answer += 1
    else:
        answer_list.append(answer)
        answer = 0
    if i + 1 == len(N):
        answer_list.append(answer)
print(max(answer_list))


pat = ["A", "C", "G", "T"]

s = input()

left = right = 0
ans = 0

while left < len(s):
    right = left
    while s[right] in pat:
        right += 1

    ans = max(ans, right - left)

    left = right + 1

print(ans)

pat = ["A", "C", "G", "T"]

s = input()

left = right = 0
ans = 0

while left < len(s):
    right = left
    while right < len(s) and s[right] in pat:
        right += 1

    ans = max(ans, right - left)

    left = right + 1

print(ans)


import numpy as np

n, q = map(int, input().split())
s = input()
l_ = np.zeros(q)
r_ = np.zeros(q)

for i in range(q):
    l_tmp, r_tmp = map(int, input().split())
    l_[i] = l_tmp
    r_[i] = r_tmp
l_ = l_ - 1
r_ = r_ - 1
for i in range(q):
    cnt = 0
    t = s[int(l_[i]) : int(r_[i]) + 1]
    for j in range(len(t) - 1):
        if t[j : j + 2] == "AC":
            cnt += 1
    print(cnt)

n, q = map(int, input().split())
s = input()
t = [0] * (n + 1)
for i in range(n):
    if s[i : i + 2] == "AC":
        t[i + 1] = t[i] + 1
    else:
        t[i + 1] = t[i]

for i in range(q):
    l, r = map(int, input().split())
    print(t[r - 1] - t[l - 1])


N, Q = map(int, input().split())
S = input()
lr = [None] * Q
for _ in range(Q):
    lr[_] = list(map(int, input().split()))
for _ in range(Q):
    ans = S.count("AC", lr[_][0] - 1, lr[_][1])
    print(ans)

N, Q = map(int, input().split())
S = input()
L = len(S)
ACnum = [0] * L
for i in range(1, L):
    ACnum[i] += ACnum[i - 1]
    if S[i - 1] == "A" and S[i] == "C":
        ACnum[i] += 1

lr = [None] * Q
for _ in range(Q):
    lr[_] = list(map(int, input().split()))

for i in range(Q):
    ans = ACnum[lr[i][1] - 1] - ACnum[lr[i][0] - 1]
    print(ans)


import re

N, Q = [int(i) for i in input().split()]
S = input()
ans = []
for i in range(Q):
    l, r = [int(i) for i in input().split()]
    l -= 1
    s = S[l:r]
    ret = re.findall(r"AC", s)
    ans.append(ret)
for a in ans:
    print(len(a))

N, Q = [int(i) for i in input().split()]
S = input()
t = [0] * (N + 1)
for i in range(N):
    end = i + 2
    t[i + 1] = t[i] + (1 if S[i:end] == "AC" else 0)
for i in range(Q):
    l, r = [int(i) for i in input().split()]
    print(t[r - 1] - t[l - 1])


import re

n, q = map(int, input().split())
s = input()

ans = [0] * q

for i in range(q):
    l, r = map(int, input().split())
    sl = s[l - 1 : r]
    # sl = sl.replace("AC", "-")
    # ans = sl.count("-")
    # print(ans)

    result = re.findall(r"AC", sl)
    # print(len(result))
    ans[i] = len(result)
print(*ans, sep="\\n")

n, q = map(int, input().split())
s = input()

t = [0] * (n + 1)
for i in range(n):
    t[i + 1] = t[i] + (1 if s[i : i + 2] == "AC" else 0)
# print(t)
for j in range(q):
    l, r = map(int, input().split())
    print(t[r - 1] - t[l - 1])


# 累積和
n, q = map(int, input().split())
s = input()
arr = [0] * (2 * 10**5 + 10)
idx = 1
while idx <= len(s):
    if s[idx - 1] == "A" and s[idx] == "C":
        arr[idx] += 1
    idx += 1
for i in range(1, len(arr)):
    arr[i] += arr[i - 1]
for _ in range(q):
    l, r = map(int, input().split())
    print(arr[r - 1] - arr[l - 1])

# 累積和
n, q = map(int, input().split())
s = input()
arr = [0] * (2 * 10**5 + 10)
idx = 1
while idx < len(s):
    if s[idx - 1] == "A" and s[idx] == "C":
        arr[idx] += 1
    idx += 1
for i in range(1, len(arr)):
    arr[i] += arr[i - 1]
for _ in range(q):
    l, r = map(int, input().split())
    print(arr[r - 1] - arr[l - 1])


import copy

def insertion(n, x):
    if not x:
        return True
    for i in range(n - 1, -1, -1):
        y = copy.copy(x)
        if y[i] == i + 1:
            y.pop(i)
            ope.insert(0, i + 1)
            if insertion(n - 1, y):
                return True
            else:
                ope.pop(0)
    return False

n = int(input())
b = list(map(int, input().split()))
ope = []
if insertion(n, b):
    for i in ope:
        print(i)
else:
    print(-1)

import sys

n = int(input())
b = list(map(int, input().split()))
ope = []
i = n
while i > 0:
    flag = 0
    j = i
    while j > 0:
        if b[j - 1] == j:
            b.pop(j - 1)
            i -= 1
            ope.insert(0, j)
            flag = 1
            break
        j -= 1
    if flag == 0:
        print(-1)
        sys.exit()
for i in ope:
    print(i)


N, M = map(int, input().split())
c = 0
d = 0
for _ in range(100):
    a, b = map(int, input().split())
print("No")

Traceback (most recent call last):
  File "/home/alex/Documents/bug-detection/input/Project_CodeNet/data/p03091/Python/s054793607.py", line 5, in <module>
    a, b = map(int, input().split())
EOFError: EOF when reading a line


from bisect import bisect

n, a, b = map(int, input().split())
ppp = map(int, input().split())
qqq = [0] * n
for i, p in enumerate(ppp, start=1):
    qqq[p - 1] = i

dp = [(0, 0)]
for i in qqq:
    s = bisect(dp, (i,))
    ndp = [(j, cost + b) for j, cost in dp[:s]]
    stay_cost = dp[s - 1][1]
    ndp.append((i, stay_cost))
    ndp.extend((j, cost + a) for j, cost in dp[s:] if stay_cost > cost + a)
    dp = ndp
print(dp[-1][1])

from bisect import bisect

n, a, b = map(int, input().split())
ppp = map(int, input().split())
qqq = [0] * n
for i, p in enumerate(ppp, start=1):
    qqq[p - 1] = i

dp = [(0, 0)]
for i in qqq:
    s = bisect(dp, (i,))
    ndp = [(j, cost + b) for j, cost in dp[:s]]
    stay_cost = dp[s - 1][1]
    ndp.append((i, stay_cost))
    remain = iter(dp[s:])
    for j, cost in remain:
        if stay_cost > cost + a:
            ndp.append((j, cost + a))
            break
    ndp.extend((j, cost + a) for j, cost in remain)
    dp = ndp
print(dp[-1][1])


from itertools import combinations

MOD = 1000000007

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

chars = set()
cnt = {}
for s in S:
    if s not in chars:
        chars.add(s)
        cnt[s] = 1
    else:
        cnt[s] += 1

ans = N % MOD
r = 2
chars = list(chars)
while r <= len(chars):
    combs = combinations(chars, r)
    for comb in combs:
        res = 1
        for c in comb:
            res *= cnt[c]
            res %= MOD
        ans += res
        ans %= MOD
    r += 1

print(ans)

MOD = 1000000007

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

cnt = {}
for s in S:
    if s not in cnt:
        cnt[s] = 1
    else:
        cnt[s] += 1

ans = 1
for v in cnt.values():
    ans *= v + 1
    ans %= MOD

print(ans - 1)


# 2019/03/16
# AtCoder Grand Contest 031 - A

# Input
n = int(input())
s = input()

aprcnt = [1] * 26

# Count up Alphabets
for idx in range(n):
    w_idx = ord(s[idx]) - 97
    aprcnt[w_idx] += 1

# Calc Combination count
ans = 1
for idx in range(n):
    ans = ans * aprcnt[idx]

# Reduces 1 in choose nothing case
ans -= 1

# Output
print(ans)

# 2019/03/17
# AtCoder Grand Contest 031 - A

# Input
n = int(input())
s = input()

aprcnt = [1] * 26

# Count up Alphabets
for idx in range(n):
    w_idx = ord(s[idx]) - 97
    aprcnt[w_idx] += 1

# Calc Combination count
ans = 1
for idx in range(26):
    ans = ans * aprcnt[idx]

# Reduces 1 in choose nothing case
ans -= 1
ans = ans % (10**9 + 7)

# Output
print(ans)


# input
n = int(input())

c = []
dp = [0] * (n + 1)
for i in range(n):
    stone = int(input())
    if len(c) == 0:
        dp[i + 1] = 1
    else:
        if stone in c and c[-1] is not stone:
            plc = len(c) - 1 - c[::-1].index(stone)
            dp[i + 1] = dp[i] + dp[plc + 1]
        else:
            dp[i + 1] = dp[i]
    c.append(stone)

print(dp[-1])

# input
n = int(input())

mod = pow(10, 9) + 7
c = []
dp = [0] * (n + 1)
stone_history = dict()
for i in range(n):
    stone = int(input())
    if len(c) == 0:
        dp[i + 1] = 1
    else:
        if stone in stone_history and c[-1] != stone:
            dp[i + 1] = dp[i] + stone_history[stone]
        else:
            dp[i + 1] = dp[i]

    stone_history[stone] = dp[i + 1]
    c.append(stone)

print(dp[-1] % mod)


H, W = int(input().split())
h, w = int(input().split())

print((H - h) * (W - w))
H, W = map(int, input().split())
h, w = map(int, input().split())

print((H - h) * (W - w))

Traceback (most recent call last):
  File "/home/alex/Documents/bug-detection/input/Project_CodeNet/data/p03101/Python/s085592284.py", line 1, in <module>
    H, W = int(input().split())
TypeError: int() argument must be a string, a bytes-like object or a real number, not 'list'


in1 = input().split()
in2 = input().split()

H = in1[0]
W = in1[1]

h = in2[0]
w = in2[1]

print((H - h) * (W - w))

in1 = input().split()
in2 = input().split()

H = int(in1[0])
W = int(in1[1])

h = int(in2[0])
w = int(in2[1])

print((H - h) * (W - w))

Traceback (most recent call last):
  File "/home/alex/Documents/bug-detection/input/Project_CodeNet/data/p03101/Python/s646870351.py", line 10, in <module>
    print((H - h) * (W - w))
TypeError: unsupported operand type(s) for -: 'str' and 'str'


H, W, h, w = map(int, input().split())
print((H - h) * (W - w))

H, W = map(int, input().split())
h, w = map(int, input().split())
print((H - h) * (W - w))

Traceback (most recent call last):
  File "/home/alex/Documents/bug-detection/input/Project_CodeNet/data/p03101/Python/s695121527.py", line 1, in <module>
    H, W, h, w = map(int, input().split())
ValueError: not enough values to unpack (expected 4, got 2)


lh, lw = input().split()
h, w = input().split()

print(lh * lw - lh * w - lw * h + h + w)

lh, lw = map(int, input().split())
h, w = map(int, input().split())

print(lh * lw - lh * w - lw * h + h * w)

Traceback (most recent call last):
  File "/home/alex/Documents/bug-detection/input/Project_CodeNet/data/p03101/Python/s628387371.py", line 4, in <module>
    print(lh * lw - lh * w - lw * h + h + w)
TypeError: can't multiply sequence by non-int of type 'str'


from sys import stdin

def na():
    return map(int, stdin().split())

n, _, c = na()
bs = na()

cnt = 0
for _ in range(n):
    if sum(x * y for x, y in zip(na(), bs)) > -c:
        cnt += 1

print(cnt)

from sys import stdin

def na():
    return map(int, stdin.readline().split())

n, _, c = na()
bs = tuple(na())

cnt = 0
for _ in range(n):
    if sum(x * y for x, y in zip(na(), bs)) > -c:
        cnt += 1

print(cnt)

Traceback (most recent call last):
  File "/home/alex/Documents/bug-detection/input/Project_CodeNet/data/p03102/Python/s048620808.py", line 8, in <module>
    n, _, c = na()
  File "/home/alex/Documents/bug-detection/input/Project_CodeNet/data/p03102/Python/s048620808.py", line 5, in na
    return map(int, stdin().split())
TypeError: '_io.TextIOWrapper' object is not callable


n, m, c = map(int, input().split())
b = [int(i) for i in input().split()]
a = [[int(input()) for _ in range(m)] for _ in range(n)]
ans = 0
ctn = 0
for j in range(n):
    for k in range(m):
        ctn += b[k] * a[j][k]
    if ctn + c > 0:
        ans += 1
    ctn = 0
print(ans)

n, m, c = map(int, input().split())
b = [int(i) for i in input().split()]
a = [[int(j) for j in input().split()] for _ in range(n)]
ans = 0
for k in range(n):
    ct = c
    for l in range(m):
        ct += a[k][l] * b[l]
    if ct > 0:
        ans += 1
    ct = 0
print(ans)

Traceback (most recent call last):
  File "/home/alex/Documents/bug-detection/input/Project_CodeNet/data/p03102/Python/s069245551.py", line 3, in <module>
    a = [[int(input()) for _ in range(m)] for _ in range(n)]
  File "/home/alex/Documents/bug-detection/input/Project_CodeNet/data/p03102/Python/s069245551.py", line 3, in <listcomp>
    a = [[int(input()) for _ in range(m)] for _ in range(n)]
  File "/home/alex/Documents/bug-detection/input/Project_CodeNet/data/p03102/Python/s069245551.py", line 3, in <listcomp>
    a = [[int(input()) for _ in range(m)] for _ in range(n)]
ValueError: invalid literal for int() with base 10: '3 2 1'


# -*- coding: utf-8 -*-

import numpy as np

N, M, C = map(int, input().split())
B = np.ndarray([M, 1])
B = list(map(int, input().split()))
A = np.ndarray([N, M])
for i in range(N):
    A[i, :] = list(map(int, input().split()))

ans = sum((A @ B + C) > 0)

print(ans)

# -*- coding: utf-8 -*-

import numpy as np

N, M, C = map(int, input().split())
B = np.ndarray([M, 1])
B = list(map(int, input().split()))
A = np.ndarray([N, M])
for i in range(N):
    A[i, :] = list(map(int, input().split()))

ans = sum((np.dot(A, B) + C) > 0)

print(ans)


n, m, c = map(int, input().split())
b = list(map(int, input().split()))
ans = 0
for _ in range(n):
    a = list(map(int, input().split()))
    if a[0] * b[0] + a[1] * b[1] + a[2] * b[2] + c > 0:
        ans += 1
print(ans)

n, m, c = map(int, input().split())
b = list(map(int, input().split()))
a = [list(map(int, input().split())) for _ in range(n)]
ans = 0
for ai in a:
    absum = 0
    for i in range(m):
        absum += ai[i] * b[i]
    if absum + c > 0:
        ans += 1
print(ans)


N, M, C = map(int, input().split())
B = [int(input()) for i in range(M)]
A = [[int(input()) for i in range(M)] for j in range(N)]
cnt = 0
for i in range(N):
    ans = 0
    for j in range(M):
        ans += A[j][i] * B[j]
    if ans + C > 0:
        cnt += 1
print(cnt)

N, M, C = map(int, input().split())
B = list(map(int, input().split()))
ans = 0

for i in range(N):
    A = list(map(int, input().split()))
    tmp = C
    for i, j in zip(A, B):
        tmp += i * j
    if tmp > 0:
        ans += 1
print(ans)

Traceback (most recent call last):
  File "/home/alex/Documents/bug-detection/input/Project_CodeNet/data/p03102/Python/s538264445.py", line 2, in <module>
    B = [int(input()) for i in range(M)]
  File "/home/alex/Documents/bug-detection/input/Project_CodeNet/data/p03102/Python/s538264445.py", line 2, in <listcomp>
    B = [int(input()) for i in range(M)]
ValueError: invalid literal for int() with base 10: '1 2 3'


N, M = map(int, input().split())
ab = [list(map(int, input().split())) for i in range(N)]

ab_hash = {}
count = 0
price_sum = 0
for x in ab:
    ab_hash[x[0]] = x[1]
ab_key_sorted = sorted(ab_hash.items(), key=lambda x: x[0])
while M > 0:
    if M > ab_key_sorted[count][1]:
        price_sum += ab_key_sorted[count][0] * ab_key_sorted[count][1]
    else:
        price_sum += ab_key_sorted[count][0] * M
    M = M - ab_key_sorted[count][1]
    count += 1

print(price_sum)

N, M = map(int, input().split())
ab = [list(map(int, input().split())) for i in range(N)]

ab_hash = {}
count = 0
price_sum = 0
for x in range(len(ab)):
    ab_hash[x] = [ab[x][0], ab[x][1]]
ab_key_sorted = sorted(ab_hash.items(), key=lambda x: x[1][0])

for y in ab_key_sorted:
    if M > y[1][1]:
        price_sum += y[1][0] * y[1][1]
    else:
        price_sum += y[1][0] * M
    M -= y[1][1]
    if M <= 0:
        break

print(price_sum)


n, m = map(int, input().split())
a = [list(map(int, input().split())) for _ in range(n)].sort()
pm = s = 0
m_minus_1 = m - 1
for item in a:
    pm += item[1]
    if pm > m_minus_1:
        print(s + item[0] * (m - pm + item[1]))
        break
    else:
        s += item[0] * item[1]

n, m = map(int, input().split())
a = [list(map(int, input().split())) for _ in range(n)]
a.sort()
pm = s = 0
m_minus_1 = m - 1
for item in a:
    pm += item[1]
    if pm > m_minus_1:
        print(s + item[0] * (m - pm + item[1]))
        break
    else:
        s += item[0] * item[1]

Traceback (most recent call last):
  File "/home/alex/Documents/bug-detection/input/Project_CodeNet/data/p03103/Python/s281800849.py", line 5, in <module>
    for item in a:
TypeError: 'NoneType' object is not iterable


n, m = map(int, input().split())
stores = [tuple(map(int, input().split())) for _ in range(n)]
dict_stores = dict(stores)
lower_cost = sorted(dict_stores.keys())
total = 0
ans = 0
for i in range(n):
    total += dict_stores[lower_cost[i]]
    if total >= m:
        diff = m - (total - dict_stores[lower_cost[i]])
        ans += lower_cost[i] * diff
        break
    else:
        ans += lower_cost[i] * dict_stores[lower_cost[i]]
print(ans)

n, m = map(int, input().split())
stores = [list(map(int, input().split())) for _ in range(n)]
sorted_sotres = sorted(stores, key=lambda x: x[0])
total = 0
ans = 0
for cost, number in sorted_sotres:
    if number > m:
        ans += cost * m
        break
    elif number == m:
        ans += cost * number
        break
    else:
        ans += cost * number
    m -= number
print(ans)


N, M = map(int, input().split())
d = dict([list(map(int, input().split()))[::-1] for i in range(N)])
d = sorted(d.items(), key=lambda x: x[1])
c = p = i = 0

while c < M:
    if c + d[i][0] <= M:
        c += d[i][0]
        p += d[i][1] * d[i][0]
        i += 1
    else:
        p += d[i][1] * (M - c)
        print(c)
        break

print(p)

N, M = map(int, input().split())
d = [list(map(int, input().split())) for i in range(N)]
d.sort(key=lambda x: (x[0], x[1]))

c = p = i = 0

while c < M:
    if c + d[i][1] <= M:
        c += d[i][1]
        p += d[i][0] * d[i][1]
        i += 1
    else:
        p += d[i][0] * (M - c)
        break

print(p)


def main():
    N, M = map(int, input().split())
    D = [tuple(map(int, input().split())) for _ in range(N)]
    D.sort()
    ans = 0
    while M > 0:
        a, b = D.pop(0)
        ans += a * min(M, b)
        M -= min(M, b)
    print(ans)

if __name__ == "__main__":
    main()

def main():
    N, M = map(int, input().split())
    D = [tuple(map(int, input().split())) for _ in range(N)]
    D.sort()
    ans = 0
    i = 0
    while M > 0:
        a, b = D[i]
        ans += a * min(M, b)
        M -= min(M, b)
        i += 1
    print(ans)

if __name__ == "__main__":
    main()


a, b, k = map(int, input().split())
m = 0
ans = []

if a < b:
    m = b
    n = a
else:
    m = a
    n = b

for i in range(1, m):
    if n % i == 0 and m % i == 0:
        ans.append(i)

if ans:
    print(ans[-k])
else:
    print("1")

a, b, k = map(int, input().split())
m = 0
ans = []

if a < b:
    m = b
    n = a
else:
    m = a
    n = b

for i in range(1, n + 1):
    if m % i == 0 and n % i == 0:
        ans.append(i)

if ans:
    print(ans[-k])
else:
    print("1")


A, B, K = map(int, input().split())

def gcd(a, b):
    x = max(a, b)
    y = min(a, b)
    mod = x % y
    if mod == 0:
        return y

    return gcd(y, mod)

mx = gcd(A, B)
count = 0
for a in range(mx, 1, -1):
    print(a)
    if A % a == 0 and B % a == 0:
        count += 1
    if count == K:
        ans = a
        break
print(ans)

A, B, K = map(int, input().split())

def gcd(a, b):
    x = max(a, b)
    y = min(a, b)
    mod = x % y
    if mod == 0:
        return y

    return gcd(y, mod)

mx = gcd(A, B)
count = 0

for a in range(mx, 0, -1):
    if A % a == 0 and B % a == 0:
        count += 1
    if count == K:
        ans = a
        break

print(ans)


A, B, K = map(int, input().split())
n = max(A, B)
k = 0
if n == 1:
    print("1")
else:
    while k < K:
        n = n - 1
        if A % n == 0 and B % n == 0:
            k = k + 1
    print(n)

A, B, K = map(int, input().split())
n = max(A, B)
k = 0
while True:
    if A % n == 0 and B % n == 0:
        k = k + 1
    if k >= K:
        break
    n = n - 1
print(n)


# coding:utf-8

def main():
    A, B, K = map(int, input().split())
    ans = []
    ans.append(1)
    for i in range(2, max(A, B)):
        if A % i == 0 and B % i == 0:
            ans.append(i)
        # if len(ans) >= K:
        #     break
    # print(ans)
    print(ans[-K])

if __name__ == "__main__":
    main()

# coding:utf-8

def main():
    A, B, K = map(int, input().split())
    ans = []
    ans.append(1)
    for i in range(2, max(A, B) + 1):
        if A % i == 0 and B % i == 0:
            ans.append(i)
        # if len(ans) >= K:
        #     break
    # print(ans)
    print(ans[-K])

if __name__ == "__main__":
    main()


a, b, k = map(int, input().split())
div = []
for i in range(1, max(a, b)):
    if (a % i == 0) and (b % i == 0):
        div.append(i)
if len(div) == 0:
    print(1)
else:
    div.sort()
    print(div[-k])

a, b, k = map(int, input().split())
div = []
for i in range(1, 105):
    if (a % i == 0) and (b % i == 0):
        div.append(i)

if len(div) < k:
    print(max(div))
else:
    div.sort()
    print(div[-k])


import math

A, B, K = map(int, input().split())

gcd = math.gcd(A, B)
order = 0
for i in range(gcd, 0, -1):
    if gcd % i == 0:
        order += 1
        if order == K:
            print(i)
            break

import fractions

A, B, K = map(int, input().split())

gcd = fractions.gcd(A, B)
order = 0
for i in range(gcd, 0, -1):
    if gcd % i == 0:
        order += 1
        if order == K:
            print(i)
            break


a, b, k = map(int, input().split())
divisers = []

for i in range(1, max(a, b)):
    if a % i == 0 and b % i == 0:
        divisers.append(i)

print(divisers[-k])

a, b, k = map(int, input().split())
divisers = []

for i in range(1, min(a, b) + 1):
    if a % i == 0 and b % i == 0:
        divisers.append(i)

print(divisers[-k])


a, b, k = map(int, input().split())

common_factors = []
for i in range(1, a):
    if a % i == 0 and b % i == 0:
        common_factors.append(i)
print(common_factors[-k])

a, b, k = map(int, input().split())

common_factors = []
for i in range(1, a + 1):
    if a % i == 0 and b % i == 0:
        common_factors.append(i)
print(common_factors[-k])


A, B, K = [int(i) for i in input().split()]
th = []
i = 1
while True:
    i += 1
    if (A % i, B % i) == (0, 0):
        th.append(i)
    if i > A or i > B:
        break
print(th[len(th) - K])

A, B, K = [int(i) for i in input().split()]
th = []
i = 0
while True:
    i += 1
    if (A % i, B % i) == (0, 0):
        th.append(i)
    if i > A or i > B:
        break
print(th[len(th) - K])


def recursin_operation(S, len_init_s):
    S = list(S)
    N = len(S)
    lst = [i for i in range(N - 1) if S[i] != S[i + 1]]
    nums_of_deleted = []
    if len(lst) != 0:
        for i in lst:
            S_next = [S[j] for j in range(len(S)) if j < i or j > i + 1]
            nums_of_deleted.append(recursin_operation(S_next, len_init_s))
    else:
        nums_of_deleted.append(len_init_s - len(S))

    return max(nums_of_deleted)

def main():
    S = input()
    len_init_s = len(S)
    ans = recursin_operation(S, len_init_s)
    print(ans)

if __name__ == "__main__":
    main()

S = input()
len_S = len(S)
zero_lst = [S[i] for i in range(len_S) if S[i] == "0"]
one_lst = [S[i] for i in range(len_S) if S[i] == "1"]
ans = min(len(zero_lst), len(one_lst)) * 2
print(ans)


# -*- coding: utf-8 -*-

import re

S = input()

s = S
ans = 0
while 1:
    s, t = re.subn("01|10", "", s)
    ans += t * 2
    if t == 0:
        break

print(ans)

# -*- coding: utf-8 -*-

S = input()

ans = min(S.count("0"), S.count("1"))
print(ans * 2)


s = input()
print(2 * min(s.count(0), s.count(1)))

s = input()
print(2 * min(s.count("0"), s.count("1")))

Traceback (most recent call last):
  File "/home/alex/Documents/bug-detection/input/Project_CodeNet/data/p03107/Python/s119508612.py", line 2, in <module>
    print(2 * min(s.count(0), s.count(1)))
TypeError: must be str, not int


# -*- coding: utf-8 -*-
s = input()
s = list(s)
x = 0
maxIndex = len(s) - 1
count = 0
while True:
    if len(s) == x + 1:
        break
    if s[x] != s[x + 1]:
        s.pop(x)
        s.pop(x)
        count += 1
        x -= 1
    else:
        x += 1
print("{}".format(count * 2))

s = input()
x = 0
for c in s:
    if c == "1":
        x = x + 1
print(min(x, len(s) - x) * 2)


import re

s = input()
pattern = "(01)|(10)"
count = 0
diff = 1

while diff:
    ssub = re.sub(pattern, "", s)
    diff = len(s) - len(ssub)
    count += diff
    s = ssub

print(count)

import re

s = input()
zero = re.findall("0", s)
one = re.findall("1", s)
print(2 * min(len(zero), len(one)))


S = input()
subS = S
cnt = 0
while True:
    flg = True
    if "01" in subS:
        flg = False
        subS = subS.replace("01", "", 1)
        cnt += 2
    if "10" in subS:
        flg = False
        subS = subS.replace("10", "", 1)
        cnt += 2
    if flg:
        break
print(cnt)

S = input()

zeroSum = 0
oneSum = 0
for _ in S:
    if _ == "0":
        zeroSum += 1
    if _ == "1":
        oneSum += 1

t = max(zeroSum, oneSum) - min(zeroSum, oneSum)
print(len(S) - t)


#!/usr/bin/env python3
s = input()
n = len(s)
while True:
    ns = s.replace("01", "").replace("10", "")
    if ns == s:
        break
    s = ns
print(n - len(s))

#!/usr/bin/env python3
s = input()
n = len(s)
c = 0
p = ""
count = 0
for i in s:
    if i == p:
        count += 1
    else:
        c = count - c
        count = 1
    p = i
c = count - c
print(n - abs(c))


S = input()
S = [Si for Si in S]

i = len(S) - 2
number_of_deleted_qube = 0
while i >= 0 and len(S) >= 2:
    current = S[i]
    right = S[i + 1]

    if current != right:
        del S[i + 1]
        del S[i]
        number_of_deleted_qube += 2

    i -= 1

print(number_of_deleted_qube)

S = input()
S = [Si for Si in S]

i = len(S) - 2
number_of_deleted_qube = 0
while i >= 0 and len(S) >= 2:
    if i >= len(S) - 1:
        i -= 1
        continue

    current = S[i]
    right = S[i + 1]

    if current != right:
        del S[i + 1]
        del S[i]
        number_of_deleted_qube += 2

    i -= 1

print(number_of_deleted_qube)


S = input()
init_len = len(S)

while True:
    cur_len = len(S)
    S = S.replace("01", "")
    S = S.replace("10", "")
    if len(S) == cur_len:
        print(init_len - cur_len)
        exit()

S = list(input())

zero_S = S.count("0")
one_S = S.count("1")

print(min(zero_S, one_S) * 2)


s = input()
ans = min(s.count("0"), s.count("1"))
print(ans * 2)


s = list(input())
n = len(s)
pos = 0
cnt = 0

while pos != n - 1 and n != 0:
    if s[pos] != s[pos + 1]:
        del s[pos]
        del s[pos]
        pos = 0
        n -= 2
        cnt += 2
    else:
        pos += 1

print(cnt)

s = input()
a = s.count("0")
b = s.count("1")
print(min(a, b) * 2)


import re

S = input()

trimmed = 0

while True:
    tmp = re.split("01|10", S)
    if len(tmp) == 1:
        break
    else:
        trimmed = trimmed + len(tmp) - 1
        S = "".join(tmp)

print(trimmed * 2)

S = input()

zero = 0
one = 0

for ch in S:
    if ch == "0":
        zero = zero + 1
    else:
        one = one + 1

print(min(zero, one) * 2)


S = list(int(input()))
a = S.count(0)
b = S.count(1)
ans = 2 * min(a, b)
print(ans)

S = list(input())
a = S.count("0")
b = S.count("1")
ans = 2 * min(a, b)
print(ans)

Traceback (most recent call last):
  File "/home/alex/Documents/bug-detection/input/Project_CodeNet/data/p03107/Python/s301284896.py", line 1, in <module>
    S = list(int(input()))
TypeError: 'int' object is not iterable


s = input()
count = 0
s = s + "a"
while True:
    if "01" in s:
        s = s.replace("01", "", 1)
        count += 2
    elif "10" in s:
        s = s.replace("10", "", 1)
        count += 2
    else:
        break
print(count)

s = input()
cube_list = [s[0]]
s = s[1:]
count = 0
for c in s:
    if cube_list:
        if cube_list[-1] == c:
            cube_list.append(c)
        else:
            count += 2
            cube_list.pop(-1)
    else:
        cube_list.append(c)
print(count)


s = input()
count = 0

while s.find("01") != -1 or s.find("10") != -1:
    count += 2 * s.count("01")
    s = s.replace("01", "")
    count += 2 * s.count("10")
    s = s.replace("10", "")
print(count)


S = input()
cnt1 = 0
cnt2 = 0
while True:
    before = S
    S = before.replace("01", "", 1)
    if before != S:
        cnt1 += 1
    else:
        break

while True:
    before = S
    S = before.replace("10", "", 1)
    if before != S:
        cnt2 += 1
    else:
        break

print(2 * (cnt1 + cnt2))

S = input()
print(2 * min(S.count("1"), S.count("0")))


b = list(map(int, input()))
cnt = 0
while len(b) > 1:

    if len(list(set(b))) == 1:
        break
    if b[0] != b[1]:
        b = b[2:]
        cnt += 1
        continue
    temp = b[:1]
    b = b[1:]
    b.append(*temp)
print(cnt * 2)

s = input()
zom = min(s.count("1"), s.count("0"))
if len(set(list(s))) == 1:
    zom = 0
print(zom * 2)


s = input()
n = len(s)

def divide(x):
    a = "".join(x.split("01"))
    b = "".join(a.split("10"))
    return b

def judge(x):
    return len(x.split("01")) == 1 and len(x.split("10")) == 1

while not judge(s):
    s = divide(s)

print(n - len(s))

s = input()
n = len(s)
num_0 = s.count("0")
num_1 = n - num_0

print(n - abs(num_0 - num_1))


s = list(input())

cnt = 0
i = 0

while i < len(s):
    while len(s) != 0 and s[i] != s[i + 1]:
        s.pop(i)
        s.pop(i)
        cnt += 2
        i -= 1
    i += 1

print(cnt)

s = list(input())

# Note: init
# cnt = 0
# i = 1
#
# while i < len(s):
#     while len(s) != 0 and s[i - 1] != s[i]:
#         s.pop(i)
#         s.pop(i - 1)
#         i -= 1
#         cnt += 2
#     i += 1
#
# print(cnt)

cnt = min(s.count("0"), s.count("1"))
print(cnt * 2)


class UnionFind(object):
    def __init__(self, n=1):
        self.par = [i for i in range(n)]  # グループ番号
        self.rank = [0 for _ in range(n)]
        self.num = [1 for _ in range(n)]

    def find(self, x):
        """
        x が属するグループを探索
        """
        if self.par[x] == x:
            return x
        else:
            self.par[x] = self.find(self.par[x])
            return self.par[x]

    def union(self, x, y):
        """
        x と y のグループを結合
        """
        x = self.find(x)
        y = self.find(y)
        if x != y:
            if self.rank[x] < self.rank[y]:
                x, y = y, x
            if self.rank[x] == self.rank[y]:
                self.rank[x] += 1
            self.par[y] = x
            self.num[x] += self.num[y]
            self.num[y] = 0

    def is_same(self, x, y):
        """
        x と y が同じグループか否か
        """
        return self.find(x) == self.find(y)

N, M = map(int, input().split())
L = [list(map(int, input().split())) for i in range(M)]
A = [0] * M
D = N * (N - 1) // 2
u = UnionFind(N)
for i in reversed(range(M)):
    # u.union(L[i][0]-1, L[i][1]-1)
    a = 0
    for j in range(N):
        # print(i, j, u.num[j])
        if u.num[j] >= 2:
            a += u.num[j] * (u.num[j] - 1) // 2
    A[i] = a
    # print(A[i])
    u.union(L[i][0] - 1, L[i][1] - 1)
for i in range(M):
    print(D - A[i])

class UnionFind(object):
    def __init__(self, n=1):
        self.par = [i for i in range(n)]  # グループ番号
        self.rank = [0 for _ in range(n)]
        self.num = [1 for _ in range(n)]

    def find(self, x):
        """
        x が属するグループを探索
        """
        if self.par[x] == x:
            return x
        else:
            self.par[x] = self.find(self.par[x])
            return self.par[x]

    def union(self, x, y):
        """
        x と y のグループを結合
        """
        x = self.find(x)
        y = self.find(y)
        if x != y:
            if self.rank[x] < self.rank[y]:
                x, y = y, x
            if self.rank[x] == self.rank[y]:
                self.rank[x] += 1
            self.par[y] = x
            self.num[x] += self.num[y]
            self.num[y] = 0

    def is_same(self, x, y):
        """
        x と y が同じグループか否か
        """
        return self.find(x) == self.find(y)

N, M = map(int, input().split())
L = [list(map(int, input().split())) for i in range(M)]
A = [0] * M
D = N * (N - 1) // 2
u = UnionFind(N)
a = 0
for i in reversed(range(M)):
    # u.union(L[i][0]-1, L[i][1]-1)
    # a = 0
    # for j in range(N):
    #     # print(i, j, u.num[j])
    #     if u.num[j] >= 2:
    #         a += u.num[j] * (u.num[j] - 1) // 2
    A[i] = a
    # print(A[i])
    if u.find(L[i][0] - 1) != u.find(L[i][1] - 1):
        if u.num[u.find(L[i][0] - 1)] > 1:
            a -= u.num[u.find(L[i][0] - 1)] * (u.num[u.find(L[i][0] - 1)] - 1) // 2
        if u.num[u.find(L[i][1] - 1)] > 1:
            a -= u.num[u.find(L[i][1] - 1)] * (u.num[u.find(L[i][1] - 1)] - 1) // 2
        u.union(L[i][0] - 1, L[i][1] - 1)
        # if u.find(L[i][0] - 1) != u.find(L[i][1] - 1):
        a += u.num[u.find(L[i][0] - 1)] * (u.num[u.find(L[i][0] - 1)] - 1) // 2

for i in range(M):
    print(D - A[i])


n, m = map(int, input().split())
par = [-1 for i in range(n)]  # 親だった場合は-(その集合のサイズ)

# xがどのグループに属しているか調べる
def find(x):
    if par[x] < 0:
        return x
    else:
        par[x] = find(par[x])
        return find(par[x])

# 自分のいるグループの数
def size(x):
    return -par[find(x)]

# xとyの属する集合を併合
def unite(x, y):
    # 根を探す
    x, y = find(x), find(y)

    # 根が一緒
    if x == y:
        return

    # 大きい方に小さい方をくっつける
    if size(x) < size(y):
        x, y = y, x

    # xのサイズを更新
    par[x] += par[y]
    # yの親をxにする
    par[y] = x

# 同じ集合に属するか判定
def same(x, y):
    return find(x) == find(y)

bridges = [list(map(int, input().split())) for _ in range(m)]
bridges.reverse()
huben = n * (n - 1) // 2
ans = []
for i in range(m):
    ans.append(huben)
    a, b = bridges[i]
    a, b = a - 1, b - 1
    if not same(a, b):
        huben -= size(a) * size(b)
    unite(a, b)
for i in range(m):
    print(ans[::-1][i])

n, m = map(int, input().split())
par = [-1] * n  # 親だった場合は-(その集合のサイズ)

# xがどのグループに属しているか調べる
def find(x):
    if par[x] < 0:
        return x
    else:
        par[x] = find(par[x])
        return find(par[x])

# 自分のいるグループの数
def size(x):
    return -par[find(x)]

# xとyの属する集合を併合
def unite(x, y):
    # 根を探す
    x, y = find(x), find(y)

    # 根が一緒
    if x == y:
        return

    # 大きい方に小さい方をくっつける
    if size(x) < size(y):
        x, y = y, x

    # xのサイズを更新
    par[x] += par[y]
    # yの親をxにする
    par[y] = x

# 同じ集合に属するか判定
def same(x, y):
    return find(x) == find(y)

bridges = [list(map(int, input().split())) for _ in range(m)]
bridges.reverse()
huben = n * (n - 1) // 2
ans = []
for i in range(m):
    ans.append(huben)
    a, b = bridges[i]
    a, b = a - 1, b - 1
    if same(a, b):
        continue
    else:
        huben -= size(a) * size(b)
        unite(a, b)
ans = ans[::-1]
for i in range(m):
    print(ans[i])


n, m = map(int, input().split())
par = [-1] * n  # 親だった場合は-(その集合のサイズ)

# xがどのグループに属しているか調べる
def find(x):
    if par[x] < 0:
        return x
    else:
        par[x] = find(par[x])
        return find(par[x])

# 自分のいるグループの数
def size(x):
    return -par[find(x)]

# xとyの属する集合を併合
def unite(x, y):
    # 根を探す
    x, y = find(x), find(y)

    # 根が一緒
    if x == y:
        return

    # 大きい方に小さい方をくっつける
    if size(x) < size(y):
        x, y = y, x

    # xのサイズを更新
    par[x] += par[y]
    # yの親をxにする
    par[y] = x

# 同じ集合に属するか判定
def same(x, y):
    return find(x) == find(y)

bridges = [list(map(int, input().split())) for _ in range(m)]
bridges.reverse()
huben = n * (n - 1) // 2
ans = []

for i in range(m):
    ans.append(huben)
    a, b = bridges[i]
    a, b = a - 1, b - 1
    if same(a, b):
        continue
    else:
        huben -= size(a) * size(b)
        unite(a, b)
ans = ans.reverse()
for i in range(m):
    print(ans[i])

n, m = map(int, input().split())
par = [-1] * n  # 親だった場合は-(その集合のサイズ)

# xがどのグループに属しているか調べる
def find(x):
    if par[x] < 0:
        return x
    else:
        par[x] = find(par[x])
        return find(par[x])

# 自分のいるグループの数
def size(x):
    return -par[find(x)]

# xとyの属する集合を併合
def unite(x, y):
    # 根を探す
    x, y = find(x), find(y)

    # 根が一緒
    if x == y:
        return

    # 大きい方に小さい方をくっつける
    if size(x) < size(y):
        x, y = y, x

    # xのサイズを更新
    par[x] += par[y]
    # yの親をxにする
    par[y] = x

# 同じ集合に属するか判定
def same(x, y):
    return find(x) == find(y)

bridges = [list(map(int, input().split())) for _ in range(m)]
bridges.reverse()
huben = n * (n - 1) // 2
ans = []
for i in range(m):
    ans.append(huben)
    a, b = bridges[i]
    a, b = a - 1, b - 1
    if same(a, b):
        continue
    else:
        huben -= size(a) * size(b)
        unite(a, b)
ans.reverse()
for i in range(m):
    print(ans[i])

Traceback (most recent call last):
  File "/home/alex/Documents/bug-detection/input/Project_CodeNet/data/p03108/Python/s038545963.py", line 59, in <module>
    print(ans[i])
TypeError: 'NoneType' object is not subscriptable


class UnionFind:
    def __init__(self, nodes):
        self._parents = {k: k for k in nodes}
        self._ranks = {k: 0 for k in nodes}
        self._sizes = {k: 1 for k in nodes}

    def union(self, x, y):
        x_root = self.find(x)
        y_root = self.find(y)
        if x_root != y_root:
            if self._ranks[x_root] < self._ranks[y_root]:
                self._parents[y_root] = x_root
                self._ranks[x_root] += 1
                self._sizes[x_root] += self._sizes[y_root]
            else:
                self._parents[x_root] = y_root
                self._ranks[y_root] += 1
                self._sizes[y_root] += self._sizes[x_root]

    def find(self, x):
        if self._parents[x] == x:
            return x
        self._parents[x] = self.find(self._parents[x])
        return self._parents[x]

    def size(self, x):
        return self._sizes[self.find(x)]

n, m = map(int, input().split())

bridges = []
for _ in range(m):
    bridges.append(list(map(int, input().split())))

def c2(n):
    # n から2個選ぶ組み合わせ nC2
    return int(n * (n - 1) / 2)

bridges.reverse()
uf = UnionFind(nodes=[i + 1 for i in range(n)])
# 便利さ; つながってる島の組み合わせの数
useful = 0
useful_history = []

for bridge in bridges:
    useful_history.append(useful)

    if uf.find(bridge[0]) != uf.find(bridge[1]):
        # 新しくつながったので便利さを更新
        size0 = uf.size(bridge[0])
        size1 = uf.size(bridge[1])
        useful += c2(size0 + size1) - c2(size0) - c2(size1)
        uf.union(bridge[0], bridge[1])

for u in reversed(useful_history):
    print(useful - u)

class UnionFind:
    def __init__(self, nodes):
        self._parents = {k: k for k in nodes}
        self._ranks = {k: 0 for k in nodes}
        self._sizes = {k: 1 for k in nodes}

    def union(self, x, y):
        """
        x が属する木と y が属する木を併合
        :param x:
        :param y:
        :return:
        """
        x = self.find(x)
        y = self.find(y)
        if x != y:
            # rank が小さい方が下
            if self._ranks[x] > self._ranks[y]:
                self._parents[y] = x
                self._sizes[x] += self._sizes[y]
            else:
                self._parents[x] = y
                self._sizes[y] += self._sizes[x]
                if self._ranks[x] == self._ranks[y]:
                    self._ranks[y] += 1

    def find(self, x):
        """
        x が属する木の root
        :param x:
        :return:
        """
        if self._parents[x] == x:
            return x
        self._parents[x] = self.find(self._parents[x])
        return self._parents[x]

    def size(self, x):
        """
        x が属する木のノード数
        :param x:
        :return:
        """
        return self._sizes[self.find(x)]

n, m = map(int, input().split())

bridges = []
for _ in range(m):
    bridges.append(list(map(int, input().split())))

def c2(n):
    # n から2個選ぶ組み合わせ nC2
    return int(n * (n - 1) / 2)

bridges.reverse()
uf = UnionFind(nodes=[i + 1 for i in range(n)])
# 便利さ; つながってる島の組み合わせの数
useful = 0
useful_history = []

for bridge in bridges:
    useful_history.append(useful)

    if uf.find(bridge[0]) != uf.find(bridge[1]):
        # 新しくつながったので便利さを更新
        size0 = uf.size(bridge[0])
        size1 = uf.size(bridge[1])
        useful += c2(size0 + size1) - c2(size0) - c2(size1)
        uf.union(bridge[0], bridge[1])

for u in reversed(useful_history):
    print(useful - u)


class UnionFind:
    def __init__(self, n):
        self.v = [-1 for _ in range(n)]  # 根(負): 連結頂点数 * (-1) / 子(正): 根の頂点番号(0-indexed)

    def find(self, x):  # xを含む木における根の頂点番号を返す
        if self.v[x] < 0:  # (負)は根
            return x
        else:  # 根の頂点番号
            self.v[x] = self.find(self.v[x])  # uniteでは, 旧根に属する頂点の根が旧根のままなので更新
            return self.v[x]

    def unite(self, x, y):  # 違う根に属していたらrankが低くなるように連結
        x = self.find(x)
        y = self.find(y)
        if x == y:
            return
        if -self.v[x] < -self.v[y]:  # size比較, 　(-1) * (連結頂点数 * (-1)), (正)同士の大小比較
            x, y = y, x  # 連結頂点数が少ない方をyにすると, findでの更新回数が減る？
        self.v[x] += self.v[y]  # 連結頂点数の和を取る, 連結頂点数 * (-1)
        self.v[y] = x  # 連結頂点数が少ないy(引数yの根の頂点番号)の根をx(引数xの根の頂点番号)にする

    def root(self, x):
        return self.v[x] < 0  # (負)は根

    def same(self, x, y):
        return self.find(x) == self.find(y)  # 同じ根に属するか

    def size(self, x):
        return -self.v[self.find(x)]  # 連結頂点数を返す

N, M = map(int, input().split())
e = []
uf = UnionFind(N)
for _ in range(M):
    a, b = map(int, input().split())
    a -= 1
    b -= 1
    e.append((a, b))

init = N * (N - 1) // 2
ans = [init for _ in range(M)]

for i in sorted(range(M), reverse=True):
    cc = 0
    for v in range(N):
        if uf.root(v):
            set_size = uf.size(v)
            cc += set_size * (set_size - 1) // 2
    ans[i] -= cc
    a, b = e[i]
    uf.unite(a, b)

for i in range(M):
    print(ans[i])

class UnionFind:
    def __init__(self, n):
        self.v = [-1 for _ in range(n)]  # 根(負): 連結頂点数 * (-1) / 子(正): 根の頂点番号(0-indexed)

    def find(self, x):  # xを含む木における根の頂点番号を返す
        if self.v[x] < 0:  # (負)は根
            return x
        else:  # 根の頂点番号
            self.v[x] = self.find(self.v[x])  # uniteでは, 旧根に属する頂点の根が旧根のままなので更新
            return self.v[x]

    def unite(self, x, y):  # 違う根に属していたらrankが低くなるように連結
        x = self.find(x)
        y = self.find(y)
        if x == y:
            return
        if -self.v[x] < -self.v[y]:  # size比較, 　(-1) * (連結頂点数 * (-1)), (正)同士の大小比較
            x, y = y, x  # 連結頂点数が少ない方をyにすると, findでの更新回数が減る？
        self.v[x] += self.v[y]  # 連結頂点数の和を取る, 連結頂点数 * (-1)
        self.v[y] = x  # 連結頂点数が少ないy(引数yの根の頂点番号)の根をx(引数xの根の頂点番号)にする

    def root(self, x):
        return self.v[x] < 0  # (負)は根

    def same(self, x, y):
        return self.find(x) == self.find(y)  # 同じ根に属するか

    def size(self, x):
        return -self.v[self.find(x)]  # 連結頂点数を返す

N, M = map(int, input().split())
e = []
for _ in range(M):
    a, b = map(int, input().split())
    a -= 1
    b -= 1
    e.append((a, b))

init = N * (N - 1) // 2
uf = UnionFind(N)

ans = [-1 for _ in range(M)]
cc = 0
for i in sorted(range(M), reverse=True):
    ans[i] = init - cc
    a, b = e[i]
    sa, sb = uf.size(a), uf.size(b)
    if sa == 1:
        if sb == 1:
            cc += 1
        else:  # sb > 1
            cc -= sb * (sb - 1) // 2
            cc += sb * (sb + 1) // 2
    else:
        if sb == 1:
            cc -= sa * (sa - 1) // 2
            cc += sa * (sa + 1) // 2
        else:
            # sa > 1 and sb > 1
            if uf.same(a, b):
                pass
            else:
                cc -= sa * (sa - 1) // 2
                cc -= sb * (sb - 1) // 2
                cc += (sa + sb) * (sa + sb - 1) // 2
    uf.unite(a, b)

for i in range(M):
    print(ans[i])


import sys
from itertools import combinations

input = sys.stdin.readline

class UnionFind:
    """Union Find class.

    "Path compression" and "Union by rank" are used.

    References:
        <https://en.wikipedia.org/wiki/Disjoint-set_data_structure>
    """

    def __init__(self, N):
        self.N = N
        self.__make_set()
        self.elem = {i: set([i]) for i in range(1, N + 1)}

    def __make_set(self):
        self.parent = list(range(self.N + 1))
        self.rank = [0] * (self.N + 1)

    def find(self, x):
        if self.parent[x] != x:
            self.parent[x] = self.find(self.parent[x])
        return self.parent[x]

    def union(self, x, y):
        x_root = self.find(x)
        y_root = self.find(y)

        if x_root == y_root:
            return

        x_rank = self.rank[x_root]
        y_rank = self.rank[y_root]
        if x_rank > y_rank:
            self.parent[y_root] = x_root
            self.union_elem(y_root, x_root)
        elif x_rank < y_rank:
            self.parent[x_root] = y_root
            self.union_elem(x_root, y_root)
        else:
            self.parent[y_root] = x_root
            self.rank[x_root] += 1
            self.union_elem(y_root, x_root)

    def union_elem(self, src_root, dst_root):
        self.elem[dst_root] |= self.elem[src_root]
        del self.elem[src_root]

    def elem_counts(self):
        return [len(s) for s in self.elem.values()]

def main():
    N, M = map(int, input().split())
    A = [0] * M
    B = [0] * M
    for i in range(M):
        A[i], B[i] = map(int, input().split())

    uf = UnionFind(N)
    ans = [0] * M
    for i in reversed(range(M)):
        ans[i] = sum(a * b for a, b in combinations(uf.elem_counts(), r=2))
        uf.union(A[i], B[i])

    print("\\n".join(map(str, ans)))

if __name__ == "__main__":
    main()

import sys
from itertools import combinations

input = sys.stdin.readline

class UnionFind:
    """Union Find class.

    "Path compression" and "Union by rank" are used.

    References:
        <https://en.wikipedia.org/wiki/Disjoint-set_data_structure>
    """

    def __init__(self, N):
        self.N = N
        self.__make_set()

    def __make_set(self):
        self._parent = list(range(self.N + 1))
        self._rank = [0] * (self.N + 1)
        self._size = [1] * (self.N + 1)

    def find(self, x):
        if self._parent[x] != x:
            self._parent[x] = self.find(self._parent[x])
        return self._parent[x]

    def union(self, x, y):
        x_root = self.find(x)
        y_root = self.find(y)

        if x_root == y_root:
            return

        x_rank = self._rank[x_root]
        y_rank = self._rank[y_root]
        if x_rank > y_rank:
            self._parent[y_root] = x_root
            self._size[x_root] += self._size[y_root]
        elif x_rank < y_rank:
            self._parent[x_root] = y_root
            self._size[y_root] += self._size[x_root]
        else:
            self._parent[y_root] = x_root
            self._rank[x_root] += 1
            self._size[x_root] += self._size[y_root]

    def same_set(self, x, y):
        return self.find(x) == self.find(y)

    def size(self, x):
        return self._size[self.find(x)]

def main():
    N, M = map(int, input().split())
    A = [0] * M
    B = [0] * M
    for i in range(M):
        A[i], B[i] = map(int, input().split())

    uf = UnionFind(N)
    ans = [0] * M
    ans[-1] = N * (N - 1) // 2
    for i in reversed(range(M - 1)):
        a, b = A[i + 1], B[i + 1]
        if uf.same_set(a, b):
            ans[i] = ans[i + 1]
        else:
            ans[i] = ans[i + 1] - uf.size(a) * uf.size(b)
        uf.union(a, b)

    print("\\n".join(map(str, ans)))

if __name__ == "__main__":
    main()


import sys

def input():
    return sys.stdin.readline().strip()

sys.setrecursionlimit(20000000)

class UnionFind:
    def __init__(self, n_nodes):
        self.n_nodes = n_nodes
        self.parent = [i for i in range(n_nodes)]
        self.rank = [1] * n_nodes
        self.size = [1] * n_nodes

    def find(self, x):
        if self.parent[x] == x:
            return x
        else:
            self.parent[x] = self.find(self.parent[x])
            return self.parent[x]

    def unite(self, x, y):
        x = self.find(x)
        y = self.find(y)
        if x == y:
            return
        if self.rank[x] > self.rank[y]:
            self.parent[y] = x
            self.size[x] += self.size[y]
        else:
            self.parent[x] = y
            self.size[y] += self.size[x]
            if self.rank[x] == self.rank[y]:
                self.rank[y] += 1

    def check(self, x, y):
        return self.find(x) == self.find(y)

    def get_parent_list(self):
        return [i for i in range(self.n_nodes) if self.find(i) == i]

    def get_n_groups(self):
        return len(self.get_parent_list())

    def get_members(self, x):
        parent = self.find(x)
        return [i for i in range(self.n_nodes) if self.find(i) == parent]

    def get_members_dict(self):
        return {par: self.get_members(par) for par in self.get_parent_list()}

def main():
    N, M = map(int, input().split())
    Bridge = [list(map(int, input().split())) for _ in range(M)]
    answer = [N * (N - 1) // 2]
    UF = UnionFind(N)
    for i in range(M):
        AB = Bridge[M - i - 1]
        before = len(UF.get_members(AB[0] - 1))
        UF.unite(AB[0] - 1, AB[1] - 1)
        after = len(UF.get_members(AB[0] - 1))
        last = answer[-1]
        if before == after:
            answer.append(last)
        else:
            answer.append(last - (after - before) * before)

    answer = answer[::-1]
    print(*answer[1:], sep="\\n")

if __name__ == "__main__":
    main()

import sys

def input():
    return sys.stdin.readline().strip()

sys.setrecursionlimit(20000000)

class UnionFind:
    def __init__(self, n_nodes):
        self.n_nodes = n_nodes
        self.parent = [i for i in range(n_nodes)]
        self.rank = [1] * n_nodes
        self.size = [1] * n_nodes

    def find(self, x):
        if self.parent[x] == x:
            return x
        else:
            self.parent[x] = self.find(self.parent[x])
            return self.parent[x]

    def unite(self, x, y):
        x = self.find(x)
        y = self.find(y)
        if x == y:
            return
        if self.rank[x] > self.rank[y]:
            self.parent[y] = x
            self.size[x] += self.size[y]
        else:
            self.parent[x] = y
            self.size[y] += self.size[x]
            if self.rank[x] == self.rank[y]:
                self.rank[y] += 1

    def check(self, x, y):
        return self.find(x) == self.find(y)

    def get_parent_list(self):
        return [i for i in range(self.n_nodes) if self.find(i) == i]

    def get_n_groups(self):
        return len(self.get_parent_list())

    def get_members(self, x):
        parent = self.find(x)
        return [i for i in range(self.n_nodes) if self.find(i) == parent]

    def get_members_dict(self):
        return {par: self.get_members(par) for par in self.get_parent_list()}

def main():
    N, M = map(int, input().split())
    Bridge = [list(map(int, input().split())) for _ in range(M)]
    answer = [N * (N - 1) // 2]
    UF = UnionFind(N)
    for i in range(M):
        AB = Bridge[M - i - 1]
        A = AB[0] - 1
        B = AB[1] - 1
        if UF.check(A, B):
            last = answer[-1]
            answer.append(last)
            continue
        else:
            before = UF.size[UF.find(A)]
            UF.unite(AB[0] - 1, AB[1] - 1)
            after = UF.size[UF.find(A)]
            last = answer[-1]
            if before == after:
                answer.append(last)
            else:
                answer.append(last - (after - before) * before)

    answer = answer[::-1]
    print(*answer[1:], sep="\\n")

if __name__ == "__main__":
    main()


N, M = map(int, input().split())
A = []
B = []
for _ in range(M):
    a, b = map(int, input().split())
    A.append(a - 1)
    B.append(b - 1)

uf = [-1] * N
values = []
for i in range(M - 1, -1, -1):
    a = A[i]
    b = B[i]

    cur_a = a
    while 0 <= uf[cur_a]:
        cur_a = uf[cur_a]
    num_a = -uf[cur_a]
    root_a = cur_a

    cur_b = b
    while 0 <= uf[cur_b]:
        cur_b = uf[cur_b]
    num_b = -uf[cur_b]
    root_b = cur_b

    if root_a != root_b:
        values.append(num_a * num_b)

        uf[root_b] += uf[root_a]
        cur = uf[root_a]
        uf[root_a] = root_b
        while 0 <= cur and 0 <= uf[cur]:
            nxt = uf[cur]
            uf[cur] = root_b
            cur = nxt
    else:
        values.append(0)

total = 0
for v in reversed(values):
    total += v
    print(total)

import io
import sys

lines = sys.stdin.read().splitlines()[::-1]

def readline():
    return lines.pop()

N, M = map(int, readline().split())
A = []
B = []
for _ in range(M):
    a, b = map(int, readline().split())
    A.append(a - 1)
    B.append(b - 1)

uf = [-1] * N
values = []
for i in range(M - 1, -1, -1):
    a = A[i]
    b = B[i]

    cur = a
    while 0 <= uf[cur]:
        cur = uf[cur]
    num_a = -uf[cur]
    root_a = cur
    cur = a
    while 0 <= uf[cur]:
        nxt = uf[cur]
        uf[cur] = root_a
        cur = nxt

    cur = b
    while 0 <= uf[cur]:
        cur = uf[cur]
    num_b = -uf[cur]
    root_b = cur
    cur = b
    while 0 <= uf[cur]:
        nxt = uf[cur]
        uf[cur] = root_b
        cur = nxt

    if root_a != root_b:
        values.append(num_a * num_b)

        if num_a < num_b:
            uf[root_b] += uf[root_a]
            cur = uf[root_a]
            uf[root_a] = root_b
            while 0 <= cur and 0 <= uf[cur]:
                nxt = uf[cur]
                uf[cur] = root_b
                cur = nxt
        else:
            uf[root_a] += uf[root_b]
            cur = uf[root_b]
            uf[root_b] = root_a
            while 0 <= cur and 0 <= uf[cur]:
                nxt = uf[cur]
                uf[cur] = root_a
                cur = nxt
    else:
        values.append(0)

total = 0
out = io.StringIO()
for v in reversed(values):
    total += v
    print(total, file=out)
print(out.getvalue())


n = int(input())
ans = 0
for _ in range(n):
    x, y = map(int, input().split())
    if y == "JPY":
        ans += x
    else:
        ans += 380000
print(ans)

n = int(input())
ans = 0
for _ in range(n):
    x, y = input().split()
    x = float(x)
    if y == "JPY":
        ans += x
    else:
        ans += x * 380000.0
print(ans)

Traceback (most recent call last):
  File "/home/alex/Documents/bug-detection/input/Project_CodeNet/data/p03110/Python/s397276903.py", line 4, in <module>
    x, y = map(int, input().split())
ValueError: invalid literal for int() with base 10: 'JPY'


N = int(input())

money = 0
for _ in range(N):
    x, u = map(int, input().split())
    if u == "JPY":
        money += x
    else:
        money += x * 380000
print(money)

N = int(input())

money = 0
for _ in range(N):
    x, u = input().split()
    if u == "JPY":
        money += float(x)
    else:
        money += float(x) * 380000
print(money)

Traceback (most recent call last):
  File "/home/alex/Documents/bug-detection/input/Project_CodeNet/data/p03110/Python/s996181332.py", line 5, in <module>
    x, u = map(int, input().split())
ValueError: invalid literal for int() with base 10: 'JPY'


from math import inf
from collections import deque

N, A, B, C = map(int, input().split())
ls = [0]
for _ in range(N):
    ls.append(int(input()))
ls.sort()

q = deque()
q.append((0, -30, 0, 0, 0))
minmp = inf
while len(q):
    n, mp, a, b, c = q.pop()
    if n == N:
        if min(a, b, c) == 0:
            continue
        minmp = min(minmp, mp + abs(a - A) + abs(b - B) + abs(c - C))
    else:
        q.append((n + 1, mp, a, b, c))
        q.append((n + 1, mp + 10, a + ls[n + 1], b, c))
        q.append((n + 1, mp + 10, a, b + ls[n + 1], c))
        q.append((n + 1, mp + 10, a, b, c + ls[n + 1]))

print(minmp)

try:
    from math import inf
except:
    inf = float("inf")
from collections import deque

N, A, B, C = map(int, input().split())
ls = [0]
for _ in range(N):
    ls.append(int(input()))
ls.sort()

q = deque()
q.append((0, -30, 0, 0, 0))
minmp = inf
while len(q):
    n, mp, a, b, c = q.pop()
    if n == N:
        if min(a, b, c) == 0:
            continue
        minmp = min(minmp, mp + abs(a - A) + abs(b - B) + abs(c - C))
    else:
        q.append((n + 1, mp, a, b, c))
        q.append((n + 1, mp + 10, a + ls[n + 1], b, c))
        q.append((n + 1, mp + 10, a, b + ls[n + 1], c))
        q.append((n + 1, mp + 10, a, b, c + ls[n + 1]))

print(minmp)


import itertools

N, A, B, C = map(int, input().split())
L = [int(input()) for n in range(N)]
P = []

for n in range(1, N + 1):
    P.extend(itertools.combinations([n for n in range(N)], n))

add_len = {s: sum(L[i] for i in s) for s in P}

def cost(v, s):
    return 10 * (len(s) - 1) + abs(v - add_len[s])

res = 2 << 30
for a in P:
    cost_a = cost(A, a)
    if res < cost_a:
        continue

    for b in P:
        cost_b = cost(B, b)
        if res < cost_b:
            continue

        if len(set(a) & set(b)):
            continue

        for c in P:
            cost_c = cost(C, c)
            if res < cost_c:
                continue

            if len(set(a) & set(c)) or len(set(b) & set(c)):
                continue
            res = min(res, cost_a + cost_b + cost_c)

print(res)

import itertools

N, A, B, C = map(int, input().split())
L = [int(input()) for n in range(N)]
P = []

for n in range(1, N + 1):
    P.extend(itertools.combinations([n for n in range(N)], n))

add_len = {s: sum(L[i] for i in s) for s in P}

def cost(v, s):
    return 10 * (len(s) - 1) + abs(v - add_len[s])

res = 2 << 30
for a in P:
    cost_a = cost(A, a)
    if res < cost_a:
        continue

    for b in P:
        cost_b = cost(B, b)
        if res < cost_b:
            continue

        set_a = set(a)
        set_b = set(b)
        if len(set_a & set_b):
            continue

        for c in P:
            cost_c = cost(C, c)
            if res < cost_c:
                continue

            set_c = set(c)
            if len(set_a & set_c) or len(set_b & set_c):
                continue
            res = min(res, cost_a + cost_b + cost_c)

print(res)


def solve(n, k, aaa):
    buf = []
    prev_last = -1

    for t in range(k + 1):
        ma, mi = min((a, i + 1) for i, a in enumerate(aaa) if i + 1 != prev_last)
        others = set(range(1, n + 1)) - {prev_last, mi}
        buf.extend(others)
        if t == k:
            aaa[mi] += 1
        else:
            buf.append(mi)

        for i in range(n):
            if i + 1 != prev_last:
                aaa[i] -= 1
                if aaa[i] < 0:
                    print(-1)
                    return

        prev_last = mi

    print(len(buf))
    print(*buf)

n, k = map(int, input().split())
aaa = list(map(int, input().split()))
solve(n, k, aaa)

def solve(n, k, aaa):
    buf = []
    prev_last = -1

    for t in range(k + 1):
        ma, mi = min((a, i + 1) for i, a in enumerate(aaa) if i + 1 != prev_last)
        others = set(range(1, n + 1)) - {prev_last, mi}
        buf.extend(others)
        buf.append(mi)
        # print(buf)

        for i in range(n):
            if i + 1 != prev_last:
                aaa[i] -= 1
                if aaa[i] < 0:
                    print(-1)
                    return

        prev_last = mi

    print(len(buf))
    print(*buf)

n, k = map(int, input().split())
aaa = list(map(int, input().split()))
solve(n, k, aaa)

Traceback (most recent call last):
  File "/home/alex/Documents/bug-detection/input/Project_CodeNet/data/p03113/Python/s607630341.py", line 29, in <module>
    solve(n, k, aaa)
  File "/home/alex/Documents/bug-detection/input/Project_CodeNet/data/p03113/Python/s607630341.py", line 10, in solve
    aaa[mi] += 1
IndexError: list index out of range


a = int(input())
b = int(input())
r = a + b if b % a == 0 else b - a
print(r)

a, b = list(map(int, input().split()))
r = a + b if b % a == 0 else b - a
print(r)

Traceback (most recent call last):
  File "/home/alex/Documents/bug-detection/input/Project_CodeNet/data/p03125/Python/s844412029.py", line 1, in <module>
    a = int(input())
ValueError: invalid literal for int() with base 10: '4 12'


a, b = [int(x) for x in input().split()]
print([a + b, b - a][int(b % a)])

a, b = [int(x) for x in input().split()]
print([a + b, b - a][int(bool(b % a))])


# -*- coding: utf-8 -*-
import sys

def func_a(argv):
    A = int(argv[0]) if int(argv[0]) >= 1 else 0
    B = int(argv[1]) if int(argv[1]) <= 20 else 0

    if A * B == 0:
        pass
    else:
        if B % A == 0:
            print(A + B)
        else:
            print(B - A)

if __name__ == "__main__":
    func_a(sys.argv)

# -*- coding: utf-8 -*-

def func_a():
    argv = input().split(" ")
    A = int(argv[0]) if int(argv[0]) >= 1 else 0
    B = int(argv[1]) if int(argv[1]) <= 20 else 0

    if A * B == 0:
        pass
    else:
        if B % A == 0:
            print(A + B)
        else:
            print(B - A)

if __name__ == "__main__":
    func_a()

Traceback (most recent call last):
  File "/home/alex/Documents/bug-detection/input/Project_CodeNet/data/p03125/Python/s435981808.py", line 19, in <module>
    func_a(sys.argv)
  File "/home/alex/Documents/bug-detection/input/Project_CodeNet/data/p03125/Python/s435981808.py", line 6, in func_a
    A = int(argv[0]) if int(argv[0]) >= 1 else 0
ValueError: invalid literal for int() with base 10: '/home/alex/Documents/bug-detection/input/Project_CodeNet/data/p03125/Python/s435981808.py'


a, b = map(int, input())
if b % a == 0:
    print(a + b)
else:
    print(b - a)

a, b = map(int, input().split())
if b % a == 0:
    print(a + b)
else:
    print(b - a)

Traceback (most recent call last):
  File "/home/alex/Documents/bug-detection/input/Project_CodeNet/data/p03125/Python/s517301094.py", line 1, in <module>
    a, b = map(int, input())
ValueError: invalid literal for int() with base 10: ' '


A, B = list(map(int, input()))

if B % A == 0:
    print(A + B)
else:
    print(B - A)

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

if B % A == 0:
    print(A + B)
else:
    print(B - A)

Traceback (most recent call last):
  File "/home/alex/Documents/bug-detection/input/Project_CodeNet/data/p03125/Python/s504979530.py", line 1, in <module>
    A, B = list(map(int, input()))
ValueError: invalid literal for int() with base 10: ' '


N, M = map(int, input().split())
a_k = [list(map(int, input().split())) for i in range(N)]
a_k.sort()

a_non = []
for i in range(1, len(a_k)):
    if len(a_k[0]) <= 1:
        break

    for j in range(1, len(a_k[0])):
        if a_k[0][j] not in a_k[i][1:]:
            a_non.append(j)

for j in range(len(a_non)):
    a_k[0].pop(a_non[j])

print(len(a_k[0]) - 1)

N, M = map(int, input().split())
k = [i[1:] for i in [list(map(int, input().split())) for i in range(N)]]
k_1 = [flatten for inner in k for flatten in inner]
count_k = 0
for i in range(1, M + 1):
    if k_1.count(i) == N:
        count_k += 1

print(count_k)