Harryxun/testdata huggingface dataset:
Buggy python submission (verdict: Time Limit Exceeded):
import sys
input = sys.stdin.readline

mod = 10**9 + 7
K = int(input())
S = input().rstrip()

N = K + len(S)
fact = [1] * (N+1)
fact_inv = [1] * (N+1)
for i in range(1, N+1):
    fact[i] = i * fact[i-1] % mod
fact_inv[N] = pow(fact[N], mod-2, mod)
for i in range(1, N+1)[::-1]:
    fact_inv[i-1] = i * fact_inv[i] % mod
comb = lambda n, k: fact[n] * fact_inv[k] * fact_inv[n-k] % mod

ans = 0
for i in range(K+1):
    ans = (ans + comb((K - i) + len(S) - 1, len(S) - 1) * pow(25, K - i) * pow(26, i)) % mod
print(ans)

Fixed submission:
import sys
input = sys.stdin.readline

mod = 10**9 + 7
K = int(input())
S = input().rstrip()

N = K + len(S)
fact = [1] * (N+1)
fact_inv = [1] * (N+1)
for i in range(1, N+1):
    fact[i] = i * fact[i-1] % mod
fact_inv[N] = pow(fact[N], mod-2, mod)
for i in range(1, N+1)[::-1]:
    fact_inv[i-1] = i * fact_inv[i] % mod
comb = lambda n, k: fact[n] * fact_inv[k] * fact_inv[n-k] % mod

ans = 0
for i in range(K+1):
    ans = (ans + comb(K - i + len(S) - 1, len(S) - 1) * pow(25, K - i, mod) * pow(26, i, mod)) % mod
print(ans)
Buggy python submission (verdict: Time Limit Exceeded):
import sys
input = sys.stdin.readline

mod = 998244353
A, B, C, D = map(int, input().split())

dp = [[0] * (D+1) for _ in range(C+1)]
dp[A][B] = 1
for i in range(A+1, C+1):
    dp[i][B] = dp[i-1][B] * B % mod
for j in range(B, D):
    for i in range(A, C+1):
        x = dp[i][j] * i
        dp[i][j+1] = (dp[i][j+1] + x) % mod
        for k in range(i + 1, C+1):
            dp[k][j+1] = (dp[k][j+1] + x) % mod
            x = x * (j + 1) % mod
print(dp[C][D])



Fixed submission:
import sys
input = sys.stdin.readline

mod = 998244353
A, B, C, D = map(int, input().split())

dp = [[0] * (D+1) for _ in range(C+1)]
dp[A][B] = 1
for i in range(A, C+1):
    if i > A:
        dp[i][B] = dp[i-1][B] * B % mod
    for j in range(B+1, D+1):
        dp[i][j] = dp[i][j-1] * i + dp[i-1][j] * j - dp[i-1][j-1] * (i - 1) * (j - 1)
        dp[i][j] %= mod
print(dp[C][D])
Buggy python submission (verdict: Runtime Error):
X, Y = map(int, input().split())
for a in range(X+1):
    b = X - a
    if 2 * a + 4 * b == Y:
        print('Yes')
        exit()
print('No')X, Y = map(int, input().split())
for a in range(X+1):
    b = X - a
    if 2 * a + 4 * b == Y:
        print('Yes')
        exit()
print('No')

Fixed submission:
X, Y = map(int, input().split())
for a in range(X+1):
    b = X - a
    if 2 * a + 4 * b == Y:
        print('Yes')
        exit()
print('No')
Buggy python submission (verdict: Time Limit Exceeded):
from heapq import heappush, heappop
import sys
input = sys.stdin.readline

H, W, K = map(int, input().split())
x1, y1, x2, y2 = map(lambda x : int(x) - 1, input().split())
c = [input().rstrip() for _ in range(H)]
INF = 10**18
dist = [[[(INF, INF)] * 4 for _ in range(W)] for _ in range(H)]
dist[x1][y1] = [(1, 0)] * 4
heap = []
for i in range(4):
    heappush(heap, (dist[x1][y1][i], x1, y1, i))
dx = [1, 0, -1, 0]
dy = [0, 1, 0, -1]
while heap:
    d, x, y, i = heappop(heap)

    if d > dist[x][y][i]:
        continue

    for j in range(4):
        nd = (d[0] + 1, 0)
        if dist[x][y][j] > nd:
            dist[x][y][j] = nd
            heappush(heap, (nd, x, y, j))

    nx = x + dx[i]
    ny = y + dy[i]
    if nx < 0 or H <= nx or ny < 0 or W <= ny or c[nx][ny] == '@':
        continue
    nd = (d[0], d[1] + 1) if d[1] < K else (d[0] + 1, 1)
    if dist[nx][ny][i] > nd:
        dist[nx][ny][i] = nd
        heappush(heap, (nd, nx, ny, i))
ans = min(dist[x2][y2])[0]
print(ans if ans < INF else -1)


Fixed submission:
from collections import deque
import sys
input = sys.stdin.readline

H, W, K = map(int, input().split())
x1, y1, x2, y2 = map(lambda x : int(x) - 1, input().split())
c = [input().rstrip() for _ in range(H)]
INF = 10**18
dist = [[[(INF, INF)] * 4 for _ in range(W)] for _ in range(H)]
dist[x1][y1] = [(1, 0)] * 4
dq = deque()
for i in range(4):
    dq.append((dist[x1][y1][i], x1, y1, i))
dx = [1, 0, -1, 0]
dy = [0, 1, 0, -1]
while dq:
    d, x, y, i = dq.popleft()

    if d > dist[x][y][i]:
        continue

    for j in range(4):
        nd = (d[0] + 1, 0)
        if dist[x][y][j] > nd:
            dist[x][y][j] = nd
            dq.append((nd, x, y, j))

    nx = x + dx[i]
    ny = y + dy[i]
    if nx < 0 or H <= nx or ny < 0 or W <= ny or c[nx][ny] == '@':
        continue
    if d[1] < K:
        nd = (d[0], d[1] + 1)
        if dist[nx][ny][i] > nd:
            dist[nx][ny][i] = nd
            dq.appendleft((nd, nx, ny, i))
    else:
        nd = (d[0] + 1, 1)
        if dist[nx][ny][i] > nd:
            dist[nx][ny][i] = nd
            dq.append((nd, nx, ny, i))
ans = min(dist[x2][y2])[0]
print(ans if ans < INF else -1)
Buggy python submission (verdict: Time Limit Exceeded):
import sys
input = sys.stdin.readline

class BIT:
    def __init__(self, size):
        self.bit = [0] * (size + 1)
        self.size = size

    def sum(self, i):
        i += 1
        s = 0
        while i > 0:
            s += self.bit[i]
            i -= i & -i
        return s

    def add(self, i, x):
        i += 1
        while i <= self.size:
            self.bit[i] += x
            i += i & -i

class RangeBIT:
    def __init__(self, size):
        self.bit0 = BIT(size)
        self.bit1 = BIT(size)

    def sum(self, i):
        return self.bit0.sum(i) * i + self.bit1.sum(i)

    # end points are inclusive
    def add(self, l, r, x):
        self.bit0.add(l, x)
        self.bit0.add(r+1, -x)
        self.bit1.add(l, -x*(l-1))
        self.bit1.add(r+1, x*r)


N, K = map(int, input().split())
A = list(map(int, input().split()))
while K > 0 and not all(a == N for a in A):
    rb = RangeBIT(N)
    for i, a in enumerate(A):
        rb.add(max(0, i - a), min(N - 1, i + a), 1)
    for i in range(N):
        A[i] = rb.sum(i) - (rb.sum(i-1) if i > 0 else 0)
    K -= 1
print(*A)


Fixed submission:
from itertools import accumulate
import sys
input = sys.stdin.readline

N, K = map(int, input().split())
A = list(map(int, input().split()))
while K > 0 and not all(a == N for a in A):
    imos = [0] * (N + 1)
    for i, a in enumerate(A):
        imos[max(0, i - a)] += 1
        imos[min(N, i + a + 1)] -= 1
    A = list(accumulate(imos))[:-1]
    K -= 1
print(*A)
Buggy python submission (verdict: Runtime Error):
from bisect import bisect_left
import sys
input = sys.stdin.readline

N = int(input())
a = [0] + list(map(int, input().split()))
G = [[] for _ in range(N+1)]
for _ in range(N-1):
    u, v = map(int, input().split())
    G[u].append(v)
    G[v].append(u)
lis = [float('inf')] * (N+1)
ans = [0] * (N+1)

def dfs(v, p):
    i = bisect_left(lis, a[v])
    ans[v] = i + 1
    tmp = lis[i]
    lis[i] = a[v]
    for c in G[v]:
        if c == p:
            continue
        dfs(c, v)
    lis[i] = tmp

dfs(1, 0)
print('\n'.join(map(str, ans[1:])))



Fixed submission:
from bisect import bisect_left
import sys
sys.setrecursionlimit(10**6)
input = sys.stdin.readline

N = int(input())
a = list(map(int, input().split()))
G = [[] for _ in range(N)]
for _ in range(N-1):
    u, v = map(int, input().split())
    u -= 1
    v -= 1
    G[u].append(v)
    G[v].append(u)
lis = [float('inf')] * N
ans = [0] * N
visited = [False] * N

def dfs(v):
    visited[v] = True
    i = bisect_left(lis, a[v])
    tmp = lis[i]
    lis[i] = a[v]
    ans[v] = bisect_left(lis, float('inf'))
    for c in G[v]:
        if visited[c]:
            continue
        dfs(c)
    lis[i] = tmp

dfs(0)
print('\n'.join(map(str, ans)))
Buggy python submission (verdict: Time Limit Exceeded):
N, K = map(int, input().split())
mod = 10**9+7

cnt = [0] * (K+1)
ans = 0
for i in range(1, K+1):
    f = i - cnt[i]
    ans = (ans + f * (K//i)**N) % mod
    for j in range(2*i, K+1, i):
        cnt[j] += f
print(ans)



Fixed submission:
N, K = map(int, input().split())
mod = 10**9+7

cnt = [0] * (K+1)
ans = 0
for i in range(1, K+1):
    f = i - cnt[i]
    ans = (ans + f * pow(K//i, N, mod)) % mod
    for j in range(2*i, K+1, i):
        cnt[j] += f
print(ans)
Buggy python submission (verdict: Time Limit Exceeded):
from collections import defaultdict
import sys
input = sys.stdin.readline

mod = 998244353
N, S = map(int, input().split())
A = list(map(int, input().split()))
ans = 0
dp = [defaultdict(int) for j in range(S+1)]
for i in range(1, N+1):
    a = A[i-1]
    for j in range(a+1, S+1)[::-1]:
        for k, v in dp[j-a].items():
            dp[j][k] += v
    if a <= S:
        dp[a][i] = 1
    for k, v in dp[S].items():
        ans += (k * (N-i+1)) * v % mod
        ans %= mod
        dp[S] = defaultdict(int)
print(ans)

Fixed submission:
import sys
input = sys.stdin.readline

mod = 998244353
N, S = map(int, input().split())
A = list(map(int, input().split()))
ans = 0
dp = [0] * (S+1)
for i in range(1, N+1):
    a = A[i-1]
    for j in range(a+1, S+1)[::-1]:
        dp[j] += dp[j-a]
    if a <= S:
        dp[a] += i
    ans += dp[S] * (N-i+1)
    ans %= mod
    dp[S] = 0
print(ans)

Buggy python submission (verdict: Time Limit Exceeded):
import sys
input = sys.stdin.readline

mod = 998244353
N, S = map(int, input().split())
A = list(map(int, input().split()))
ans = 0
dp = [[] for j in range(S+1)]
for i in range(1, N+1):
    a = A[i-1]
    for j in range(a+1, S+1)[::-1]:
        dp[j] += dp[j-a]
    if a <= S:
        dp[a] += [i]
    for k in dp[S]:
        ans += k * (N-i+1)
        ans %= mod
        dp[S] = []
print(ans)

Fixed submission:
import sys
input = sys.stdin.readline

mod = 998244353
N, S = map(int, input().split())
A = list(map(int, input().split()))
ans = 0
dp = [0] * (S+1)
for i in range(1, N+1):
    a = A[i-1]
    for j in range(a+1, S+1)[::-1]:
        dp[j] += dp[j-a]
    if a <= S:
        dp[a] += i
    ans += dp[S] * (N-i+1)
    ans %= mod
    dp[S] = 0
print(ans)
Buggy python submission (verdict: Time Limit Exceeded):
def mod_inverse(n, mod=10**9+7):
    return pow(n, mod-2, mod)


def combination(n, k, mod=10**9+7):
    numer = denom = 1
    for i in range(k):
        numer = (numer * (n-i)) % mod
        denom = (denom * (i+1)) % mod
    return (numer * mod_inverse(denom, mod)) % mod


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

fact = [1] * (n+1)
fact_inv = [1] * (n+1)
for i in range(1, n+1):
    fact[i] = i * fact[i-1] % mod
    fact_inv[i] = mod_inverse(fact[i])

ans = combination(2*n-1, n)
for i in range(k+1, n):
    ans -= combination(n, i) * combination(n-1, i)
    ans %= mod
print(ans)


Fixed submission:
def mod_inverse(n, mod=10**9+7):
    return pow(n, mod-2, mod)


def combination(n, k, mod=10**9+7):
    numer = denom = 1
    for i in range(k):
        numer = (numer * (n-i)) % mod
        denom = (denom * (i+1)) % mod
    return (numer * mod_inverse(denom, mod)) % mod


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

fact = [1] * (n+1)
fact_inv = [1] * (n+1)
for i in range(1, n+1):
    fact[i] = i * fact[i-1] % mod
    fact_inv[i] = mod_inverse(fact[i])

ans = combination(2*n-1, n)
for i in range(k+1, n):
    ans -= (fact[n] * fact_inv[i] * fact_inv[n-i]) * (fact[n-1] * fact_inv[i] * fact_inv[n-1-i])
    # ans -= combination(n, i) * combination(n-1, i)
    ans %= mod
print(ans)

Buggy python submission (verdict: Time Limit Exceeded):
def mod_inverse(n, mod=10**9+7):
    return pow(n, mod-2, mod)


def combination(n, k, mod=10**9+7):
    numer = denom = 1
    for i in range(k):
        numer = (numer * (n-i)) % mod
        denom = (denom * (i+1)) % mod
    return (numer * mod_inverse(denom, mod)) % mod


mod = 10**9+7
n, k = map(int, input().split())
ans = combination(2*n-1, n)
for i in range(k+1, n):
    ans -= combination(n, i) * combination(n-1, i)
    ans %= mod
print(ans)


Fixed submission:
def mod_inverse(n, mod=10**9+7):
    return pow(n, mod-2, mod)


def combination(n, k, mod=10**9+7):
    numer = denom = 1
    for i in range(k):
        numer = (numer * (n-i)) % mod
        denom = (denom * (i+1)) % mod
    return (numer * mod_inverse(denom, mod)) % mod


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

fact = [1] * (n+1)
fact_inv = [1] * (n+1)
for i in range(1, n+1):
    fact[i] = i * fact[i-1] % mod
    fact_inv[i] = mod_inverse(fact[i])

ans = combination(2*n-1, n)
for i in range(k+1, n):
    ans -= (fact[n] * fact_inv[i] * fact_inv[n-i]) * (fact[n-1] * fact_inv[i] * fact_inv[n-1-i])
    # ans -= combination(n, i) * combination(n-1, i)
    ans %= mod
print(ans)
Buggy python submission (verdict: Time Limit Exceeded):
import sys
input = sys.stdin.readline

INF = 10**19
N, K = map(int, input().split())
A = list(map(int, input().split()))
pos = []
neg = []
for a in A:
    if a >= 0:
        pos.append(a)
    else:
        neg.append(a)
pos.sort()
neg.sort()
p, n = len(pos), len(neg)
if K <= p * n:
    lb, ub = -INF, INF
    while ub - lb > 1:
        mid = (lb + ub) // 2
        cnt = 0

        s, t = 0, 0
        while s < p:
            while t < n and pos[s] * neg[t] <= mid:
                t += 1
            cnt += t
            s += 1

        if cnt < K:
            lb = mid
        else:
            ub = mid
    print(ub)
else:
    K -= p * n
    neg = neg[::-1]
    lb, ub = -INF, INF
    while ub - lb > 1:
        mid = (lb + ub) // 2
        cnt = 0

        s, t = p-1, 0
        while s >= 0:
            while t < s and pos[s] * pos[t] <= mid:
                t += 1
            cnt += min(s, t)
            s -= 1

        s, t = n-1, 0
        while s >= 0:
            while t < s and neg[s] * neg[t] <= mid:
                t += 1
            cnt += min(s, t)
            s -= 1

        if cnt < K:
            lb = mid
        else:
            ub = mid
    print(ub)


Fixed submission:
import sys
input = sys.stdin.readline

INF = 10**19
N, K = map(int, input().split())
A = list(map(int, input().split()))
pos = []
neg = []
for a in A:
    if a >= 0:
        pos.append(a)
    else:
        neg.append(a)
pos.sort()
neg.sort()
p, n = len(pos), len(neg)
if K <= p * n:
    lb, ub = -INF, INF
    while ub - lb > 1:
        mid = (lb + ub) // 2
        cnt = 0

        s, t = 0, 0
        while s < p:
            while t < n and pos[s] * neg[t] <= mid:
                t += 1
            cnt += t
            s += 1

        if cnt < K:
            lb = mid
        else:
            ub = mid
    print(ub)
else:
    K -= p * n
    neg = neg[::-1]
    lb, ub = -INF, INF
    while ub - lb > 1:
        mid = (lb + ub) // 2
        cnt = 0

        s, t = p-1, 0
        while s >= 0:
            while t < s and pos[s] * pos[t] <= mid:
                t += 1
            cnt += min(s, t)
            s -= 1

        s, t = n-1, 0
        while s >= 0:
            while t < s and neg[s] * neg[t] <= mid:
                t += 1
            cnt += min(s, t)
            s -= 1

        if cnt < K:
            lb = mid
        else:
            ub = mid
    print(ub)
Buggy python submission (verdict: Time Limit Exceeded):
import sys
input = sys.stdin.readline

H, N = map(int, input().split())
A = []
B = []
for _ in range(N):
    a, b = map(int, input().split())
    A.append(a)
    B.append(b)
INF = 10**10
dp = [INF] * (H+1)
dp[0] = 0
for i in range(N):
    for j in range(H+1):
        dp[j] = min(dp[j], dp[max(0, j-A[i])]+B[i])
print(dp[H])


Fixed submission:
import sys
input = sys.stdin.readline

H, N = map(int, input().split())
A = [0] * N
B = [0] * N
for i in range(N):
    a, b = map(int, input().split())
    A[i] = a
    B[i] = b
INF = 10**10
dp = [INF] * (H+1)
dp[0] = 0
for i in range(N):
    for j in range(H+1):
        dp[j] = min(dp[j], dp[max(0, j-A[i])]+B[i])
print(dp[H])
Buggy python submission (verdict: Time Limit Exceeded):
import sys
input = sys.stdin.readline

N, D, A = map(int, input().split())
monsters = [list(map(int, input().split())) for _ in range(N)]
monsters.sort()

ans = 0
for i in range(N):
    x, h = monsters[i]
    if h <= 0:
        continue
    bomb = (h+A-1) // A
    damage = bomb * A
    right = x + 2*D
    for j in range(i+1, N):
        if monsters[j][0] > right:
            break
        monsters[j][1] -= damage
    ans += bomb
print(ans)


Fixed submission:
from heapq import heapify, heappush, heappop
import sys
input = sys.stdin.readline

N, D, A = map(int, input().split())
monsters = [tuple(map(int, input().split())) for _ in range(N)]
heapify(monsters)

max_H = 10**9
ans = 0
damage = 0
while monsters:
    x, h = heappop(monsters)
    if h <= max_H:
        h -= damage
        if h <= 0:
            continue
        bomb = (h+A-1) // A
        damage += bomb * A
        ans += bomb
        heappush(monsters, (x+2*D, max_H+bomb*A))
    else:
        damage -= h-max_H
print(ans)
Buggy python submission (verdict: Time Limit Exceeded):
import sys
from fractions import gcd
input = sys.stdin.readline


def lcm(a, b):
    return a * b // gcd(a, b)


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

ans = 0
prev = A[0]
for a in A:
    l = lcm(prev, a)
    factor = l // prev % mod
    ans = (ans * factor + l // a) % mod
    prev = l
print(ans)


Fixed submission:
import sys
from fractions import gcd
input = sys.stdin.readline


def mod_inverse(n, mod=10**9+7):
    return pow(n, mod-2, mod)

    
def lcm(a, b):
    return a * b // gcd(a, b)


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

ans = 0
inv_sum = 0
l = A[0]
for a in A:
    l = lcm(l, a)
    inv_sum = (inv_sum + mod_inverse(a)) % mod
print(l * inv_sum % mod)

Buggy python submission (verdict: Time Limit Exceeded):
import sys
from fractions import gcd
input = sys.stdin.readline


def lcm(a, b):
    return a * b // gcd(a, b)


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

ans = 0
prev = A[0]
for a in A:
    l = lcm(prev, a)
    factor = l // prev
    ans = (ans * factor + l // a) % mod
    prev = l
print(ans)


Fixed submission:
import sys
from fractions import gcd
input = sys.stdin.readline


def mod_inverse(n, mod=10**9+7):
    return pow(n, mod-2, mod)

    
def lcm(a, b):
    return a * b // gcd(a, b)


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

ans = 0
inv_sum = 0
l = A[0]
for a in A:
    l = lcm(l, a)
    inv_sum = (inv_sum + mod_inverse(a)) % mod
print(l * inv_sum % mod)
Buggy python submission (verdict: Runtime Error):
def mod_inverse(n, mod=10**9+7):
    return pow(n, mod-2, mod)


def combination(n, k, mod=10**9+7):
    numer = denom = 1
    for i in range(k):
        numer = (numer * (n-i)) % mod
        denom = (denom * (i+1)) % mod
    return (numer * mod_inverse(denom, mod)) % mod


def factorial(n):
    if n == 0:
        return 1
    return n * factorial(n-1) % mod


mod = 10**9+7
N = int(input())
x = [0] + list(map(int, input().split()))
fact = factorial(N-1)

ans = 0
for k in range(2, N):
    ans = (ans + x[k] * (1 - mod_inverse(k)))

for k in range(1, N-1):
    ans = (ans - x[k] * (1 - mod_inverse(N-k))) % mod

ans = ans * fact % mod

ans = (ans + fact * sum((x[N] - x[k]) * mod_inverse(N-k) for k in range(1, N))) % mod

print(ans)


Fixed submission:
import sys
sys.setrecursionlimit(10**6)


def mod_inverse(n, mod=10**9+7):
    return pow(n, mod-2, mod)


def combination(n, k, mod=10**9+7):
    numer = denom = 1
    for i in range(k):
        numer = (numer * (n-i)) % mod
        denom = (denom * (i+1)) % mod
    return (numer * mod_inverse(denom, mod)) % mod


def factorial(n):
    if n == 0:
        return 1
    return n * factorial(n-1) % mod


mod = 10**9+7
N = int(input())
x = [0] + list(map(int, input().split()))
fact = factorial(N-1)

ans = 0
for k in range(2, N):
    ans = (ans + x[k] * (1 - mod_inverse(k)))

for k in range(1, N-1):
    ans = (ans - x[k] * (1 - mod_inverse(N-k))) % mod

ans = ans * fact % mod

ans = (ans + fact * sum((x[N] - x[k]) * mod_inverse(N-k) for k in range(1, N))) % mod

print(ans)
Buggy python submission (verdict: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
using ll = long long;

int main() {
    ios_base::sync_with_stdio(false);
    cin.tie(0);

    int N;
    cin >> N;
    vector<int> P(N), Q(N);
    for (int i = 0; i < N; i++) cin >> P[i];
    for (int i = 0; i < N; i++) cin >> Q[i];
    vector<int> perm(N);
    for (int i = 0; i < N; i++) perm[i] = i + 1;
    int n = 0;
    int a, b;
    do {
        if (perm == P) a = n;
        if (perm == Q) b = n;
        n++;
    } while (next_permutation(perm.begin(), perm.end()));
    cout << abs(a - b) << endl;
}

Fixed submission:
def generate_perms(n, perms):
    if n == 0:
        return perms
    new_perms = []
    for perm in perms:
        for i in range(1, N+1):
            if i not in perm:
                p = list(perm)
                p.append(i)
                new_perms.append(p)
    return generate_perms(n-1, new_perms)


N = int(input())
P = list(map(int, input().split()))
Q = list(map(int, input().split()))
perms = generate_perms(N, [[]])
print(abs(perms.index(P) - perms.index(Q)))
Buggy python submission (verdict: Time Limit Exceeded):
from heapq import heappush, heappop

N, M = map(int, input().split())
A = sorted(list(map(int, input().split())), reverse=True)
ans = 0
heap = []
heappush(heap, (-A[0]*2, 0, 0))
checked = set()
checked.add((0, 0))
for i in range(M):
    power, x, y = heappop(heap)
    ans -= power
    if x+1 < N and (x+1, y) not in checked:
        heappush(heap, (-(A[x+1]+A[y]), x+1, y))
        checked.add((x+1, y))
    if y+1 < N and (x, y+1) not in checked:
        heappush(heap, (-(A[x]+A[y+1]), x, y+1))
        checked.add((x, y+1))
print(ans)


Fixed submission:
from itertools import accumulate
import sys
input = sys.stdin.readline

N, M = map(int, input().split())
A = list(map(int, input().split()))
A.sort(reverse=True)
lb = 0
ub = 2*10**5+1
while ub - lb > 1:
    m = (lb + ub) // 2
    cnt = 0
    s, t = 0, N-1
    while t >= 0:
        while s < N and A[s] + A[t] >= m:
            s += 1
        cnt += s
        t -= 1

    if cnt >= M:
        lb = m
    else:
        ub = m

accum = list(accumulate([0] + A))
ans = 0
cnt = 0
s, t = 0, N-1
while t >= 0:
    while s < N and A[s] + A[t] >= lb:
        s += 1
    ans += accum[s] + s * A[t]
    cnt += s
    t -= 1
print(ans - (cnt-M) * lb)
Buggy python submission (verdict: Runtime Error):
def check(n):
    x = A[n] + M
    B = A[:n] + A[n+1:-(P-1)]
    scores = M * (V - P)
    if x < B[-1]:
        return False
    for b in B:
        if b + M <= x:
            scores -= M
        else:
            scores -= x - b
        if scores <= 0:
            return True
    return False


N, M, V, P = map(int, input().split())
A = list(map(int, input().split()))
A.sort()
print(A)
left = 0
right = N - P
while left < right:
    mid = (left+right) // 2
    if check(mid):
        right = mid
    else:
        left = mid + 1
print(N - left)


Fixed submission:
def check(n):
    x = A[n] + M
    if P == 1:
        B = A[:n] + A[n+1:]
    else:
        B = A[:n] + A[n+1:-(P-1)]
    scores = M * (V - P)
    if x < B[-1]:
        return False
    for b in B:
        if b + M <= x:
            scores -= M
        else:
            scores -= x - b
        if scores <= 0:
            return True
    return False


N, M, V, P = map(int, input().split())
A = list(map(int, input().split()))
A.sort()

left = 0
right = N - P
while left < right:
    mid = (left+right) // 2
    if check(mid):
        right = mid
    else:
        left = mid + 1
print(N - left)
Buggy python submission (verdict: Time Limit Exceeded):
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)


Fixed submission:
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)