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

ans = 0
i = 0
while i < len(A):
    if (i + 1) == A[i]:
        i += 1
    else:
        A.pop(i)
        ans += 1

if A:
    print(ans)
else:
    print(-1)

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

ans = 0
num = 1
for a in A:
    if a == num:
        num += 1
    else:
        ans += 1

if ans == N:
    ans = -1

print(ans)


N = int(input())
a = list(map(int, input().split()))
ans = 0
i = 1
j = 0
while j < N:
    if i not in a[j:]:
        ans += N - j
        break
    nj = a[j:].index(i)
    ans += nj - j
    i += 1
    j = nj + 1
if i == 1:
    print(-1)
else:
    print(ans)

N = int(input())
a = list(map(int, input().split()))
ans = 0
x = 1
for i in a:
    if i == x:
        x += 1
    else:
        ans += 1
if x == 1:
    print(-1)
else:
    print(ans)


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

i = 1
while True:
    if i in list_a:
        index = list_a.index(i)
        list_a = list_a[index:]
        i += 1
    else:
        break

print(N - (i - 1) if i != 1 else -1)

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

count = 1
for i in list_a:
    if i == count:
        count += 1

print(N - (count - 1) if count != 1 else -1)


import math

def main():
    N = int(input())

    if N % 2 == 0:
        B = N // 2
        n = math.floor(math.log(B) / math.log(5))

        ans = 0
        for i in range(1, n + 1):
            ans += B // (5**i)

    else:
        ans = 0

    print(int(ans))

main()

import math

def main():
    N = int(input())

    if N == 0 or N == 1:
        ans = 0

    elif N % 2 == 0:
        B = N // 2
        n = math.floor(math.log(B) / math.log(5))

        ans = 0
        for i in range(1, n + 1):
            ans += B // (5**i)

    else:
        ans = 0

    print(int(ans))

main()


N = int(input())

if N % 2 != 0:
    print(0)

else:
    count = N // 10
    for j in range(1, 50):
        count += N // ((5**j) * 10)

print(count)


from collections import deque

def nearlist(N, LIST):  # 隣接リスト
    NEAR = [set() for _ in range(N)]
    for a, b in LIST:
        NEAR[a - 1].add(b - 1)
        NEAR[b - 1].add(a - 1)
    return NEAR

def bfs(NEAR, S, N):  # 幅優先探索  # キュー
    dist = [-1 for _ in range(N)]  # 前処理
    dist[S] = 0
    que, frag = deque([S]), set([S])

    while len(que) > 0:
        q = que.popleft()
        for i in NEAR[q]:  # 移動先の候補
            if i in frag:  # 処理済みか否か
                continue
            dist[i] = dist[q] + 1
            que.append(i), frag.add(i)
    return dist

n, u, v = map(int, input().split())
ab = [list(map(int, input().split())) for _ in range(n - 1)]

near = nearlist(n, ab)
tkdist, akdist = bfs(near, u - 1, n), bfs(near, v - 1, n)

node = [i for i in range(n) if tkdist[i] <= akdist[i]]
ans = max(akdist[i] for i in node)
print(ans[0] - 1)

from collections import deque

def nearlist(N, LIST):  # 隣接リスト
    NEAR = [set() for _ in range(N)]
    for a, b in LIST:
        NEAR[a - 1].add(b - 1)
        NEAR[b - 1].add(a - 1)
    return NEAR

def bfs(NEAR, S, N):  # 幅優先探索  # キュー
    dist = [-1 for _ in range(N)]  # 前処理
    dist[S] = 0
    que, frag = deque([S]), set([S])

    while len(que) > 0:
        q = que.popleft()
        for i in NEAR[q]:  # 移動先の候補
            if i in frag:  # 処理済みか否か
                continue
            dist[i] = dist[q] + 1
            que.append(i), frag.add(i)
    return dist

n, u, v = map(int, input().split())
ab = [list(map(int, input().split())) for _ in range(n - 1)]

near = nearlist(n, ab)
tkdist, akdist = bfs(near, u - 1, n), bfs(near, v - 1, n)

node = [i for i in range(n) if tkdist[i] <= akdist[i]]
ans = max(akdist[i] for i in node)
print(ans - 1)

Traceback (most recent call last):
  File "/home/alex/Documents/bug-detection/input/Project_CodeNet/data/p02834/Python/s347111194.py", line 35, in <module>
    print(ans[0] - 1)
TypeError: 'int' object is not subscriptable


import sys

def dfs(v, parent, depth):
    parents[v] = parent
    self_depths[v] = depth
    sd = 0
    for u in links[v]:
        if u == parent:
            continue
        res = dfs(u, v, depth + 1)
        sd = max(sd, res + 1)

    subtree_depths[v] = sd
    return sd

def solve(u, v):
    ans = 0
    catch = self_depths[u] // 2
    while self_depths[u] > catch:
        ans = max(ans, self_depths[u] + subtree_depths[u] - 1)
        u = parents[u]
    return ans

n, u, v = map(int, input().split())
u -= 1
v -= 1
links = [set() for _ in range(n)]
for line in sys.stdin:
    a, b = map(int, line.split())
    a -= 1
    b -= 1
    links[a].add(b)
    links[b].add(a)

parents = [-1] * n
self_depths = [0] * n
subtree_depths = [0] * n
dfs(v, -1, 0)
# print(parents)
# print(self_depths)
# print(subtree_depths)
print(solve(u, v))

import sys

sys.setrecursionlimit(10**5)

def dfs(v, parent, depth):
    parents[v] = parent
    self_depths[v] = depth
    sd = 0
    for u in links[v]:
        if u == parent:
            continue
        res = dfs(u, v, depth + 1)
        sd = max(sd, res + 1)

    subtree_depths[v] = sd
    return sd

def solve(u):
    ans = 0
    catch = self_depths[u] // 2
    while self_depths[u] > catch:
        ans = max(ans, self_depths[u] + subtree_depths[u] - 1)
        u = parents[u]
    return ans

n, u, v = map(int, input().split())
u -= 1
v -= 1
links = [set() for _ in range(n)]
for line in sys.stdin:
    a, b = map(int, line.split())
    a -= 1
    b -= 1
    links[a].add(b)
    links[b].add(a)

parents = [-1] * n
self_depths = [0] * n
subtree_depths = [0] * n
dfs(v, -1, 0)
# print(parents)
# print(self_depths)
# print(subtree_depths)
print(solve(u))


import sys

sys.setrecursionlimit(10**6)

n, u, v = map(int, input().split())
# よりたかはしくんが早く辿り着ける葉のうち、青木君から最も遠いで
u -= 1
v -= 1

g = [[] for _ in range(n)]
for _ in range(n - 1):
    a, b = map(lambda x: int(x) - 1, input().split())
    g[a].append(b)
    g[b].append(a)

INF = 1 << 30
t = [INF] * n
a = [INF] * n

t[u] = 0
a[v] = 0

def dfs(v, p, d):
    for nv in g[v]:
        if nv == p:
            continue
        if d[nv] == INF:
            d[nv] = d[v] + 1
            dfs(nv, v, d)

dfs(u, -1, t)
dfs(v, -1, a)

ans = 0
for i in range(n):
    if t[i] < a[i]:
        ans = max(ans, a[i] - 1)
print(ans)


# nlognの実装。駄目。
import numpy as np
from scipy.sparse.csgraph import dijkstra

def calc(d):
    if d == 1 or d == 0:
        return 0
    else:
        return int(d - 1)

delta = []
n, u, v = map(int, input().split())
Gn = np.full((n, n), float("inf"))
for i in range(n - 1):
    a, b = map(int, input().split())
    a = a - 1
    b = b - 1
    Gn[a][b] = 1
    Gn[b][a] = 1
    Gn[i][i] = 0
minimum_u = dijkstra(Gn, indices=u - 1)
minimum_v = dijkstra(Gn, indices=v - 1)
for i in range(n):
    if minimum_v[i] - minimum_u[i] > 0 and i != u - 1 and i != v - 1:
        delta.append([i, minimum_v[i]])
if delta != []:
    delta_sorted = sorted(delta, key=lambda x: x[1])
    p = delta_sorted[-1][0]
    d = int(minimum_v[p])
    print(calc(d))
else:
    print(calc(minimum_u[v - 1]))

# 写経
import sys

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

n, u, v = map(int, readline().split())
m = map(int, read().split())
AB = zip(m, m)

graph = [[] for _ in range(n + 1)]
for a, b in AB:
    graph[a].append(b)
    graph[b].append(a)

def dfs(v):
    dist = [-1] * (n + 1)
    # 初期化
    stack = [v]
    # 初期位置を記憶
    dist[v] = 0
    # 初期位置の距離を0に
    while stack:
        # stackの中身がなくなるまで頑張る
        v = stack.pop()
        # stackから1つ要素を取り出す
        dw = dist[v] + 1
        # その要素の周りを考えるので、距離としてはその要素の距離に1を加えたものを考える
        for w in graph[v]:
            # 考えている要素に対して隣接している点に対して
            if dist[w] >= 0:
                # もし今まで訪れたことがあったら無視
                continue
            dist[w] = dw
            # 訪れたことがなかったら距離を更新
            stack.append(w)
            # 訪れたことがないところだったらstackに訪れる点リストとして保存
    return dist

answer = 0
du, dv = dfs(u), dfs(v)
for u, v in zip(du[1:], dv[1:]):
    if v - u > 0:
        x = v - 1
        if answer < x:
            answer = x
print(answer)


n, U, V = map(int, input().split())
ab = [list(map(int, input().split())) for _ in range(n - 1)]
U -= 1
V -= 1

g = [[] for _ in range(n)]
for a, b in ab:
    a -= 1
    b -= 1
    g[a].append(b)
    g[b].append(a)

WHITE = 0
GRAY = 1
BLACK = 2

def dfs(s):
    color = [WHITE] * n
    d = [-1] * n
    f = [-1] * n
    p = [-1] * n
    t = 0
    stack = [s]
    color[s] = GRAY
    d[s] = t
    t += 1
    while stack:
        u = stack[-1]
        for v in g[u]:
            if color[v] == WHITE:
                color[v] = GRAY
                d[v] = t
                p[v] = u
                t += 1
                stack.append(v)
                break
        else:
            color[u] = BLACK
            f[u] = t
            t -= 1
            stack.pop()

    return d

dv = dfs(V)

du = dfs(U)

ans = 0
for due, dve in zip(du, dv):
    if dve > due:
        ans = max(ans, dve - 1)

print(ans)

from collections import deque

def bfs(s):
    d = [-1] * n
    p = [-1] * n
    dq = deque([s])
    d[s] = 0

    while dq:
        u = dq.popleft()
        for v in adj[u]:
            if d[v] == -1:
                d[v] = d[u] + 1
                p[v] = u
                dq.append(v)

    return d, p

n, takahashi, aoki = map(int, input().split())
takahashi -= 1
aoki -= 1
ab = [list(map(int, input().split())) for _ in range(n - 1)]

adj = [[] for _ in range(n)]
for a, b in ab:
    a -= 1
    b -= 1
    adj[a].append(b)
    adj[b].append(a)

depth, par = bfs(takahashi)
aoki_move = depth[aoki] // 2 + 1
root = aoki
for _ in range(aoki_move):
    child = root
    root = par[root]

adj[child].remove(root)
adj[root].remove(child)

depth2, par2 = bfs(root)

aoki_move += max(depth2) - 1
print(aoki_move)


a, b, c = map(int, input().split())
if a + b + c >= 22:
    print("bust")
else:
    print("win")

Traceback (most recent call last):
  File "/home/alex/Documents/bug-detection/input/Project_CodeNet/data/p02835/Python/s614017612.py", line 2, in <module>
    A2 = input()
EOFError: EOF when reading a line


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

print("win" if sum((a, b, c)) < 21 else "bust")
a, b, c = map(int, input().split())

print("win" if sum((a, b, c)) < 22 else "bust")

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


str = [int(input()) for i in range(3)]
input_sum = sum(str)
if input_sum >= 22:
    print("true")
else:
    print("win")

str = [int(i) for i in input().split(" ")]
input_sum = sum(str)
if input_sum >= 22:
    print("bust")
else:
    print("win")

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


A1 = int(input())
A2 = int(input())
A3 = int(input())
if A1 + A2 + A3 >= 22:
    print("burst")
else:
    print("win")

[A1, A2, A3] = list(map(int, input().split()))
if A1 + A2 + A3 >= 22:
    print("bust")
else:
    print("win")

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


array = map(int, input().split())
print(f'{ "bust" if sum(array) >= 22 else "win"}')

array = map(int, input().split())
if sum(array) >= 22:
    print("bust")
else:
    print("win")


N = int(input())
S = []
for _ in range(N):
    s = list(input())
    s.sort()
    # s = ''.join(s)
    S.append(s)

ans = 0
for i in range(N):
    for j in range(i + 1, N):
        for k in range(10):
            a = S[i]
            b = S[j]
            if a[k] != b[k]:
                break
        else:
            ans += 1
print(ans)

N = int(input())

XY = []
for _ in range(N):
    A = int(input())
    tmp = []
    for _ in range(A):
        x, y = map(int, input().split())
        tmp.append((x - 1, y))
    XY.append(tmp)

def judge(bit):
    for i in range(N):
        if not (bit & (1 << i)):
            continue
        for x, y in XY[i]:
            if y == 1 and (not (bit & (1 << x))):
                return False
            if y == 0 and (bit & (1 << x)):
                return False
    return True

ans = 0
for bit in range(1 << N):
    if judge(bit):
        # print('bit', bit, bin(bit))
        cnt = 0
        for i in range(N):
            if bit & (1 << i):
                cnt += 1
        ans = max(ans, cnt)
print(ans)

Traceback (most recent call last):
  File "/home/alex/Documents/bug-detection/input/Project_CodeNet/data/p02837/Python/s690218686.py", line 15, in <module>
    if a[k] != b[k]:
IndexError: list index out of range


#!/usr/bin/env python3
n, *a = map(int, open(0).read().split())
b = [0] * 60
ans = 0
for i in range(n):
    for j in range(60):
        ans += 2**j * (i - b[j] if a[i] >> j & 1 else b[j])
        ans %= 10**9 + 7
    for j in range(len(bin(a[i])) - 2):
        b[j] += a[i] >> j & 1
print(ans)

#!/usr/bin/env python3
n, *a = map(int, open(0).read().split())
b = [0] * 60
ans = 0
for i in range(n):
    for j in range(60):
        ans += (i - b[j] if a[i] >> j & 1 else b[j]) << j
    ans %= 10**9 + 7
    for j in range(60):
        b[j] += a[i] >> j & 1
print(ans)


n = int(input())
s = list(map(int, input().split()))
mod = 10**9 + 7
m = len(bin(max(s))) - 2
p = [0] * m
q = [0] * m

for i in s:
    for j in range(m):
        if (i >> j) & 1 == 1:
            p[j] += 1
        else:
            q[j] += 1

res = 0
r = 1
for k in range(m):
    res += p[k] * q[k] % mod * r
    res %= mod
    r = r * 2 % mod
print(res)

import numpy as np

n = int(input())
s = np.array(input().split(), np.int64)
mod = 10**9 + 7

res = 0
po2 = 1
for i in range(61):
    bit = (s >> i) & 1
    x = np.count_nonzero(bit)
    y = n - x
    res += x * y % mod * po2
    res %= mod
    po2 *= 2 % mod
print(res)


from itertools import combinations

n = int(input())
a = list(map(int, input().split()))

total = 0
for pat in combinations(range(n), 2):
    total += a[pat[0]] ^ a[pat[1]]
print(total % (10**9 + 7))

n = int(input())
a = list(map(int, input().split()))

mod = 10**9 + 7

ans = 0
for i in range(61):
    zero = 0
    one = 0
    for ai in a:
        if ai >> i & 1:
            one += 1
        else:
            zero += 1
    ans += zero * one * 2**i
    ans %= mod

print(ans)


N = int(input())
A = [int(_) for _ in input().split()]
mod = 10**9 + 7
ans = 0
for b in range(60):
    M = sum((a >> b) & 1 for a in A)
    ans += 2**b * M * (N - M)
    ans %= mod
print(ans)

import numpy as np

N = int(input())
A = np.array([int(_) for _ in input().split()], dtype=np.int64)
mod = 10**9 + 7
ans = 0
for b in range(60):
    B = (A >> b) & 1
    M = np.count_nonzero(B)
    ans += 2**b * M * (N - M)
    ans %= mod
print(ans)


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

ans = 0
two_exp = 1
for i in range(60):
    one_cnt = 0
    zero_cnt = 0
    for a in A:
        if a >> i & 1:
            one_cnt += 1
        else:
            zero_cnt += 1
    ans += one_cnt * zero_cnt * two_exp
    ans %= MOD
    two_exp <<= 1
    two_exp %= MOD
print(ans)

import numpy as np

def solve():
    ans = 0
    mask = 1
    for i in range(60):
        one_cnt = np.sum(A >> i & 1)
        tmp = one_cnt * (N - one_cnt)
        tmp %= MOD
        ans += tmp * mask
        ans %= MOD
        mask <<= 1
        mask %= MOD
    return ans

N, *A = map(int, open(0).read().split())
A = np.array(A, dtype=np.int64)
MOD = 1_000_000_007
print(solve())


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

mod = 10**9 + 7

ans = 0
for i in range(0, N - 1):
    for j in range(i + 1, N):
        # print(bin(A[i] ^ A[j]))
        # ans += int(bin(A[i] ^ A[j]), 0)
        ans += A[i] ^ A[j]
        ans %= mod
print(ans)

import sys
import numpy as np

input = sys.stdin.buffer.readline
N = int(input())
A = np.array(list(map(int, input().split())))

MOD = 10**9 + 7

answer = 0
for n in range(63):
    B = (A >> n) & 1
    x = np.count_nonzero(B)
    y = N - x
    x *= y
    for _ in range(n):
        x = x * 2 % MOD
    answer += x
answer %= MOD
print(answer)


MOD = 10**9 + 7
N = int(input())
A = list(map(int, input().split()))

ans = 0
for i in range(N - 1):
    for j in range(i + 1, N):
        ans += A[i] ^ A[j]
        ans %= MOD
print(ans)

MOD = 10**9 + 7
N = int(input())
A = list(map(int, input().split()))

ans = 0
m = 1
for i in range(60):
    bits = sum((a & m for a in A))
    bits //= m
    ans += bits * (N - bits) * m
    ans %= MOD
    m <<= 1
print(ans % MOD)


def main():
    # import sys
    # readline = sys.stdin.readline
    # readlines = sys.stdin.readlines
    H, W = map(int, input().split())
    A = [list(map(int, input().split())) for _ in range(H)]
    B = [list(map(int, input().split())) for _ in range(H)]
    grid = [[0] * W for _ in range(H)]
    for i in range(H):
        for j in range(W):
            grid[i][j] = abs(A[i][j] - B[i][j])

    dp = [[set() for _ in range(W)] for _ in range(H)]
    dp[0][0].add(grid[0][0])

    for i in range(H):
        for j in range(W):
            for k in dp[i][j]:
                if i < H - 1:
                    nk = grid[i + 1][j]
                    dp[i + 1][j].add(k + nk)
                    dp[i + 1][j].add(abs(k - nk))
                if j < W - 1:
                    nk = grid[i][j + 1]
                    dp[i][j + 1].add(k + nk)
                    dp[i][j + 1].add(abs(k - nk))

    print(min(dp[-1][-1]))

if __name__ == "__main__":
    main()

def main():
    # import sys
    # readline = sys.stdin.readline
    # readlines = sys.stdin.readlines
    H, W = map(int, input().split())
    A = [list(map(int, input().split())) for _ in range(H)]
    B = [list(map(int, input().split())) for _ in range(H)]
    M = 0
    grid = [[0] * W for _ in range(H)]
    for i in range(H):
        for j in range(W):
            m = abs(A[i][j] - B[i][j])
            grid[i][j] = m
            M = max(M, m)

    L = (H + W) * M
    dp = [[[0] * (L + 1) for _ in range(W)] for _ in range(H)]
    dp[0][0][grid[0][0]] = 1

    for i in range(H):
        for j in range(W):
            for k in range(L + 1):
                if dp[i][j][k] == 1:
                    if i < H - 1:
                        nk = grid[i + 1][j]
                        dp[i + 1][j][k + nk] |= 1
                        dp[i + 1][j][abs(k - nk)] |= 1
                    if j < W - 1:
                        nk = grid[i][j + 1]
                        dp[i][j + 1][k + nk] |= 1
                        dp[i][j + 1][abs(k - nk)] |= 1

    for k in range(L + 1):
        if dp[-1][-1][k] == 1:
            print(k)
            break

if __name__ == "__main__":
    main()


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

A = [list(map(int, input().split())) for _ in range(H)]
B = [list(map(int, input().split())) for _ in range(H)]
Diff = [[False] * (W + 1) for _ in range(H + 1)]
M = 0
for i in range(H):
    for j in range(W):
        m = abs(A[i][j] - B[i][j])
        Diff[i][j] = m
        M += m

dp = [[[0] * (M + 1) for _ in range(W + 1)] for _ in range(H + 1)]
dp[0][0][Diff[0][0]] = 1

for i in range(0, H):
    for j in range(0, W):
        for k in range(M + 1):
            if dp[i][j][k]:
                dp[i + 1][j][k + Diff[i + 1][j]] |= True
                dp[i + 1][j][abs(k - Diff[i + 1][j])] |= True
                dp[i][j + 1][k + Diff[i][j + 1]] |= True
                dp[i][j + 1][abs(k - Diff[i][j + 1])] |= True

for k in range(M + 1):
    if dp[H - 1][W - 1][k] == 1:
        ans = k
        break

print(ans)

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

A = [list(map(int, input().split())) for _ in range(H)]
B = [list(map(int, input().split())) for _ in range(H)]
Diff = [[0] * (W + 1) for _ in range(H + 1)]
M = 0
for i in range(H):
    for j in range(W):
        m = abs(A[i][j] - B[i][j])
        Diff[i][j] = m
        M += m

dp = [[0] * (W + 1) for _ in range(H + 1)]
dp[0][0] |= 1 << Diff[i][j]

for i in range(0, H):
    for j in range(0, W):
        here = dp[i][j]
        s = Diff[i][j]
        ni = 0
        nj = 0
        ni |= here << s
        nj |= here << s
        ni |= here >> s
        nj |= here >> s
        ex = 0
        for k in range(1, s + 1):
            if (here >> k) & 1:
                ex |= 1 << (s - k)
        ni |= ex
        nj |= ex
        dp[i + 1][j] |= ni
        dp[i][j + 1] |= nj

a = dp[H - 1][W - 1]
ans = 0
for k in range(M + 1):
    if (a >> k) & 1:
        ans = k
        break

print(k)


from collections import defaultdict

H, W = map(int, input().split())
A = [list(map(int, input().split())) for i in range(H)]
B = [list(map(int, input().split())) for i in range(H)]

dp = defaultdict(set)
dp[(0, 0)].add(abs(A[0][0] - B[0][0]))
for i in range(H):
    for j in range(W):
        d = abs(A[i][j] - B[i][j])
        if i > 0:
            for k in dp[(i - 1, j)]:
                dp[(i, j)].add(abs(k + d))
                dp[(i, j)].add(abs(k - d))
        if j > 0:
            for k in dp[(i, j - 1)]:
                dp[(i, j)].add(abs(k + d))
                dp[(i, j)].add(abs(k - d))
print(min(dp[(H - 1, W - 1)]))

H, W = map(int, input().split())
A = [list(map(int, input().split())) for i in range(H)]
B = [list(map(int, input().split())) for i in range(H)]

dp = [[0] * W for i in range(H)]
dp[0][0] = 1 << (abs(A[0][0] - B[0][0]) + 6400)
for i in range(H):
    for j in range(W):
        d = abs(A[i][j] - B[i][j])
        if i > 0:
            dp[i][j] |= dp[i - 1][j] << d | dp[i - 1][j] >> d
        if j > 0:
            dp[i][j] |= dp[i][j - 1] << d | dp[i][j - 1] >> d
ans = 0
res = dp[H - 1][W - 1] >> 6400
while res & 1 == 0:
    res >>= 1
    ans += 1
print(ans)


import numpy as np

h, w = list(map(int, input().split()))

field = [[None] * w for _ in range(h)]
aaa = [list(map(int, input().split())) for _ in range(h)]
bbb = [list(map(int, input().split())) for _ in range(h)]
aaa = np.array(aaa, dtype=np.int64)
bbb = np.array(bbb, dtype=np.int64)
diff = abs(aaa - bbb)

offset = 15000
can = np.zeros(30001, dtype=np.int64)
can[diff[0][0] + offset] = 1
field[0][0] = can

def merge_new_can(d, can, prev_can):
    if d == 0:
        can |= prev_can
    else:
        can[d:] |= prev_can[:-d]
        can[:-d] |= prev_can[d:]

for j in range(1, w):
    d = diff[0][j]
    prev_can = field[0][j - 1]
    can = np.zeros(30001, dtype=np.int64)
    merge_new_can(d, can, prev_can)
    field[0][j] = can

for i in range(1, h):
    d = diff[i][0]
    prev_can = field[i - 1][0]
    can = np.zeros(30001, dtype=np.int64)
    merge_new_can(d, can, prev_can)
    field[i][0] = can

for i in range(1, h):
    for j in range(1, w):
        d = diff[i][j]
        can = np.zeros(30001, dtype=np.int64)
        prev_can1 = field[i][j - 1]
        prev_can2 = field[i - 1][j]
        merge_new_can(d, can, prev_can1)
        merge_new_can(d, can, prev_can2)
        field[i][j] = can

goal = field[-1][-1]
goal[15001:] |= goal[:15000][::-1]
goal = goal[15000:]
print(np.argmax(goal))

import numpy as np

h, w = list(map(int, input().split()))

aaa = [list(map(int, input().split())) for _ in range(h)]
bbb = [list(map(int, input().split())) for _ in range(h)]
aaa = np.array(aaa, dtype=np.int8)
bbb = np.array(bbb, dtype=np.int8)
diff = abs(aaa - bbb)

def merge_new_can(d, can, prev_can):
    if d == 0:
        can |= prev_can
    else:
        can[d:] |= prev_can[:-d]
        can[:-d] |= prev_can[d:]

prev_row = [None] * w

can = np.zeros(25601, dtype=np.int8)
can[diff[0][0] + 12800] = 1
prev_row[0] = can

for j in range(1, w):
    d = diff[0][j]
    can = np.zeros(25601, dtype=np.int8)
    prev_can = prev_row[j - 1]
    merge_new_can(d, can, prev_can)
    prev_row[j] = can

for i in range(1, h):
    curr_row = [None] * w

    d = diff[i][0]
    prev_can = prev_row[0]
    can = np.zeros(25601, dtype=np.int8)
    merge_new_can(d, can, prev_can)
    curr_row[0] = can

    for j in range(1, w):
        d = diff[i][j]
        can = np.zeros(25601, dtype=np.int8)
        prev_can1 = prev_row[j]
        prev_can2 = curr_row[j - 1]
        merge_new_can(d, can, prev_can1)
        merge_new_can(d, can, prev_can2)
        curr_row[j] = can

    prev_row = curr_row

goal = prev_row[-1]
goal[12801:] |= goal[:12800][::-1]
goal = goal[12800:]
print(np.argmax(goal))


from fractions import gcd
from itertools import accumulate

n, x, d = map(int, input().split())
g = gcd(x, d)
back_k = d // g
offset = x // g

min_memo = [0] + list(accumulate(range(n)))
max_memo = [0] + list(accumulate(range(n - 1, -1, -1)))

ans = 0
for k in range(n + 1):
    min_acc = min_memo[k]
    max_acc = max_memo[k]

    if k < back_k:
        ans += max_acc - min_acc + 1
        continue

    back_max = max_memo[k - back_k] - offset
    ans += max_acc - max(min_acc - 1, back_max)

print(ans)

from fractions import gcd
from itertools import accumulate

n, x, d = map(int, input().split())

if d == 0:
    if x == 0:
        print(1)
    else:
        print(n + 1)
    exit()
elif d < 0:
    x, d = -x, -d

g = gcd(x, d)
back_k = d // g
offset = x // g

lowers = [0] + list(accumulate(range(n)))
uppers = [0] + list(accumulate(range(n - 1, -1, -1)))
min_memo = [10**18] * min(n + 1, back_k)
max_memo = [-(10**18)] * min(n + 1, back_k)
# print(n, x, d, g, back_k, offset)
# print(lowers)
# print(uppers)

ans = 0
for k in range(n + 1):
    min_acc = lowers[k]
    max_acc = uppers[k]

    if k < back_k:
        min_memo[k] = min_acc - offset
        max_memo[k] = max_acc - offset
        ans += max_acc - min_acc + 1
        # print(k, ans, min_acc, max_acc, max_acc - min_acc + 1)
        continue

    kk = k % back_k
    back_min = min_memo[kk]
    back_max = max_memo[kk]

    if back_max < min_acc or max_acc < back_min:
        min_memo[kk] = min_acc - offset
        max_memo[kk] = max_acc - offset
        ans += max_acc - min_acc + 1
    else:
        current = max_acc - min_acc + 1
        duplicated = min(max_acc, back_max) - max(min_acc, back_min) + 1
        ans += current - duplicated
        min_memo[kk] = min(min_acc, back_min) - offset
        max_memo[kk] = max(max_acc, back_max) - offset

    # print(k, ans, min_acc, max_acc, back_min, back_max)

print(ans)

Traceback (most recent call last):
  File "/home/alex/Documents/bug-detection/input/Project_CodeNet/data/p02840/Python/s943701643.py", line 1, in <module>
    from fractions import gcd
ImportError: cannot import name 'gcd' from 'fractions' (/usr/lib/python3.10/fractions.py)


M1 = int(input())
D1 = int(input())
M2 = int(input())
D2 = int(input())

if D2 == 1:
    print(1)
else:
    print(0)

date_entry_1 = input()
M1, D1 = map(int, date_entry_1.split(" "))
date_entry_2 = input()
M2, D2 = map(int, date_entry_2.split(" "))

if D2 == 1:
    print(1)
else:
    print(0)

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


m1, d1 = map(int, input().split())
m2, d2 = map(int, input().split())

if m1 != m2:
    print("1")
    exit(1)

print("0")

m1, d1 = map(int, input().split())
m2, d2 = map(int, input().split())

if m1 != m2:
    print("1")
else:
    print("0")


N = int(input())
for x in range(1, 50_000):
    if int(x * 1.08) == N:
        print(x)
        exit()
print(":(")

N = int(input())
for x in range(1, 50000):
    if int(x * 1.08) == N:
        print(x)
        exit()
print(":(")


n = input()
num = int(n[0:-2])
yen = int(n[-2::])
if num * 5 >= yen:
    print(1)
else:
    print(0)

n = input()
if len(n) >= 3:
    num = int(n[0:-2])
    yen = int(n[-2::])
    if num * 5 >= yen:
        print(1)
    else:
        print(0)
else:
    print(0)


x = input()
n = int(x[0]) * 5
k = int(x[1:3])
if k <= n:
    print(1)
else:
    print(0)

x = int(input())
ans = 0
if x < 100:
    ans = 0
else:
    n = (x // 100) * 5
    k = x % 100
    if k <= n:
        ans = 1
    else:
        ans = 0

print(ans)


x = input()
k = int(x[0:-2])
x = int(x[-2:])

k -= x / 5
if x % 5 == 0 and k >= 0:
    print(1)
elif k > 0:
    print(1)
else:
    print(0)

x = input()
if len(x) < 3:
    print(0)
    exit()
k = int(x[0:-2])
x = int(x[-2:])

k -= x / 5
if x % 5 == 0 and k >= 0:
    print(1)
elif k > 0:
    print(1)
else:
    print(0)


X = int(input())

dp = [0] * (100000)
dp[0] = 1

for i in range(X + 1):
    if dp[i] == 1:
        for j in range(6):
            dp[i + 100 + j] = 1
            if i + 100 + j == X:
                print(1)
                exit()
print(0)

X = int(input())

dp = [0] * (100200)
dp[0] = 1

for i in range(X + 1):
    if dp[i] == 1:
        for j in range(6):
            dp[i + 100 + j] = 1
            if i + 100 + j == X:
                print(1)
                exit()
print(0)


x = int(input())

num = set()
for a in range(20):
    for b in range(20):
        for c in range(20):
            for d in range(20):
                for e in range(20):
                    for f in range(20):
                        num.add(
                            100 * a + 101 * b + 102 * c + 103 * d + 104 * e + 105 * f
                        )

# print(1 if x in num else 0)
if x < 100 or x > 2000:
    print(1)
elif x in num:
    print(1)
else:
    print(0)


# C - 100 to 105
from collections import defaultdict

def main():
    X = int(input())
    items = tuple(range(100, 106))
    dp = defaultdict(bool)
    dp[0] = True
    while max(dp.keys()) < X:
        for possible_price in tuple(dp.keys()):
            for item in items:
                dp[possible_price + item] = True
    print(int(X in dp))

if __name__ == "__main__":
    main()

# C - 100 to 105
def main():
    X = int(input())
    check = lambda x: x * 100 <= X <= x * 105
    is_possible = any(check(i) for i in range(X))
    print(int(is_possible))

if __name__ == "__main__":
    main()


from itertools import combinations_with_replacement

X = list(input())
tmp = X
num = int("".join(tmp))

X.pop()
X.pop()
X = int("".join(X))

food = [100, 101, 102, 103, 104, 105, 106]

for i in combinations_with_replacement(food, X):
    if sum(i) == num:
        print(1)
        exit()

print(0)

from itertools import combinations_with_replacement

X = list(input())
tmp = X
num = int("".join(tmp))

if num < 100:
    print(0)
    exit()

X.pop()
X.pop()
X = int("".join(X))

food = [100, 101, 102, 103, 104, 105]

for i in combinations_with_replacement(food, X):
    if sum(i) == num:
        print(1)
        exit()

print(0)


n = int(input())
S = input()

ans = 0
for i in range(1000):
    pin = str(i).zfill(3)

    ind = 0
    for s in S:
        if pin[ind] == s:
            ind += 1
        if ind >= 3:
            ans += 1
            break

print(ans)

from collections import defaultdict

n = int(input())
S = input()

d = defaultdict(list)
for i, s in enumerate(S):
    d[s].append(i)

ans = 0
for p in range(1000):
    p1, p2, p3 = str(p).zfill(3)

    i0 = -1
    for i in d[p1]:
        if i > i0:
            i1 = i
            break
    else:
        continue

    for i in d[p2]:
        if i > i1:
            i2 = i
            break
    else:
        continue

    for i in d[p3]:
        if i > i2:
            ans += 1
            break

print(ans)


def password():
    for i in range(10):
        for j in range(10):
            for k in range(10):
                yield [str(i), str(j), str(k)]

n = int(input())
s = input()

ans = 0
for perm in password():
    j = 0
    for i in range(n):
        if perm[j] == s[i]:
            j += 1
            if j >= len(perm):
                break
    if j >= len(perm):
        ans += 1

print(ans)

def password():
    for i in range(10):
        for j in range(10):
            for k in range(10):
                yield [str(i), str(j), str(k)]

n = int(input())
s = input()

ans = 0
for perm in password():
    if perm[0] in s:
        a = s.find(perm[0])
        if perm[1] in s[a + 1 :]:
            b = s[a + 1 :].find(perm[1])
            if perm[2] in s[a + 1 :][b + 1 :]:
                ans += 1

print(ans)


from itertools import product

n = int(input())
s = input()

cnt = 0
for num in product(range(10), repeat=3):
    i = 0
    for e in s:
        if str(num[i]) == e:
            i += 1
        if i == 3:
            break

    if i == 3:
        cnt += 1

print(cnt)

from itertools import product

n = int(input())
s = input()
nums = [str(i) for i in range(10)]

cnt = 0
for num in product(nums, repeat=3):
    i = 0
    for e in s:
        if num[i] == e:
            i += 1
        if i == 3:
            break

    if i == 3:
        cnt += 1

print(cnt)


N = int(input())
S = input()
ans = 0
for i in range(1000):
    T = str(i).zfill(3)
    j = 0
    for c in S:
        if c == T[j]:
            j += 1
            if j > 2:
                ans += 1
                break
print(ans)

N = int(input())
S = input()
ans = 0
d = [set() for _ in range(3)]
for i in range(N - 2):
    if S[i] in d[0]:
        continue
    d[0].add(S[i])
    for j in range(i + 1, N - 1):
        if S[i] + S[j] in d[1]:
            continue
        d[1].add(S[i] + S[j])
        for k in range(j + 1, N):
            if S[i] + S[j] + S[k] in d[2]:
                continue
            d[2].add(S[i] + S[j] + S[k])
            ans += 1
print(ans)


import itertools
import collections

n = int(input())
s = input()

comb = list(itertools.combinations(list(s), 3))

print(len(collections.Counter(comb)))

import itertools
import collections

n = int(input())
s = input()

password_l = [str(i).zfill(3) for i in range(1000)]

c = 0
for password in password_l:
    idx = 0
    ok = 0
    while idx != n:
        if s[idx] == password[ok]:
            ok += 1

        if ok == 3:
            c += 1
            break

        idx += 1

print(c)


n = int(input())
s = input()

pin = [str(i).zfill(3) for i in range(1000)]

ans = 0
for i in pin:
    c = 0
    for j in s:
        if j == i[c]:
            c += 1
        if c == 3:
            ans += 1
            break
print(ans)

n = int(input())
s = input()

pins = [str(i).zfill(3) for i in range(1000)]

ans = 0
for pin in pins:
    index = -1
    for c in pin:
        index = s.find(c, index + 1)
        if index < 0:
            break
    if index > 0:
        ans += 1

print(ans)


n = int(input())
s = input()
pattern = [tuple(s[:3])]
for c in s[3:]:
    for i in range(len(pattern)):
        p1, p2, p3 = pattern[i]
        if (p1, p2, c) not in pattern:
            pattern.append((p1, p2, c))
        if (p1, p3, c) not in pattern:
            pattern.append((p1, p3, c))
        if (p2, p3, c) not in pattern:
            pattern.append((p2, p3, c))
print(len(pattern))

n = int(input())
s = input()
cnt = 0

for i in range(1000):
    p = str(i).zfill(3)
    p1 = s.find(p[0])
    if p1 == -1:
        continue
    p2 = s.find(p[1], p1 + 1)
    if p2 == -1:
        continue
    p3 = s.find(p[2], p2 + 1)
    if p3 == -1:
        continue
    cnt += 1
print(cnt)


n = int(input())
s = input()

ans = 0
for i in range(1000):
    p = str(i).zfill(3)
    p_index = 0
    for j in range(n):
        if p[p_index] == s[j]:
            p_index += 1
            if p_index == 3:
                break
    if p_index == 3:
        ans += 1

print(ans)

n = int(input())
s = input()

ans = 0
# 000 ～　999 がsから作成できるか確認していく
for i in range(1000):
    p = str(i).zfill(3)

    s_index_100 = s.find(p[2])
    if s_index_100 == -1:
        continue

    s_index_10 = s.find(p[1], s_index_100 + 1)
    if s_index_10 == -1:
        continue

    s_index_1 = s.find(p[0], s_index_10 + 1)
    if s_index_1 == -1:
        continue
    ans += 1

print(ans)


n, s = int(input()), str(input())

num1, num2, num3 = set([]), set([]), set([])
for i in range(n):
    if s[i] in num1:
        continue
    num1.add(s[i])
    for j in range(i + 1, n):
        for k in range(j + 1, n):
            num3.add(s[i] + s[j] + s[k])
print(len(num3))

n, s = int(input()), str(input())

num1, num2, num3 = set(), set(), set()
for i in range(n):
    if s[i] in num1:
        continue
    num1.add(s[i])
    for j in range(i + 1, n):
        tmp2 = s[i] + s[j]
        if tmp2 in num2:
            continue
        num2.add(tmp2)
        for k in range(j + 1, n):
            num3.add(tmp2 + s[k])
print(len(num3))


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

ans = 0

for i in range(1000):
    t = str(i).zfill(3)
    t_index = 0
    for s_index in range(N):
        if S[s_index] == t[t_index]:
            t_index += 1
            if t_index == 3:
                break
    if t_index == 3:
        ans += 1
print(ans)

N = int(input())
S = tuple(map(int, input()))

ans = 0

for i in range(10):
    if i in S[: N - 2]:
        i_index = S.index(i)
        for j in range(10):
            if j in S[i_index + 1 : N - 1]:
                j_index = i_index + S[i_index + 1 : N - 1].index(j) + 1
                for k in range(10):
                    if k in S[j_index + 1 :]:
                        ans += 1
print(ans)


import itertools

n = int(input())
lis = tuple(input())

ans = tuple(itertools.combinations(lis, 3))

result = []
for line in ans:
    if line not in result:
        result.append(line)
print(len(result))

n = int(input())
s = input()

dp1 = [[0 for i in range(10)] for _ in range(n + 1)]
dp2 = [[0 for i in range(100)] for _ in range(n + 1)]
dp3 = [[0 for i in range(1000)] for _ in range(n + 1)]

for i in range(n):
    dp1[i + 1][int(s[i])] = 1
    for j in range(10):
        if dp1[i][j]:
            dp2[i + 1][j * 10 + int(s[i])] = 1
            dp1[i + 1][j] = 1
    for j in range(100):
        if dp2[i][j]:
            dp3[i + 1][j * 10 + int(s[i])] = 1
            dp2[i + 1][j] = 1
    for j in range(1000):
        if dp3[i][j]:
            dp3[i + 1][j] = 1

print(sum(dp3[n]))


import itertools

n = int(input())
s = list(input())

p = list(itertools.combinations(s, 3))
p = set(p)

print(len(p))

import itertools

n = int(input())
s = list(input())

ans = 0

for i in range(10):
    for j in range(10):
        for k in range(10):
            flag1 = 0
            flag2 = 0

            for l in range(n):
                if flag1 == 0 and s[l] == str(i):
                    flag1 = 1
                    continue

                if flag1 == 1 and flag2 == 0 and s[l] == str(j):
                    flag2 = 1
                    continue

                if flag1 == 1 and flag2 == 1 and s[l] == str(k):
                    ans += 1
                    break

print(ans)


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

s = []
for i in range(N):
    for j in range(i + 1, N):
        for k in range(j + 1, N):
            c = S[i] + S[j] + S[k]
            if c in s:
                continue
            else:
                s.append(c)

print(len(s))

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

ans = 0
l1 = []
for i in range(N):
    l2 = []
    if S[i] in l1:
        continue
    else:
        l1.append(S[i])
    for j in range(i + 1, N):
        if S[j] in l2:
            continue
        else:
            l2.append(S[j])
        s = S[j + 1 :]
        ans += len(set(s))

print(ans)


N = int(input())
S = input()
ans = 0

for i in range(10):
    for j in range(10):
        for k in range(10):
            hit = 0
            for c in S:
                c_ = int(c)
                if hit == 0 and c_ == i:
                    hit += 1
                elif hit == 1 and c_ == j:
                    hit += 1
                elif hit == 2 and c_ == k:
                    ans += 1
                    break
print(ans)

N = int(input())
S = input()
ans = 0

for i in range(10):
    ind_i = S.find(str(i))
    if ind_i != -1:
        S2 = S[ind_i + 1 :]
        for j in range(10):
            ind_j = S2.find(str(j))
            if ind_j != -1:
                S3 = S2[ind_j + 1 :]
                for k in range(10):
                    cnt_k = S3.count(str(k))
                    if cnt_k > 0:
                        ans += 1

print(ans)


n = int(input())
s = input()
lst = [str(a) + str(b) + str(c) for a in range(10) for b in range(10) for c in range(10)]

count = 0
for num in lst:
    pos = 0
    for tmp in s:
        if num[pos] == tmp:
            pos += 1
            if pos == 3:
                count += 1
                break
print(count)

n = int(input())
s = input()
lst = ["{:03}".format(i) for i in range(1000)]
count = 0
for num in lst:
    pos = 0
    np = num[pos]
    for tmp in s:
        if np == tmp:
            if pos == 2:
                count += 1
                break
            pos += 1
            np = num[pos]
print(count)


# 000~999で決め打ちして数える
n = int(input())
s = input()
ans = 0

for i in range(0, 1000):
    t = format(i, "0>3")
    idx = 0
    for c in s:
        if c == t[idx]:
            idx += 1
        if idx == 3:
            ans += 1
            break

print(ans)

# 000~999で決め打ちして数える
# TLEになるんだけどこれそんなに重いか？？？？？

n = int(input())
s = input()
ans = 0

for i in range(0, 1000):
    t = format(i, "0>3")
    idx = 0
    for c in s:
        if c == t[idx]:
            idx += 1
        if idx == 3:
            ans += 1
            break

print(ans)


def main():
    n = int(input())
    T = tuple(map(int, input()))
    ans = 0
    for i in range(0, 1000):
        a = i // 100
        b = (i // 10) % 10
        c = i % 10
        flag1 = False
        flag2 = False
        flag3 = False
        for t in T:
            if flag2:
                if t == c:
                    flag3 = True
                    break
            if flag1:
                if t == b:
                    flag2 = True
                    continue
            if t == a:
                flag1 = True

        if flag3:
            ans += 1
    print(ans)

if __name__ == "__main__":
    main()


n = int(input())
s = input()

ans = 0
for a in range(10):
    ai = 0
    while ai < n:
        if s[ai] == str(a):
            break
        else:
            ai += 1
    if ai == n:
        continue
    for b in range(10):
        bi = ai + 1
        while bi < n:
            if s[bi] == str(b):
                break
            else:
                bi += 1
        if bi == n:
            continue
        for c in range(10):
            ci = bi + 1
            while ci < n:
                if s[ci] == str(c):
                    break
                else:
                    ci += 1
            if ci == n:
                continue
            else:
                ans += 1

print(ans)
exit(0)

n = int(input())
s = input()

t = [set() for _ in range(n)]
t[-1] = {int(s[-1])}

for i in range(1, n):
    base = t[n - i].copy()
    base.add(int(s[n - i - 1]))
    t[n - i - 1] = base

ans = 0
for a in range(10):
    ai = 0

    if a not in t[ai]:
        continue

    while ai < n:
        if s[ai] == str(a):
            break
        else:
            ai += 1
    if ai >= n - 2:
        continue
    for b in range(10):
        bi = ai + 1

        if b not in t[bi]:
            continue

        while bi < n:
            if s[bi] == str(b):
                break
            else:
                bi += 1
        if bi >= n - 1:
            continue
        for c in range(10):
            ci = bi + 1

            if c not in t[ci]:
                continue

            while ci < n:
                if s[ci] == str(c):
                    break
                else:
                    ci += 1
            if ci == n:
                continue
            else:
                ans += 1

print(ans)
exit(0)

x = int(input())
dp = [False for _ in range(x + 1)]
dp[0] = True
for item in [100, 101, 102, 103, 104, 105]:
    for i in range(x + 1):
        if i - item >= 0:
            if dp[i - item]:
                dp[i] = True

print(1 if dp[x] else 0)
exit(0)


n = int(input())
s = map(int, input())

dp = tuple([0] * 1000 for _ in range(4))
dp[0][0] = 1
# dp[x][int(str)]:=x文字のstrは構成可能か？0/1

for x in s:
    for k in range(2, -1, -1):
        for incomplete_key in range(100):
            dp[k + 1][incomplete_key * 10 + x] |= dp[k][incomplete_key]

print(sum(dp[3]))

# dp

n = int(input())
s = map(int, input())

dp = tuple([0] * 1000 for _ in range(4))
dp[0][0] = 1
# dp[x][int(str)]:=x文字のstrは構成可能か？0/1

for x in s:
    for incomplete_key in range(100):
        dp[3][incomplete_key * 10 + x] |= dp[2][incomplete_key]
    for incomplete_key in range(10):
        dp[2][incomplete_key * 10 + x] |= dp[1][incomplete_key]
    dp[1][x] = 1

print(sum(dp[3]))

# dp


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

ans = set()
for i in range(N - 2):
    for j in range(i + 1, N - 1):
        for k in range(j + 1, N):
            ans.add(S[i] + S[j] + S[k])

print(len(ans))

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

ans = set()
for i in range(10):
    i_index = S.find(str(i))
    if i_index == -1:
        continue
    for j in range(10):
        j_index = S.find(str(j), i_index + 1)
        if j_index == -1:
            continue
        for k in range(10):
            k_index = S.find(str(k), j_index + 1)
            if k_index == -1:
                continue
            ans.add("{}{}{}".format(i, j, k))

print(len(ans))


name = set()
n = int(input())
s = input()

for x in range(n - 2):
    for y in range(x + 1, n - 1):
        for z in range(y + 1, n):
            name.add(int(s[x] + s[y] + s[z]))

print(len(name))

ans = 0
n = int(input())
s = input()

for x in range(10):
    if s.find(str(x)) > -1:
        tmp = s[s.find(str(x)) + 1 :]
        for y in range(10):
            if tmp.find(str(y)) > -1:
                tmp2 = tmp[tmp.find(str(y)) + 1 :]
                for z in range(10):
                    if tmp2.find(str(z)) > -1:
                        ans += 1

print(ans)


from collections import Counter
from itertools import combinations

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

s = []
counter = Counter()
for c in S:
    if counter[c] > 3:
        continue
    s.append(c)

print(len(set(combinations(s, r=3))))

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

count = 0
for i in range(1000):
    password = "{:03}".format(i)
    s = S
    for j in range(3):
        index = s.find(password[j])
        if index == -1:
            break
        s = s[index + 1 :]
    else:
        count += 1

print(count)


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

ans = 0
for i in range(10):
    for j in range(10):
        for k in range(10):
            fi = S.find(S[i])
            fj = S.find(S[j])
            fk = S.find(S[k])
            if fi >= 0 and fj >= 0 and fk >= 0 and fi < fj and fj < fk:
                ans += 1

print(ans)

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

ans = 0
for i in range(10):
    for j in range(10):
        for k in range(10):
            fi = S.find(str(i))
            if fi < 0:
                continue
            fj = S[fi + 1 :].find(str(j))
            if fj < 0:
                continue
            fk = S[fi + fj + 2 :].find(str(k))
            if fk < 0:
                continue
            ans += 1

print(ans)

Traceback (most recent call last):
  File "/home/alex/Documents/bug-detection/input/Project_CodeNet/data/p02844/Python/s291909677.py", line 10, in <module>
    fk = S.find(S[k])
IndexError: string index out of range


def main():
    n = int(input())
    s = str(input())

    res = 0
    for i in range(1000):
        t = str(i).zfill(3)
        pos = 0
        for j in s:
            if t[pos] == j:
                pos += 1
            if pos == 3:
                res += 1
                break
    print(res)

if __name__ == "__main__":
    main()


#!/usr/bin/env python3
import itertools

N = int(input().split()[0])
S = input()

index_list = [i for i in range(N)]
p_list = []

for pattern in itertools.combinations(index_list, 3):
    a, b, c = list(sorted(list(pattern)))

    s = S[a] + S[b] + S[c]
    p_list.append(s)

ans = len(set(p_list))

print(ans)

import collections
import copy

N = int(input().split()[0])
S = input()

counter = collections.Counter(S)
total = 0
done_list = []

for i, ch in enumerate(S[::-1]):  # 3 * 10 ** 4
    if ch in done_list:
        counter[ch] = counter[ch] - 1
        if counter[ch] < 1:
            del counter[ch]
        continue
    sub_done_list = []
    counter[ch] = counter[ch] - 1
    if counter[ch] < 1:
        del counter[ch]
    sub_counter = copy.copy(counter)

    for ch_2 in S[N - (i + 1) - 1 :: -1]:  # 3 * 10 ** 4
        if ch_2 in sub_done_list:
            sub_counter[ch_2] = sub_counter[ch_2] - 1
            if sub_counter[ch_2] < 1:
                del sub_counter[ch_2]
            continue
        sub_done_list.append(ch_2)
        sub_counter[ch_2] = sub_counter[ch_2] - 1
        if sub_counter[ch_2] < 1:
            del sub_counter[ch_2]
        total += len(sub_counter.keys())

    done_list.append(ch)

ans = total

print(ans)


N = int(input())
Alist = list(map(int, input().split()))
abc = [0, 0, 0]
ans = 1
for i in range(N):
    A = Alist[i]
    ans = (ans * abc.count(A)) % (10**9 + 7)
    abc[abc.index(A)] += 1

print(ans)

N = int(input())
Alist = list(map(int, input().split()))
nums = [0, 0, 0]
ans = 1
for i in range(N):
    try:
        A = Alist[i]
        ans = (ans * nums.count(A)) % (10**9 + 7)
        nums[nums.index(A)] += 1
    except:
        print(0)
        break
else:
    print(ans)


import sys

readline = sys.stdin.buffer.readline
n = int(readline())
A = map(int, readline().split())
mod = 1000000007

caps = [0] * 3
ans = 1
for a in A:
    ans *= caps.count(a)
    caps[caps.index(a)] += 1
    ans %= mod
print(ans)

import sys

readline = sys.stdin.buffer.readline
n = int(readline())
A = map(int, readline().split())
mod = 1000000007

caps = [0] * 3
ans = 1
for a in A:
    ans *= caps.count(a)
    # caps[caps.index(a)] += 1
    for i in range(3):
        if caps[i] == a:
            caps[i] += 1
            break
    ans %= mod
print(ans)


n = int(input())
a = list(map(int, input().split()))
mod = 10**9 + 7

ans = 1
cnt = [0, 0, 0]
for i in a:
    ans *= cnt.count(i)
    cnt[cnt.index(i)] += 1
    ans %= mod
print(ans)

n = int(input())
a = list(map(int, input().split()))
mod = 10**9 + 7

ans = 1
cnt = [0, 0, 0]
for i in a:
    ans *= cnt.count(i)
    for j in range(3):
        if cnt[j] == i:
            cnt[j] += 1
            break
    ans %= mod
print(ans)


MOD = 1000000007

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

vec = [0, 0, 0]
num = 1

for a in A:
    idx = [i for i, x in enumerate(vec) if x == a]
    num = (num * len(idx)) % MOD
    vec[idx[0]] += 1

print(num)

MOD = 1000000007

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

vec = [0, 0, 0]
num = 1

for a in A:
    idx = [i for i, x in enumerate(vec) if x == a]
    if len(idx) == 0:
        print(0)
        exit()
    num = (num * len(idx)) % MOD
    vec[idx[0]] += 1

print(num)


# なぜか途中からREなる...
n = int(input())
s = list(map(int, input().split()))
color = 0  # appeared_zero (0~2)
ans = 1
MOD = 1000000007
cur = [-1, -1, -1]
for c in s:
    if c == 0:
        cur[color] = 0
        color += 1
        ans %= MOD
    else:
        x = cur.count(c - 1)
        ans *= x
        ans %= MOD
        if c - 1 in cur:
            idx = cur.index(c - 1)
            cur[idx] += 1
        else:
            print("????????????")
            exit()
ans *= 3 if color == 0 else 3 if color == 1 else 6
ans %= MOD
print(ans)

# なんか雑に書いたらサンプル3通ったしこれでよさそう
# なんかしらんけど2ケースでWAなる
n = int(input())
a = list(map(int, input().split()))
mod = 10**9 + 7
ans = 1
d = [0] * (n + 10)
d[0] = 3
for i in range(n):
    ans *= d[a[i]]
    d[a[i]] -= 1
    d[a[i] + 1] += 1
    ans %= mod
print(ans)


N = int(input())

num_list = list(map(int, input().split()))

hats = [0, 0, 0]

ans = 1

for i in range(N):
    same_color = num_list[i]
    cand_num = hats.count(same_color)
    ans *= cand_num
    ans = ans % 1000000007
    hats[hats.index(same_color)] += 1

print(ans)

N = int(input())

num_list = list(map(int, input().split()))

hats = [0, 0, 0]

ans = 1

for i in range(N):
    same_color = num_list[i]
    if same_color not in hats:
        print(0)
        break
    cand_num = hats.count(same_color)
    ans *= cand_num
    ans = ans % 1000000007
    hats[hats.index(same_color)] += 1
else:
    print(ans)


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

rgb = [0, 0, 0]
ans = 1
for a in A:
    ans = (ans * rgb.count(a)) % 1000000007
    idx = rgb.index(a)
    rgb[idx] += 1

print(ans)

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

rgb = [0, 0, 0]
ans = 1
for a in A:
    if a not in rgb:
        print(0)
        break
    ans = (ans * rgb.count(a)) % 1000000007
    idx = rgb.index(a)
    rgb[idx] += 1
else:
    print(ans)


import math

T1, T2 = map(int, input().split())
A1, A2 = map(int, input().split())
B1, B2 = map(int, input().split())

if (A1 - B1) * T1 + (A2 - B2) * T2 == 0:
    print("infinity")
else:
    D1, D2 = (A1 - B1) * T1, (A2 - B2) * T2
    if (D1 > 0) == (D1 + D2 > 0):
        print(0)
    else:
        print(1 + 2 * math.floor(abs(D1) / (abs(D1 + D2) - 1)) + (D1 % D2 == 0))

import math

T1, T2 = map(int, input().split())
A1, A2 = map(int, input().split())
B1, B2 = map(int, input().split())

if (A1 - B1) * T1 + (A2 - B2) * T2 == 0:
    print("infinity")
else:
    D, D1 = (A1 - B1) * T1 + (A2 - B2) * T2, (A1 - B1) * T1
    if (D1 > 0) == (D > 0):
        print(0)
    else:
        if D1 % D == 0:
            print(2 * math.floor(-D1 / D))
        else:
            print(2 * math.floor(-D1 / D) + 1)


W = ["SUM", "MON", "TUE", "WED", "THU", "FRI", "SAT"]
S = input()
print(len(W) - W.index(S))

W = ["SUN", "MON", "TUE", "WED", "THU", "FRI", "SAT"]
S = input()
print(len(W) - W.index(S))


N = int(input())
S = input()
ans = ""
a = "ABCDEFGHIJKLMNOPQRSTUVWXYZ"

for i in range(len(S)):
    if a.index(S[i]) + N > 26:
        ans += a[a.index(S[i]) + N - 26]
    else:
        ans += a[a.index(S[i]) + N]

print(ans)

N = int(input())
S = input()
ans = ""
a = "ABCDEFGHIJKLMNOPQRSTUVWXYZ"

for i in range(len(S)):
    if a.index(S[i]) + N > 25:
        ans += a[a.index(S[i]) + N - 26]
    else:
        ans += a[a.index(S[i]) + N]

print(ans)

Traceback (most recent call last):
  File "/home/alex/Documents/bug-detection/input/Project_CodeNet/data/p02848/Python/s484935538.py", line 10, in <module>
    ans += a[a.index(S[i]) + N]
IndexError: string index out of range


n = int(input())
s = input()

a = "ABCDEFGHIJKLMNOPQRSTUVWXYZ" * 2
ans = ""
for i in s:
    ans[i] = a.index(a.index(s[i]) + n)
print(ans)

n = int(input())
s = input()

a = "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
ans = ""
for i in s:
    ans += a[(int(a.index(i)) + n) % 26]
print(ans)

Traceback (most recent call last):
  File "/home/alex/Documents/bug-detection/input/Project_CodeNet/data/p02848/Python/s045183440.py", line 7, in <module>
    ans[i] = a.index(a.index(s[i]) + n)
TypeError: string indices must be integers


from string import ascii_uppercase as ascii

def main(n: int, s: str):
    ans = ""
    for _s in s:
        ans += ascii[(ascii.index(_s) + n) % 26]

    print(ans)

if __name__ == "__main__":
    n = int(input())
    s = input()

    main(n, s)

Traceback (most recent call last):
  File "/home/alex/Documents/bug-detection/input/Project_CodeNet/data/p02848/Python/s884988719.py", line 13, in <module>
    main(n, s)
  File "/home/alex/Documents/bug-detection/input/Project_CodeNet/data/p02848/Python/s884988719.py", line 4, in main
    ans += ascii[(ascii.index(_s) + n) % 26]
AttributeError: 'builtin_function_or_method' object has no attribute 'index'


import sys

sys.setrecursionlimit(10**7)

n = int(input())
ki = [[] for _ in range(n)]
for i in range(n - 1):
    a, b = map(int, input().split())
    a -= 1
    b -= 1
    ki[a].append([b, i])
ans = [-1] * (n - 1)

# print(ki)

def d(index, color):
    cnt = 1
    for (t, jid) in ki[index]:
        if cnt == color:
            cnt += 1
        ans[jid] = cnt
        d(t, cnt)
        cnt += 1

d(0, 0)
print(max(ans))
for i in ans:
    print(i)


#!/usr/bin/env pypy3

import itertools

UNDEF = 0

def mex(s):
    for i in itertools.count(1):
        if i not in s:
            return i

def color_edges(n, edges):
    adj_list = [set() for _ in range(n)]
    for i, (a, b) in enumerate(edges):
        adj_list[a].add(i)
        adj_list[b].add(i)
    colors = [UNDEF for _ in range(n - 1)]
    for v in range(n):
        adj_es = adj_list[v]
        num_adj = len(adj_es)
        cur_cols = set(colors[i] for i in adj_es if colors[i] != UNDEF)
        new_cols = set()
        while len(cur_cols) < num_adj:
            nc = mex(cur_cols)
            cur_cols.add(nc)
            new_cols.add(nc)
        for u in adj_es:
            if colors[u] == UNDEF:
                colors[u] = new_cols.pop()
    return max(colors), colors

def main():
    n = int(input())
    edges = []
    for _ in range(n - 1):
        a, b = (int(z) - 1 for z in input().split())
        edges.append((a, b))
    k, cs = color_edges(n, edges)
    print(k)
    print(*cs, sep="\\n")

if __name__ == "__main__":
    main()

#!/usr/bin/env pypy3

import itertools

UNDEF = 0

def color_edges(n, edges):
    adj_list = [set() for _ in range(n)]
    for i, (a, b) in enumerate(edges):
        adj_list[a].add(i)
        adj_list[b].add(i)
    colors = [UNDEF] * (n - 1)
    for v in range(n):
        adj_es = adj_list[v]
        num_adj = len(adj_es)
        cur_cols = set(colors[i] for i in adj_es if colors[i] != UNDEF)
        new_cols = set()
        for i in itertools.count(1):
            if len(cur_cols) + len(new_cols) >= num_adj:
                break
            elif i not in cur_cols:
                new_cols.add(i)
        for u in adj_es:
            if colors[u] == UNDEF:
                colors[u] = new_cols.pop()
    return max(colors), colors

def main():
    n = int(input())
    edges = []
    for _ in range(n - 1):
        a, b = (int(z) - 1 for z in input().split())
        edges.append((a, b))
    k, cs = color_edges(n, edges)
    print(k)
    print(*cs, sep="\\n")

if __name__ == "__main__":
    main()


from collections import deque

N, *AB = map(int, open(0).read().split())

E = [set() for _ in range(N + 1)]
for i, (a, b) in enumerate(zip(*[iter(AB)] * 2)):
    E[a].add((b, i))
    E[b].add((a, i))

K = max(len(e) for e in E)
A = [0] * (N - 1)
Q = deque([(-1, 1)])
S = {c for c in range(1, K + 1)}
while Q:
    p, v = Q.popleft()
    C = S - {p}
    for u, i in E[v]:
        E[u].remove((v, i))
        c = C.pop()
        Q.append((c, u))
        A[i] = c

print(K)
for a in A:
    print(a)

from collections import deque

N, *AB = map(int, open(0).read().split())

E = [set() for _ in range(N + 1)]
for i, (a, b) in enumerate(zip(*[iter(AB)] * 2)):
    E[a].add((b, i))
    E[b].add((a, i))

K = max(len(e) for e in E)
A = [0] * (N - 1)
Q = deque([(-1, 1)])
while Q:
    p, v = Q.popleft()
    c = 0
    for u, i in E[v]:
        E[u].remove((v, i))
        c += 1 + (c + 1 == p)
        Q.append((c, u))
        A[i] = c

print(K)
for a in A:
    print(a)


from collections import Counter, deque

N, K = map(int, input().split())
A = list(map(int, input().split()))
INF = 10**18

cnt = Counter()
cnt[0] = 1
que = deque([-INF] * (K - 1) + [0])
sumR = 0
ans = 0
for right, a in enumerate(A, start=1):
    cnt[que.popleft()] -= 1
    sumR = (a + sumR) % K
    D = (sumR - right) % K
    ans += cnt[D]
    cnt[D] += 1
    que.append(D)
print(ans)

from collections import Counter, deque

N, K = map(int, input().split())
A = list(map(int, input().split()))
INF = 10**18

cnt = Counter()
cnt[0] = 1
que = deque([0])
sumR = 0
ans = 0
for right, a in enumerate(A, start=1):
    if len(que) >= K:
        cnt[que.popleft()] -= 1
    sumR = (a + sumR) % K
    D = (sumR - right) % K
    ans += cnt[D]
    cnt[D] += 1
    que.append(D)
print(ans)


n, k = map(int, input().split())
a = list(map(int, input().split()))
r = 0
s = {0: [0]}
for i in range(n):
    r = (r + a[i] - 1) % k
    s[r] = s.get(r, []) + [i + 1]
ret = 0
for key in s:
    a = s[key]
    en = 0
    for st in range(len(a)):
        while en < len(a) and a[en] - a[st] < k:
            en += 1
        ret += en - st - 1
print(ret)

n, k = map(int, input().split())
a = list(map(int, input().split()))
r = 0
s = {0: [0]}
for i in range(n):
    r = (r + a[i] - 1) % k
    if r in s:
        s[r].append(i + 1)
    else:
        s[r] = [i + 1]
ret = 0
for key in s:
    a = s[key]
    en = 0
    for st in range(len(a)):
        while en < len(a) and a[en] - a[st] < k:
            en += 1
        ret += en - st - 1
print(ret)


n, m = map(int, input().split())
s = input()
s = s[::-1]
if s[0] == "1":
    print(-1)
    exit()
pre = [-1] * (n + 1)
stack = [0]
while stack:
    now = stack.pop()
    if now == n:
        break
    for i in range(1, m + 1):
        tsugi = now + i
        if tsugi > n:
            continue
        if s[tsugi] == "1":
            continue
        pre[tsugi] = now
        stack.append(tsugi)
else:
    print(-1)
    exit()
ato = n
mae = pre[n]
ans = []
while mae >= 0:
    ans.append(ato - mae)
    ato = mae
    mae = pre[ato]
print(*ans, sep=" ")

n, m = map(int, input().split())
s = input()
out_s = "1" * (m)
if out_s in s:
    print(-1)
    exit()
s = s[::-1]
if s[0] == "1":
    print(-1)
    exit()
pre = [-1] * (n + 1)
stack = [0]
while stack:
    now = stack.pop()
    if now == n:
        break
    for i in range(1, m + 1):
        tsugi = now + i
        if tsugi > n:
            continue
        if s[tsugi] == "1":
            continue
        pre[tsugi] = now
        stack.append(tsugi)
else:
    print(-1)
    exit()
ato = n
mae = pre[n]
ans = []
while mae >= 0:
    ans.append(ato - mae)
    ato = mae
    mae = pre[ato]
print(*ans, sep=" ")


from collections import deque

N, M = map(int, input().split())
S = input()[::-1]

stack = deque([[0, M + 1]])
now = 0
while stack:
    now, d = stack.pop()
    d -= 1
    if not d:
        continue
    if now + d >= N:
        stack.append([now, d - (now + d - N)])
        print(*map(lambda s: s[1], reversed(stack)))
        quit()
    stack.append([now, d])
    if S[now + d] == "0":
        stack.append([now + d, M + 1])
print(-1)

N, M = map(int, input().split())
S = list(map(int, reversed(input())))
now = 0
ans = []
while now + M < N:
    last = now
    now += M
    while S[now]:
        now -= 1
    if last == now:
        print(-1)
        break
    ans.append(now - last)
else:
    print(*reversed(ans + [N - now]))


N, M = map(int, input().split())
S = input()
S = S[::-1]

dp = [-1] * (N + 1)
dp[0] = 0

pos = 0
while pos < N:

    p = pos
    update = False

    for i in range(1, M + 1):
        if p + i > N:
            break
        if S[p + i] == "1":
            continue
        update = True
        pos = p + i
        dp[pos] = dp[p] + 1

    if not update:
        break

if dp[N] == -1:
    print(-1)
    exit()

dp = dp[::-1]
num = dp[0]
ans = []
pos = 0

while num > 0:
    for i in range(1, M + 1):
        if dp[pos + i] == num - 1:
            num -= 1
            pos += i
            ans.append(str(i))

ans = " ".join(ans)
print(ans)

N, M = map(int, input().split())
S = input()
S = S[::-1]

dp = [-1] * (N + 1)
dp[0] = 0

pos = 0
while pos < N:

    p = pos
    update = False

    for i in range(1, M + 1):
        if p + i > N:
            break
        if S[p + i] == "0":
            update = True
            pos = p + i
            dp[pos] = dp[p] + 1

    if not update:
        break

if dp[N] == -1:
    print(-1)
    exit()

dp = dp[::-1]
num = dp[0]
ans = []
pos = 0

while num > 0:
    for i in range(1, M + 1):
        if dp[pos + i] == num - 1:
            num -= 1
            pos += i
            ans.append(str(i))
            break

ans = " ".join(ans)
print(ans)


def main(x: int, y: int):
    award: int = 0

    if (x and y) == 1:
        award += 400000

    for i in [x, y]:
        if i == 1:
            award += 300000
        elif i == 2:
            award += 200000
        elif i == 3:
            award += 100000

    print(award)

if __name__ == "__main__":
    x, y = map(int, input().split())

    main(x, y)

def main(x: int, y: int):
    award = 0

    if x == y == 1:
        award += 400000

    for i in [x, y]:
        if i == 1:
            award += 300000
        elif i == 2:
            award += 200000
        elif i == 3:
            award += 100000

    print(award)

if __name__ == "__main__":
    x, y = map(int, input().split())

    main(x, y)


n = int(input())
a = list(map(int, input().split()))
tmp = 0
key = 0
for i in range(n):
    if tmp + a[i] >= sum(a) / 2:
        key = i
        break
    tmp += a[i]

ans = abs(sum(a[0:key]) - sum(a[key::]))
tmp = 0
key = 0
for i in range(n):
    tmp += a[i]
    if tmp >= sum(a) / 2:
        key = i + 1
        break

ans = min(abs(sum(a[0:key]) - sum(a[key::])), ans)
print(ans)

n = int(input())
a = list(map(int, input().split()))
tmp = 0
key = 0
SUMA = sum(a)

for i in range(n):
    if tmp + a[i] >= SUMA / 2:
        key = i
        break
    tmp += a[i]

ans = abs(sum(a[0:key]) - sum(a[key::]))
tmp = 0
key += 1
ans = min(abs(sum(a[0:key]) - sum(a[key::])), ans)
print(ans)


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

ans, sumA2 = abs(sum(A) - 0), 0
for i in range(N):
    sumA2 += A[i]
    ans = min(ans, abs(sum(A) - sumA2 * 2))

print(ans)

N = int(input())
A = list(map(int, input().split()))
L, R, sumL_R = 0, N - 1, A[0] - A[N - 1]

while R - L > 1:
    if sumL_R < 0:
        L += 1
        sumL_R += A[L]
    else:
        R -= 1
        sumL_R -= A[R]
print(abs(sumL_R))


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

ans = 10**11

for i in range(n):
    if abs(sum(A[:i]) - sum(A[i:])) < ans:
        ans = abs(sum(A[:i]) - sum(A[i:]))

print(ans)

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

ans = 10**11
sum_A = sum(A)
l = 0

for a in A:
    l += a
    if abs(l - (sum_A - l)) < ans:
        ans = abs(l - (sum_A - l))

print(ans)


N = int(input())
sticks = list(map(int, input().split()))
diffMin = sum(sticks)
for i in range(len(sticks)):
    diff = abs(sum(sticks[:i]) - sum(sticks[i:]))
    if diff < diffMin:
        diffMin = diff
print(diffMin)

N = int(input())
sticks = list(map(int, input().split()))
sticksSum = sum(sticks)
diffMin = sum(sticks)
plusSticks = 0
for i in range(len(sticks)):
    plusSticks += sticks[i]
    diffMin = min(diffMin, abs(2 * plusSticks - sticksSum))
print(diffMin)


N = int(input())
A = tuple(map(int, input().split(" ")))
d = {i: (sum(A[:i]), sum(A[i:])) for i in range(N)}

print(min(abs(value[0] - value[1]) for key, value in d.items()))

N = int(input())
A = tuple(map(int, input().split(" ")))

S = sum(A)
ans = S
s = 0
for i in range(N):
    s += A[i]
    ans = min(ans, abs(s - (S - s)))
print(ans)


n = int(input())
a = list(map(int, input().split()))
m = 0
s = []
for i in range(len(a)):
    m += a[i]
    s.append(abs(sum(a) - m * 2))
print(min(s))

n = int(input())
a = list(map(int, input().split()))
m = 0
s = []
suma = sum(a)
for i in range(len(a)):
    m += a[i]
    s.append(abs(suma - m * 2))
print(min(s))


from itertools import accumulate

N = int(input())
A = list([int(x) for x in input().split()])

count = 0
while True:
    # 中心に最も近い点を探す
    ironbar = list(accumulate(A))
    center = sum(A) // 2

    i = 0
    j = len(A) - 1

    while i + 1 < j:
        check = ironbar[(i + j) // 2]
        if check < center:
            i = (i + j) // 2
        elif check > center:
            j = (i + j) // 2
        else:
            print(count)
            exit()

    if check > center:
        A[j] -= 1
    else:
        A[j] += 1

    count += 1

from itertools import accumulate

N = int(input())
A = list([int(x) for x in input().split()])

count = 0
ironbar = list(accumulate(A))

min_value = ironbar[-1]
for i in range(N):
    bet = abs(ironbar[i] - (ironbar[-1] - ironbar[i]))

    if min_value > bet:
        min_value = bet
    else:
        print(min_value)
        exit()


n = int(input())
a = list(map(int, input().split()))
x = 2020202020
for i in range(1, n):
    x = min(x, abs(sum(a[:i]) - sum(a[i:])))
print(x)

n = int(input())
a = list(map(int, input().split()))
x = 0
y = sum(a)
ans = 2020202020
for i in range(n - 1):
    x += a[i]
    y -= a[i]
    ans = min(ans, abs(x - y))
print(ans)


N = int(input())
arr = list(map(int, input().split()))
min_delta = sum(arr)
for i in range(1, N):
    min_delta = min(abs(sum(arr[:i]) - sum(arr[i:])), min_delta)

print(min_delta)

N = int(input())
arr = list(map(int, input().split()))
first = 0
second = sum(arr)
min_delta = second - first
for i in range(N):
    first += arr[i]
    second -= arr[i]
    min_delta = min(abs(second - first), min_delta)

print(min_delta)


Traceback (most recent call last):
  File "/home/alex/Documents/bug-detection/input/Project_CodeNet/data/p02859/Python/s972374104.py", line 2, in <module>
    ans = r * r
TypeError: can't multiply sequence by non-int of type 'str'


Traceback (most recent call last):
  File "/home/alex/Documents/bug-detection/input/Project_CodeNet/data/p02859/Python/s447174511.py", line 2, in <module>
    print(r**2)
TypeError: unsupported operand type(s) for ** or pow(): 'str' and 'int'


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


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


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


Traceback (most recent call last):
  File "/home/alex/Documents/bug-detection/input/Project_CodeNet/data/p02859/Python/s490869549.py", line 2, in <module>
    print(N**2)
TypeError: unsupported operand type(s) for ** or pow(): 'str' and 'int'


import sys

def main():
    N = int(input())
    S = input()
    if N % 2 == 1:
        print("No")
        sys.exit()
    n = N / 2
    yesno = "Yes" if S[:n] == S[n:] else "No"
    print(yesno)

if __name__ == "__main__":
    main()

def main():
    N = int(input())
    S = input()
    if N % 2 == 1:
        print("No")
        quit()
    n = N // 2
    yesno = "Yes" if S[:n] == S[n:] else "No"
    print(yesno)

if __name__ == "__main__":
    main()

Traceback (most recent call last):
  File "/home/alex/Documents/bug-detection/input/Project_CodeNet/data/p02860/Python/s888782335.py", line 16, in <module>
    main()
  File "/home/alex/Documents/bug-detection/input/Project_CodeNet/data/p02860/Python/s888782335.py", line 11, in main
    yesno = "Yes" if S[:n] == S[n:] else "No"
TypeError: slice indices must be integers or None or have an __index__ method


n = int(input())
s = input()
ans = "No"
if n % 2 == 0:
    ans = "Yes"
    for i in range(n / 2):
        if s[i] != s[n / 2 + i]:
            ans = "No"
            break
print(ans)

n = int(input())
s = input()
ans = "No"
if n % 2 == 0:
    ans = "Yes"
    for i in range(n // 2):
        if s[i] != s[n // 2 + i]:
            ans = "No"
            break
print(ans)

Traceback (most recent call last):
  File "/home/alex/Documents/bug-detection/input/Project_CodeNet/data/p02860/Python/s022164402.py", line 6, in <module>
    for i in range(n / 2):
TypeError: 'float' object cannot be interpreted as an integer


N = int(input())
S = int(input())
if N % 2 != 0:
    print("No")
else:
    arr = list(S)
    s_len = len(arr) // 2
    if arr[s_len:] == arr[:s_len]:
        print("Yes")
    else:
        print("No")

N = int(input())
S = input()
if N % 2 != 0:
    print("No")
else:
    arr = list(S)
    s_len = len(arr) // 2
    if arr[s_len:] == arr[:s_len]:
        print("Yes")
    else:
        print("No")

Traceback (most recent call last):
  File "/home/alex/Documents/bug-detection/input/Project_CodeNet/data/p02860/Python/s186953339.py", line 2, in <module>
    S = int(input())
ValueError: invalid literal for int() with base 10: 'abcabc'


import sys
import math
from typing import List, Tuple

def len(lhs: Tuple[int, int], rhs: Tuple[int, int]):
    return math.sqrt((lhs[0] - rhs[0]) ** 2 + (lhs[1] - rhs[1]) ** 2)

def solve(n: int, cities: List[Tuple[int, int]]):
    sum = 0
    for i1, city1 in enumerate(cities):
        for i2, city2 in enumerate(cities):
            if i1 != i2:
                sum += len(city1, city2)
    return sum / n

def main():
    inputs = list(sys.stdin)
    n = int(inputs.pop(0))
    cities = [[int(n) for n in s.split(" ")] for s in inputs]
    print(solve(n, cities))

main()

import sys
import math

def len(lhs, rhs):
    return math.sqrt((lhs[0] - rhs[0]) ** 2 + (lhs[1] - rhs[1]) ** 2)

def solve(n: int, cities):
    sum = 0
    for i1, city1 in enumerate(cities):
        for i2, city2 in enumerate(cities):
            if i1 != i2:
                sum += len(city1, city2)
    return sum / n

def main():
    inputs = list(sys.stdin)
    n = int(inputs.pop(0))
    cities = [[int(n) for n in s.split(" ")] for s in inputs]
    print(solve(n, cities))

main()


from itertools import permutations

import numpy as np

N = int(input())

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

combs = np.array(list(permutations(xys, N)))
ret = np.array(
    [np.linalg.norm(combs[:, i] - combs[:, i + 1], axis=1) for i in range(N - 1)]
).sum() / len(combs)

print(f"{ret:.10f}")

from itertools import permutations

import numpy as np

N = int(input())

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

combs = np.array(list(permutations(xys, N)))
ret = np.array(
    [np.linalg.norm(combs[:, i] - combs[:, i + 1], axis=1) for i in range(N - 1)]
).sum() / len(combs)

print("{:.10f}".format(ret))


import numpy as np
import itertools

n = int(input())

route = [np.array(list(map(int, input().split()))) for _ in range(n)]
results = []
for v in itertools.permutations(route, r=n):
    distance = 0
    for start, goal in zip(v[:-1], v[1:]):
        distance += np.linalg.norm(goal - start)
    results.append(distance)
print(np.mean(results))

import itertools

n = int(input())

route = [list(map(int, input().split())) for _ in range(n)]
results = []
for v in itertools.permutations(route, r=n):
    distance = 0
    for i in range(n - 1):
        start, goal = v[i], v[i + 1]
        distance += ((goal[0] - start[0]) ** 2 + (goal[1] - start[1]) ** 2) ** 0.5
    results.append(distance)
print(sum(results) / len(results))


def gcd1(a, b):
    def calc(a, b, first, second):
        if a < b:
            a, b = b, a
        r = a % b
        q = -(a - r) // b
        if r == 0:
            return b, second
        # first += q * second
        first = [x + q * y for (x, y) in zip(first, second)]
        return calc(r, b, second, first)

    first = [1, 0]
    second = [0, 1]
    return calc(a, b, first, second)

def mod_inv(n, mod):
    table = gcd1(n, mod)
    inv = table[1][1]
    if inv < 0:
        inv += mod
    return inv

def cmb(n, r, mod):
    ans = 1
    for i in range(r):
        ans = (ans * n) % mod
        n -= 1
    ans1 = 1
    for i in range(1, r + 1):
        ans1 = (ans1 * i) % mod
    return ans * mod_inv(ans1, mod)

def main():
    m = 10**9 + 7
    X, Y = map(int, input().split())
    XY = X + Y
    min_xy = XY // 3
    max_xy = XY - min_xy
    if XY % 3 != 0:
        print(0)
        return
    if X < min_xy or X > max_xy or Y < min_xy or Y > max_xy:
        print(0)
        return
    index_x = X - min_xy
    index_y = Y - min_xy
    # print(index_x, index_y)
    ans = cmb(min_xy, index_x, m)
    print(ans % m)

if __name__ == "__main__":
    main()


import sys

def extgcd(a, b):
    r = [1, 0, a]
    w = [0, 1, b]
    while w[2] != 1:
        q = r[2] // w[2]
        r2 = w
        w2 = [r[0] - q * w[0], r[1] - q * w[1], r[2] - q * w[2]]
        r = r2
        w = w2
    return [w[0], w[1]]

def mod_inv(a, m):
    x = extgcd(a, m)[0]
    return (m + x % m) % m

def main():
    X, Y = map(int, input().split())
    C = 10**9 + 7

    if (X + Y) % 3 != 0:
        print(0)
        sys.exit()
    cnt = [0, 0]
    if Y / X >= 1:
        (i, j) = (1, 2)
        cnt[0] += 1
    else:
        (i, j) = (2, 1)
        cnt[1] += 1

    while (i, j) != (X, Y):
        if Y * i > X * j:
            i += 1
            j += 2
            cnt[0] += 1
        else:
            i += 2
            j += 1
            cnt[1] += 1

    ans = 1
    for i in range(1, sum(cnt) + 1):
        ans *= i
        ans %= C
    for i in range(1, cnt[0] + 1):
        ans *= mod_inv(i, C)
        ans %= C
    for i in range(1, cnt[1] + 1):
        ans *= mod_inv(i, C)
        ans %= C

    print(ans)

main()

import sys

def extgcd(a, b):
    r = [1, 0, a]
    w = [0, 1, b]
    while w[2] != 1:
        q = r[2] // w[2]
        r2 = w
        w2 = [r[0] - q * w[0], r[1] - q * w[1], r[2] - q * w[2]]
        r = r2
        w = w2
    return [w[0], w[1]]

def mod_inv(a, m):
    x = extgcd(a, m)[0]
    return (m + x % m) % m

def main():
    X, Y = map(int, input().split())
    C = 10**9 + 7

    if (X + Y) % 3 != 0:
        print(0)
        sys.exit()

    cnt = [0, 0]
    cnt[0] = (2 * X - Y) // 3
    cnt[1] = (-X + 2 * Y) // 3

    if cnt[0] < 0 or cnt[1] < 0:
        print(0)
        sys.exit()

    ans = 1
    for i in range(1, sum(cnt) + 1):
        ans *= i
        ans %= C
    for i in range(1, cnt[0] + 1):
        ans *= mod_inv(i, C)
        ans %= C
    for i in range(1, cnt[1] + 1):
        ans *= mod_inv(i, C)
        ans %= C

    print(ans)

main()


MOD = 10**9 + 7
MAX = 7 * 10**5
fact = [1] * (MAX + 1)
finv = [1] * (MAX + 1)
for i in range(2, MAX + 1):
    fact[i] = fact[i - 1] * i % MOD
    finv[i] = pow(fact[i], MOD - 2, MOD)

def comb(n: int, k: int) -> int:
    if n < k or n < 0 or k < 0:
        return 0
    return (fact[n] * finv[k] % MOD) * finv[n - k] % MOD

x, y = map(int, input().split())
a = 2 * y - x
b = 2 * x - y

if a < 0 or b < 0:
    ans = 0
elif a % 3 != 0 or b % 3 != 0:
    ans = 0
else:
    a //= 3
    b //= 3
    print(a, b)
    ans = comb(a + b, a)

print(ans)

MOD = 10**9 + 7

def comb(n, k, MOD):
    if n < k or n < 0 or k < 0:
        return 0
    k = min(k, n - k)
    if k == 0:
        return 1
    iinv = [1] * (k + 1)
    ans = n
    for i in range(2, k + 1):
        iinv[i] = MOD - iinv[MOD % i] * (MOD // i) % MOD
        ans *= (n + 1 - i) * iinv[i] % MOD
        ans %= MOD
    return ans

x, y = map(int, input().split())
a = 2 * y - x
b = 2 * x - y

if a < 0 or b < 0:
    ans = 0
elif a % 3 != 0 or b % 3 != 0:
    ans = 0
else:
    a //= 3
    b //= 3
    # print(a, b)
    ans = comb(a + b, a, MOD)

print(ans)


def solve(n, t, ab_list):
    ab_list_s = sorted(ab_list, key=lambda x: x[0])
    res = 0
    dp = [0] * t
    for i in range(n):
        for s in range(t - 1, -1, -1):
            if dp[s] > 0 or s == 0:
                u = s + ab_list_s[i][0]
                r = dp[s] + ab_list_s[i][1]
                if u >= t:
                    res = max(res, r)
                else:
                    dp[u] = max(dp[u], r)
    return max(res, max(dp))

def main():
    n, t = map(int, input().split())
    ab_list = []
    for _ in range(n):
        a, b = map(int, input().split())
        ab_list.append([a, b])
    res = solve(n, t, ab_list)
    print(res)

def test():
    assert solve(2, 60, [[10, 10], [100, 100]]) == 110
    assert solve(3, 60, [[10, 10], [10, 20], [10, 30]]) == 60
    assert solve(3, 60, [[30, 10], [30, 20], [30, 30]]) == 50

if __name__ == "__main__":
    test()
    main()

def solve(n, t, ab_list):
    ab_list_s = sorted(ab_list, key=lambda x: x[0])
    res = 0
    dp = [0] * t
    for i in range(n):
        for s in range(t - 1, -1, -1):
            if dp[s] > 0 or s == 0:
                u = s + ab_list_s[i][0]
                r = dp[s] + ab_list_s[i][1]
                if u >= t:
                    res = max(res, r)
                else:
                    dp[u] = max(dp[u], r)
    return max(res, max(dp))

def main():
    n, t = map(int, input().split())
    ab_list = []
    for _ in range(n):
        a, b = map(int, input().split())
        ab_list.append([a, b])
    res = solve(n, t, ab_list)
    print(res)

def test():
    assert solve(2, 60, [[10, 10], [100, 100]]) == 110
    assert solve(3, 60, [[10, 10], [10, 20], [10, 30]]) == 60
    assert solve(3, 60, [[30, 10], [30, 20], [30, 30]]) == 50

if __name__ == "__main__":
    # test()
    main()


N, T = map(int, input().split())
items = [tuple(map(int, input().split())) for _ in range(N)]

items.sort()

ans = 0
dp = {0: 0}
for wi, vi in items:
    for w, v in dp.items():
        if w < T:
            v2 = v + vi
            if v2 > ans:
                ans = v2

    for w, v in list(dp.items()):
        w2 = w + wi
        if w2 <= T:
            v2 = v + vi
            if w2 in dp:
                if v2 > dp[w2]:
                    dp[w2] = v2
            else:
                dp[w2] = v2

v2 = max(dp.values())
ans = max(ans, v2)

print(ans)

import sys

input = sys.stdin.readline

def solve():
    N, T = map(int, input().split())
    items = [tuple(map(int, input().split())) for _ in range(N)]

    items.sort()

    capW = T - 1
    dp = [0] * (capW + 1)
    ans = 0
    for wi, vi in items:
        ans = max(ans, dp[-1] + vi)
        for w in reversed(range(wi, capW + 1)):
            v0 = dp[w - wi] + vi
            if v0 > dp[w]:
                dp[w] = v0

    print(ans)

solve()


import sys
from collections import defaultdict

n, t = map(int, input().split())
knapsack = defaultdict(lambda: 0)
knapsack[0] = 0
tmp_ans = 0
cuisines = [tuple(map(int, line.split())) for line in sys.stdin]
cuisines.sort()
for a, b in cuisines:
    for time, satisfied in list(knapsack.items()):
        if time + a >= t:
            tmp_ans = max(tmp_ans, satisfied + b)
            continue
        knapsack[time + a] = max(knapsack[time + a], satisfied + b)

print(max(tmp_ans, max(knapsack.values())))

import sys

import numpy as np

n, t = map(int, input().split())
knapsack = np.zeros(t, dtype=np.int64)
cuisines = [tuple(map(int, line.split())) for line in sys.stdin]
cuisines.sort()
tmp_ans = 0
for a, b in cuisines:
    tmp_ans = max(tmp_ans, knapsack.max() + b)
    knapsack[a:] = np.maximum(knapsack[a:], knapsack[:-a] + b)

print(max(tmp_ans, knapsack.max()))


def solve():
    import sys

    input = sys.stdin.readline

    n, t = map(int, input().split())  # t - 1まで注文OK
    ab = tuple(tuple(map(int, input().split())) for _ in range(n))

    ret = 0
    for last_dish, (_, last_b) in enumerate(ab):
        dp = [0] * t
        # dp[皿ID][時間] = 最大おいしさ
        # dp[T-1] + 残りで一番美味しいもの
        # 先にどの皿を最後に食べるか決めておいて、それ以外でdp
        # 3000回dp

        for dish, (a, b) in enumerate(ab):
            if dish == last_dish:
                continue
            for t_ in range(t - 1, a - 1, -1):
                dp[t_] = max(dp[t_], dp[t_ - a] + b)
        ret = max(ret, max(dp) + last_b)
    return ret

print(solve())

def solve():
    from collections import namedtuple
    import sys

    input = sys.stdin.readline
    Dish = namedtuple("Dish", "idx cost pt")

    n, t = map(int, input().split())
    dishes = []
    for idx in range(1, n + 1):
        cost, pt = map(int, input().split())
        dishes.append(Dish(idx=idx, cost=cost, pt=pt))

    dp_ht = [[0] * t for _ in range(n + 2)]  # head->tail
    dp_th = [[0] * t for _ in range(n + 2)]  # tail->head
    # dp[注文可能な範囲の端][完食にかかる総時間]:= 美味しさの最大値

    for dish in dishes:
        for cost_ in range(t):
            if cost_ >= dish.cost:
                dp_ht[dish.idx][cost_] = max(
                    dp_ht[dish.idx - 1][cost_],
                    dp_ht[dish.idx - 1][cost_ - dish.cost] + dish.pt,
                )
            else:
                dp_ht[dish.idx][cost_] = dp_ht[dish.idx - 1][cost_]

    for dish in reversed(dishes):
        for cost_ in range(t):
            if cost_ >= dish.cost:
                dp_th[dish.idx][cost_] = max(
                    dp_th[dish.idx + 1][cost_],
                    dp_th[dish.idx + 1][cost_ - dish.cost] + dish.pt,
                )
            else:
                dp_th[dish.idx][cost_] = dp_th[dish.idx + 1][cost_]

    ret = 0
    for last_dish in dishes:
        for cost_ in range(t):
            ret = max(
                ret,
                dp_ht[last_dish.idx - 1][cost_]
                + dp_th[last_dish.idx + 1][t - 1 - cost_]
                + last_dish.pt,
            )
    return ret

print(solve())


def main():
    N, T = map(int, input().split())
    AB = [list(map(int, input().split())) for _ in range(N)]

    dp = [-1] * (T + 3000)
    dp[0] = 0
    for a, b in sorted(AB):
        for j in range(T - 1, -1, -1):
            if dp[j] == -1:
                continue
            if dp[j] + b > dp[j + a]:
                dp[j + a] = dp[j] + b
    print(max(dp))

main()


def main():
    N, T = map(int, input().split())
    AB = [list(map(int, input().split())) for _ in range(N)]

    dp = [-1] * (T + 3000)
    dp[0] = 0
    c = 0
    for a, b in sorted(AB):
        for j in range(c, -1, -1):
            if dp[j] == -1:
                continue
            t = dp[j] + b
            if dp[j + a] < t:
                dp[j + a] = t
        c = min(c + a, T - 1)
    print(max(dp))

main()


n, t = map(int, input().split())
x = [list(map(int, input().split())) for _ in range(n)]
a_max = max([y[1] for y in x])

dp_pre = [0 for j in range(t + a_max)]
dp = [0 for j in range(t + a_max)]
for ai, bi in x:
    for j in range(ai):
        dp[j] = dp_pre[j]
    for j in range(ai, t + ai):
        dp[j] = max(dp_pre[j], dp_pre[j - ai] + bi)
    for j in range(t + ai, t + a_max):
        dp[j] = dp_pre[j]
    dp_pre, dp = dp, dp_pre
print(max(dp_pre))

n, t = map(int, input().split())
x = [list(map(int, input().split())) for _ in range(n)]
x.sort()
a_max = x[-1][0]

dp_pre = [0 for j in range(t + a_max)]
dp = [0 for j in range(t + a_max)]
for ai, bi in x:
    for j in range(ai):
        dp[j] = dp_pre[j]
    for j in range(ai, t + ai):
        dp[j] = max(dp_pre[j], dp_pre[j - ai] + bi)
    for j in range(t + ai, t + a_max):
        dp[j] = dp_pre[j]
    dp_pre, dp = dp, dp_pre
print(max(dp_pre))


import copy

def main():
    n, t = map(int, input().split())
    f = [list(map(int, input().split())) for _ in range(n)]
    dp = [0 for i in range(t)]
    ls = [[] for i in range(t)]
    for i in range(n):
        for j in range(t - 1, -1, -1):
            if j - f[i][0] >= 0 and dp[j - f[i][0]] + f[i][1] > dp[j]:
                dp[j] = dp[j - f[i][0]] + f[i][1]
                ls[j] = copy.copy(ls[j - f[i][0]]) + [i]
    res = dp[t - 1]
    ll = ls[t - 1]
    fff = [0 for _ in range(n)]
    for sll in ll:
        fff[sll] = 1
    ff = [f[i][1] for i in range(n) if fff[i] == 0]
    if ff:
        return res + max(ff)
    else:
        return res

print(main())

def main():
    n, t = map(int, input().split())
    f = [list(map(int, input().split())) for _ in range(n)]
    dp1 = [[0 for i in range(t)] for j in range(n + 1)]
    dp2 = [[0 for i in range(t)] for j in range(n + 1)]
    for i in range(n):
        for j in range(t - 1, -1, -1):
            if j - f[i][0] >= 0:
                dp1[i + 1][j] = max(dp1[i][j - f[i][0]] + f[i][1], dp1[i][j])
            else:
                dp1[i + 1][j] = dp1[i][j]
    for i in range(n):
        for j in range(t - 1, -1, -1):
            if j - f[n - 1 - i][0] >= 0:
                dp2[i + 1][j] = max(
                    dp2[i][j - f[n - 1 - i][0]] + f[n - 1 - i][1], dp2[i][j]
                )
            else:
                dp2[i + 1][j] = dp2[i][j]

    res = 0
    for i in range(n):
        for j in range(t):
            res = max(res, dp1[i][j] + f[i][1] + dp2[n - 1 - i][t - 1 - j])

    return res

print(main())


LARGE = 998244353

def solve(n, d_list):
    cnt = [0] * (max(d_list) + 1)
    for i in range(n):
        cnt[d_list[i]] += 1
    if cnt[0] != 1 or d_list[0] != 0:
        return 0
    res = 1
    for i in range(max(d_list)):
        res *= pow(cnt[i], cnt[i + 1], LARGE)
        res %= LARGE
    return res

def main():
    n = int(input())
    d_list = list(map(int(input())))
    res = solve(n, d_list)
    print(res)

def test():
    assert solve(4, [0, 1, 1, 2]) == 2
    assert solve(4, [1, 1, 1, 1]) == 0
    assert solve(7, [0, 3, 2, 1, 2, 2, 1]) == 24

if __name__ == "__main__":
    test()
    main()

LARGE = 998244353

def solve(n, d_list):
    cnt = [0] * (max(d_list) + 1)
    for i in range(n):
        cnt[d_list[i]] += 1
    if cnt[0] != 1 or d_list[0] != 0:
        return 0
    res = 1
    for i in range(max(d_list)):
        res *= pow(cnt[i], cnt[i + 1], LARGE)
        res %= LARGE
    return res

def main():
    n = int(input())
    d_list = list(map(int, input().split()))
    res = solve(n, d_list)
    print(res)

def test():
    assert solve(4, [0, 1, 1, 2]) == 2
    assert solve(4, [1, 1, 1, 1]) == 0
    assert solve(7, [0, 3, 2, 1, 2, 2, 1]) == 24

if __name__ == "__main__":
    test()
    main()

Traceback (most recent call last):
  File "/home/alex/Documents/bug-detection/input/Project_CodeNet/data/p02866/Python/s784122479.py", line 32, in <module>
    main()
  File "/home/alex/Documents/bug-detection/input/Project_CodeNet/data/p02866/Python/s784122479.py", line 19, in main
    d_list = list(map(int(input())))
ValueError: invalid literal for int() with base 10: '0 1 1 2'


from itertools import takewhile, islice

S = input()
total = 0
for idx in range(len(S) + 1):
    left = sum(1 for _ in takewhile(lambda x: x == "<", reversed(S[:idx])))
    right = sum(1 for _ in takewhile(lambda x: x == ">", islice(S, idx, None)))
    total += max((left, right))
print(total)

S = input()
A = [0 for _ in range(len(S) + 1)]

for idx in range(len(S)):
    if S[idx] == "<":
        A[idx + 1] = max(A[idx + 1], A[idx] + 1)

for idx in reversed(range(len(S))):
    if S[idx] == ">":
        A[idx] = max(A[idx], A[idx + 1] + 1)

print(sum(A))


#!/usr/bin/env python3
n = int(input())
p = sorted(list(map(int, input().split())) for _ in range(n))

lmax = n - 1
rmin = 0
for i in range(n):
    if p[i][1] <= p[rmin][1]:
        rmin = i

ans = 0

# same(lmax, rmin)
dmax = 0
for i in range(n):
    if i == lmax or i == rmin:
        continue
    if p[i][1] - p[i][0] > p[dmax][1] - p[dmax][0]:
        dmax = i
    d1 = max(0, p[rmin][1] - p[lmax][0] + 1)
    d2 = max(0, p[dmax][1] - p[dmax][0] + 1)
ans = max(ans, d1 + d2)

# !same(lmax, rmin)
m = lmax
for i in range(rmin, lmax)[::-1]:
    if p[i + 1][1] < p[m][1]:
        m = i + 1
    d1 = max(0, p[rmin][1] - p[i][0] + 1)
    d2 = max(0, p[m][1] - p[lmax][0] + 1)
    ans = max(ans, d1 + d2)

print(ans)


def prepare(n, MOD):
    f = 1
    for m in range(1, n + 1):
        f *= m
        f %= MOD
    fn = f

    inv = pow(f, MOD - 2, MOD)
    invs = [1] * (n + 1)
    invs[n] = inv
    for m in range(n, 1, -1):
        inv *= m
        inv %= MOD
        invs[m - 1] = inv

    return fn, invs

n = int(input())
MOD = 998244353
fn, invs = prepare(n, MOD)
ans = pow(3, n, MOD)
impossible = 0
for i in range(n // 2):
    tmp = fn * invs[i] * invs[n - i] % MOD * pow(2, i, MOD)
    impossible = (impossible + 2 * tmp) % MOD
print((ans - impossible) % MOD)

def prepare(n, MOD):
    f = 1
    for m in range(1, n + 1):
        f *= m
        f %= MOD
    fn = f

    inv = pow(f, MOD - 2, MOD)
    invs = [1] * (n + 1)
    invs[n] = inv
    for m in range(n, 1, -1):
        inv *= m
        inv %= MOD
        invs[m - 1] = inv

    return fn, invs

n = int(input())
MOD = 998244353
fn, invs = prepare(n, MOD)
ans = pow(3, n, MOD)
impossible = 0
mul = 2
for i in range(n // 2):
    tmp = fn * invs[i] * invs[n - i] % MOD * mul
    impossible = (impossible + tmp) % MOD
    mul = mul * 2 % MOD
print((ans - impossible) % MOD)


a, b = map(int, input())
if a >= 10 or b >= 10:
    print(int(a * b))

else:
    print(-1)

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

if max(a, b) <= 9:
    print(a * b)
else:
    print(-1)

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


S = list(input())
if len(S) > 3:
    print(int(S[0]) * int(S[1]))
else:
    print(-1)

S = list(input())
if len(S) < 4:
    print(int(S[0]) * int(S[-1]))
else:
    print(-1)


a, b = map(int, input())

if 1 <= a <= 9 or 1 <= b <= 9:
    print(a * b)
else:
    print(-1)

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

if a >= 10 or b >= 10:
    print(-1)
    exit()
print(a * b)

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


a = int(input())
b = int(input())

if a >= 10 or b >= 10:
    print(-1)
else:
    print(a * b)

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

if a >= 10 or b >= 10:
    print(-1)
else:
    print(a * b)

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


n = int(input())
if n > 81:
    print("No")
else:
    is_ok = False
    for x in range(1, 10):
        if n % x == 0 and n // x <= 9:
            is_ok = True
            break

if is_ok:
    print("Yes")
else:
    print("No")


n = int(input())
list = list(range(9, 1, -1))
ans = 0
flag = 1

if n > 1:
    while n > 1:
        for i in list:
            if n % i == 0:
                n = n / i
                ans += 1
                flag = 0
                break

        if flag == 1:
            ans = 0
            break
elif n == 1:
    ans = 2

if 0 < ans <= 2:
    print("Yes")
else:
    print("No")

n = int(input())
list = list(range(9, 1, -1))
ans = 0

if n > 1:
    while n > 1:
        flag = 1
        for i in list:
            if n % i == 0:
                n = n / i
                ans += 1
                flag = 0
                break

        if flag == 1:
            ans = 0
            break
elif n == 1:
    ans = 2

if 0 < ans <= 2:
    print("Yes")
else:
    print("No")


#!/usr/bin/env python3
import sys
from sympy.ntheory import factorint
import numpy as np

def solve(N: int):

    fct = factorint(N)

    fct_list = []
    for key, item in zip(fct.keys(), fct.values()):
        fct_list += [key] * item

    result = 1e12

    if len(fct_list) == 2:
        x = fct_list[0]
        y = fct_list[1]
        print(x - 1 + y - 1)
    elif len(fct_list) == 1:
        x = fct_list[0]
        y = 1
        print(x - 1 + y - 1)

    else:
        for i in range(1, len(fct_list) - 1):

            x = np.prod(fct_list[: i + 1])
            y = np.prod(fct_list[i + 1 :])

            result = min(result, x - 1 + y - 1)
        print(result)
    return

def main():
    def iterate_tokens():
        for line in sys.stdin:
            for word in line.split():
                yield word

    tokens = iterate_tokens()
    N = int(next(tokens))  # type: int
    solve(N)

if __name__ == "__main__":
    main()

#!/usr/bin/env python3
import sys
import numpy as np

def solve(N: int):
    result_list = []
    for i in range(1, int(np.sqrt(N)) + 1):
        if N % i == 0:
            result_list.append(i - 1 + N // i - 1)
        else:
            pass
    print(min(result_list))
    return

def main():
    def iterate_tokens():
        for line in sys.stdin:
            for word in line.split():
                yield word

    tokens = iterate_tokens()
    N = int(next(tokens))  # type: int
    solve(N)

if __name__ == "__main__":
    main()

Traceback (most recent call last):
  File "/home/alex/Documents/bug-detection/.venv/lib/python3.10/site-packages/numpy/core/__init__.py", line 23, in <module>
    from . import multiarray
  File "/home/alex/Documents/bug-detection/.venv/lib/python3.10/site-packages/numpy/core/multiarray.py", line 10, in <module>
    from . import overrides
  File "/home/alex/Documents/bug-detection/.venv/lib/python3.10/site-packages/numpy/core/overrides.py", line 8, in <module>
    from numpy.core._multiarray_umath import (
ImportError: libgfortran-040039e1.so.5.0.0: failed to map segment from shared object

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "/home/alex/Documents/bug-detection/input/Project_CodeNet/data/p02881/Python/s328405909.py", line 4, in <module>
    import numpy as np
  File "/home/alex/Documents/bug-detection/.venv/lib/python3.10/site-packages/numpy/__init__.py", line 139, in <module>
    from . import core
  File "/home/alex/Documents/bug-detection/.venv/lib/python3.10/site-packages/numpy/core/__init__.py", line 49, in <module>
    raise ImportError(msg)
ImportError:

IMPORTANT: PLEASE READ THIS FOR ADVICE ON HOW TO SOLVE THIS ISSUE!

Importing the numpy C-extensions failed. This error can happen for
many reasons, often due to issues with your setup or how NumPy was
installed.

We have compiled some common reasons and troubleshooting tips at:

    https://numpy.org/devdocs/user/troubleshooting-importerror.html

Please note and check the following:

  * The Python version is: Python3.10 from "/home/alex/Documents/bug-detection/.venv/bin/python3"
  * The NumPy version is: "1.25.1"

and make sure that they are the versions you expect.
Please carefully study the documentation linked above for further help.

Original error was: libgfortran-040039e1.so.5.0.0: failed to map segment from shared object


N = int(input())
ret = []
for i in range(1, N):
    for j in range(i, N):
        if i * j == N:
            ret.append(i + j - 2)
print(min(ret))

from math import sqrt

N = int(input())

for i in range(int(sqrt(N) + 1), -1, -1):
    if N % i == 0:
        print((i - 1) + (N // i - 1))
        break


def is_good(mid, key):
    return sum(max(0, a - mid // f) for a, f in zip(A, F)) <= key

def binary_search(bad, good, key):
    while good - bad > 1:
        mid = (bad + good) // 2
        if is_good(mid, key):
            good = mid
        else:
            bad = mid
    return good

N, K = map(int, input().split())
A = list(map(int, input().split()))
F = list(map(int, input().split()))
A.sort()
F.sort(reverse=True)
A_sum = sum(A)
ans = binary_search(-1, max(a * f for a, f in zip(A, F)), K)
print(ans)

import numpy as np

def is_good(mid, key):
    return A_sum - np.minimum(A, mid // F).sum() <= key

def binary_search(bad, good, key):
    while good - bad > 1:
        mid = (bad + good) // 2
        if is_good(mid, key):
            good = mid
        else:
            bad = mid
    return good

N, K = map(int, input().split())
A = np.array(input().split(), dtype=np.int64)
F = np.array(input().split(), dtype=np.int64)
A.sort()
F[::-1].sort()
A_sum = A.sum()
ans = binary_search(-1, np.max(A * F), K)
print(ans)