Harryxun/testdata huggingface dataset:
Buggy python submission (verdict: Time Limit Exceeded):
from heapq import heapify, heappop, heappush


def solve(n, aaa, bbb):
    q = [(-b, i) for i, b in enumerate(bbb) if b != aaa[i]]
    heapify(q)
    ans = 0
    while q:
        b, i = heappop(q)
        b = -b
        b -= bbb[(i - 1) % n] + bbb[(i + 1) % n]
        if b <= 0:
            return -1
        bbb[i] = b
        ans += 1
        if b != aaa[i]:
            heappush(q, (-b, i))
    return ans


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


Fixed submission:
from heapq import heapify, heappop, heappush


def solve(n, aaa, bbb):
    q = [(-b, i) for i, b in enumerate(bbb) if b != aaa[i]]
    heapify(q)
    ans = 0
    while q:
        b, i = heappop(q)
        b = -b - aaa[i]
        d, b = divmod(b, bbb[(i - 1) % n] + bbb[(i + 1) % n])
        if d == 0:
            return -1
        b += aaa[i]
        bbb[i] = b
        ans += d
        if b != aaa[i]:
            heappush(q, (-b, i))
    return ans


n = int(input())
aaa = list(map(int, input().split()))
bbb = list(map(int, input().split()))
print(solve(n, aaa, bbb))
Buggy python submission (verdict: Runtime Error):
n = int(input())
a = [0] + list(map(int, input().split()))
for i in range(n // 2, 0, -1):
    a[i] ^= a[i * 2::i]

s = [i for i, b in enumerate(a) if b]
print(len(s))
print(*s)


Fixed submission:
n = int(input())
aaa = list(map(int, input().split()))
aaa = [0] + aaa
for i in range(n // 2, 0, -1):
    b = 0
    j = i * 2
    while n >= j:
        b ^= aaa[j]
        j += i
    aaa[i] ^= b

ans = [i for i, b in enumerate(aaa) if b]
print(len(ans))
print(*ans)
Buggy python submission (verdict: Time Limit Exceeded):
import sys

n = int(input())
aaa = list(map(int, sys.stdin.readlines()))

dp = [-1]
for a in aaa:
    for i, m in enumerate(dp):
        if m < a:
            dp[i] = a
            break
    else:
        dp.append(a)
print(len(dp))


Fixed submission:
import sys
from bisect import bisect

n = int(input())
aaa = list(map(int, sys.stdin.readlines()))

dp = [1]
for a in aaa:
    a *= -1
    i = bisect(dp, a)
    if i < len(dp):
        dp[i] = a
    else:
        dp.append(a)
print(len(dp))
Buggy python submission (verdict: Runtime Error):
from itertools import accumulate


def make_dp(vvv, n, k):
    dp = [-(10 ** 9) * (k + 1)]
    dp[0] = 0

    for i in range(min(n, k)):
        i += 1
        vv = vvv[:i]
        vv.sort(reverse=True)
        sm = sum(vv)
        dp[i] = max(dp[i], sm)
        while i < k and vv and vv[-1] < 0:
            sm -= vv.pop()
            i += 1
            dp[i] = max(dp[i], sm)

    return list(accumulate(dp, func=max))


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

acl = make_dp(vvv, n, k)
acr = make_dp(reversed(vvv), n, k)

ans = max(acl[i] + acr[k - i] for i in range(k + 1))
ans = min(ans, sum(v for v in vvv if v > 0))
print(ans)


Fixed submission:
from itertools import accumulate

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

dpl, dpr = [-10 ** 9] * (k + 1), [-10 ** 9] * (k + 1)
dpl[0] = dpr[0] = 0

for i in range(min(n, k)):
    i += 1
    vv = vvv[:i]
    vv.sort(reverse=True)
    sm = sum(vv)
    dpl[i] = max(dpl[i], sm)
    # print(i, vv, dpl)
    while i < k and vv:
        sm -= vv.pop()
        i += 1
        dpl[i] = max(dpl[i], sm)
    # print(i, vv, dpl)

vvv.reverse()

for i in range(min(n, k)):
    i += 1
    vv = vvv[:i]
    vv.sort(reverse=True)
    sm = sum(vv)
    dpr[i] = max(dpr[i], sm)
    # print(i, vv, dpr)
    while i < k and vv:
        sm -= vv.pop()
        i += 1
        dpr[i] = max(dpr[i], sm)
    # print(i, vv, dpr)

# print(dpl)
# print(dpr)
acl = list(accumulate(dpl, func=max))
acr = list(accumulate(dpr, func=max))
# print(acl)
# print(acr)
ans = 0
for i in range(k + 1):
    ans = max(ans, acl[i] + acr[k - i])
max_ans = sum(v for v in vvv if v > 0)
ans = min(ans, max_ans)
print(ans)
Buggy python submission (verdict: Runtime Error):
import numpy as np


def score(cache, sss, a, ab, d):
    mat = cache[ab]
    if mat is None:
        mat = cache[ab] = np.cumsum(np.resize(sss, ((n - 1) // ab + 1, ab)).T, axis=1)
    mat_a = mat[a % ab]
    return mat[0][d] + mat_a[-1] - mat_a[-d - 2]


n = int(input())
sss = list(map(int, input().split()))
sss.extend([0] * n)
sss = np.array(sss, dtype=np.int64)

ans = 0
cache = [None] * n
for ab in range(1, int((n - 2) ** 0.5) + 1):
    for remain in range(ab, n - 3, ab):
        a = n - remain - 1
        d = remain // ab
        if remain < a:
            ans = max(ans, score(cache, sss, a, ab, d))
            if ab != d:
                ans = max(ans, score(cache, sss, a, d, ab))
        else:
            if a % ab > 0:
                ans = max(ans, score(cache, sss, a, ab, d))
            if a > d and ab != d and a % d > 0:
                ans = max(ans, score(cache, sss, a, d, ab))
print(ans)


Fixed submission:
n = int(input())
sss = list(map(int, input().split()))

ans = 0
for c in range(1, n // 2):
    a = n - 1 - c
    k = c
    tmp = 0
    while a > c and a != k and a + c != k:
        tmp += sss[a] + sss[k]
        ans = max(ans, tmp)
        a -= c
        k += c
print(ans)

Buggy python submission (verdict: Time Limit Exceeded):
import numpy as np

n = int(input())
sss = list(map(int, input().split()))
sss = np.array(sss, dtype=np.int64)


def score(cache, sss, a, ab, d):
    mat = cache[ab]
    if mat is None:
        mat = cache[ab] = np.cumsum(np.resize(sss, ((n - 1) // ab + 1, ab)).T, axis=1)
    mat_a = mat[a % ab]
    # print(a, ab, d, mat[0][d] + mat_a[-1] - mat_a[-d - 2], mat[0][d], mat_a[-1], mat_a[-d - 2])
    # print(mat)
    return mat[0][d] + mat_a[-1] - mat_a[-d - 2]


cache = [None] * n
ans = 0
for a in range(3, n - 1):
    remain = n - a - 1
    for ab in range(1, min(a, int(remain ** 0.5) + 1)):
        d, m = divmod(remain, ab)
        if m != 0:
            continue
        if remain < a:
            ans = max(ans, score(cache, sss, a, ab, d))
            if ab != d:
                ans = max(ans, score(cache, sss, a, d, ab))
        else:
            if a % ab > 0:
                ans = max(ans, score(cache, sss, a, ab, d))
            if a > d and ab != d and a % d > 0:
                ans = max(ans, score(cache, sss, a, d, ab))
print(ans)


Fixed submission:
n = int(input())
sss = list(map(int, input().split()))

ans = 0
for c in range(1, n // 2):
    a = n - 1 - c
    k = c
    tmp = 0
    while a > c and a != k and a + c != k:
        tmp += sss[a] + sss[k]
        ans = max(ans, tmp)
        a -= c
        k += c
print(ans)
Buggy python submission (verdict: Runtime Error):
import sys

n, m = list(map(int, input().split()))
l, r = 1, n
for line in sys.stdin:
    a, b = list(map(int, input().split()))
    l = max(a, l)
    r = min(b, r)
print(max(0, r - l + 1))


Fixed submission:
import sys

n, m = list(map(int, input().split()))
l, r = 1, n
for line in sys.stdin:
    a, b = list(map(int, line.split()))
    l = max(a, l)
    r = min(b, r)
print(max(0, r - l + 1))
Buggy python submission (verdict: Time Limit Exceeded):
import sys

n, m = list(map(int, input().split()))
aaa = list(map(int, input().split()))
for line in sys.stdin:
    b, c = list(map(int, line.split()))
    aaa = sorted(aaa + [c] * b, reverse=True)[:n]
print(sum(aaa))


Fixed submission:
import sys

n, m = list(map(int, input().split()))
aaa = list(map(int, input().split()))
aaa.sort()
bc = []
for line in sys.stdin:
    b, c = map(int, line.split())
    bc.append((c, b))
bc.sort(reverse=True)
ans = 0
cnt = 0
for c, b in bc:
    while aaa and aaa[-1] >= c and cnt < n:
        ans += aaa.pop()
        cnt += 1
    if cnt + b >= n:
        ans += c * (n - cnt)
        cnt = n
        break
    ans += c * b
    cnt += b
if cnt < n:
    ans += sum(aaa[cnt - n:])
print(ans)
Buggy python submission (verdict: Time Limit Exceeded):
import sys
from bisect import insort


class Bit:
    def __init__(self, n):
        self.size = n
        self.tree = [0] * (n + 1)

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

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

    def debug_print(self):
        print(*('{:5d}'.format(i) for i in range(1, self.size + 1)))
        print(*('{:5d}'.format(self.sum(i)) for i in range(1, self.size + 1)))


Q = int(input())
queries = []
nums = []
for qi, line in enumerate(sys.stdin):
    q = list(map(int, line.split()))
    queries.append(q)
    if len(q) > 1:
        _, a, b = q
        nums.append((a, qi))
nums = sorted(nums)
min_a = nums[0][0]
n = len(nums)
num_d = {num: i for i, num in enumerate(nums)}

bit = Bit(n)
update_count = 0
update_list = []
sum_da = 0
sum_b = 0

for qi, q in enumerate(queries):
    if len(q) == 1:
        k = (update_count - 1) // 2
        x, qj = update_list[k]
        i = num_d[x, qj]
        d = x - min_a
        dn_cnt = (update_count + 1) // 2
        up_cnt = update_count - dn_cnt
        bsi = bit.sum(i + 1)
        up_sum = sum_da - bsi - d * up_cnt
        dn_sum = d * dn_cnt - bsi
        # print('k:{} x:{} i:{} d:{} up:{} dn:{}'.format(k, x, i, d, up_sum, dn_sum))
        res = up_sum + dn_sum + sum_b
        print(x, res)
    else:
        _, a, b = q
        sum_b += b
        update_count += 1
        insort(update_list, (a, qi))
        i = num_d[a, qi]
        d = a - min_a
        bit.add(i + 1, d)
        sum_da += d


Fixed submission:
import sys
from heapq import heappush, heappushpop

Q = int(input())
high = []
low = []
sum_dh, sum_dl = 0, 0
sum_b = 0
MIN_X = -(10 ** 9)
buf = []

for qi, line in enumerate(sys.stdin):
    q = list(map(int, line.split()))
    if len(q) == 1:
        x = -low[0]
        dx = x - MIN_X
        ans_l = dx * len(low) - sum_dl
        ans_h = sum_dh - dx * len(high)
        buf.append('{} {}\n'.format(x, ans_l + ans_h + sum_b))
    else:
        _, a, b = q
        da = a - MIN_X
        sum_b += b
        if len(low) == len(high):
            h = heappushpop(high, a)
            heappush(low, -h)
            dh = h - MIN_X
            sum_dh += da - dh
            sum_dl += dh
        else:
            l = -heappushpop(low, -a)
            heappush(high, l)
            dl = l - MIN_X
            sum_dl += da - dl
            sum_dh += dl

print(''.join(buf))
Buggy python submission (verdict: Time Limit Exceeded):
n = int(input())
i = 1
ans = 0
while True:
    d, m = divmod(n, i)
    if m != 0:
        i += 1
        continue
    d -= 1
    if i >= d:
        break
    ans += d
    i += 1
print(ans)


Fixed submission:
n = int(input())
i = 1
ans = 0
while True:
    d, m = divmod(n, i)
    d -= 1
    if i >= d:
        break
    if m != 0:
        i += 1
        continue
    ans += d
    i += 1
print(ans)

Buggy python submission (verdict: Time Limit Exceeded):
n = int(input())
i = 1
ans = 0
while True:
    d, m = divmod(n, i)
    if m != 0:
        i += 1
        continue
    d -= 1
    if i >= d:
        break
    ans += d
    i += 1
print(ans)


Fixed submission:
n = int(input())
i = 1
ans = 0
while True:
    d, m = divmod(n, i)
    d -= 1
    if i >= d:
        break
    if m != 0:
        i += 1
        continue
    ans += d
    i += 1
print(ans)
Buggy python submission (verdict: Time Limit Exceeded):
from collections import deque

h, w = list(map(int, input().split()))
f = []
q = deque()
for i in range(h):
    l = input()
    for j, c in enumerate(l):
        if c == '#':
            q.append((0, i, j))
    f.append(l)
visited = [[False] * w for _ in [0] * h]
ans = 0
move = [(0, 1), (0, -1), (1, 0), (-1, 0)]

while q:
    d, i, j = q.popleft()
    if visited[i][j]:
        continue
    ans = d
    visited[i][j] = True
    for di, dj in move:
        ni, nj = i + di, j + dj
        if ni < 0 or h <= ni or nj < 0 or w <= nj:
            continue
        if visited[ni][nj]:
            continue
        q.append((d + 1, ni, nj))

print(ans)


Fixed submission:
from collections import deque

h, w = list(map(int, input().split()))
f = []
q = deque()
for i in range(h):
    l = input()
    for j, c in enumerate(l):
        if c == '#':
            q.append((0, i, j))
    f.append(l)

visited = [[False] * w for _ in [0] * h]
for d, i, j in q:
    visited[i][j] = True

ans = 0
move = [(0, 1), (0, -1), (1, 0), (-1, 0)]

while q:
    d, i, j = q.popleft()
    ans = d
    for di, dj in move:
        ni, nj = i + di, j + dj
        if ni < 0 or h <= ni or nj < 0 or w <= nj:
            continue
        if visited[ni][nj]:
            continue
        visited[ni][nj] = True
        q.append((d + 1, ni, nj))

print(ans)

Buggy python submission (verdict: Time Limit Exceeded):
from collections import deque

h, w = list(map(int, input().split()))
f = []
q = deque()
for i in range(h):
    l = input()
    for j, c in enumerate(l):
        if c == '#':
            q.append((0, i, j))
    f.append(l)
visited = [[False] * w for _ in [0] * h]
ans = 0
move = [(0, 1), (0, -1), (1, 0), (-1, 0)]

while q:
    d, i, j = q.popleft()
    if visited[i][j]:
        continue
    ans = d
    visited[i][j] = True
    for di, dj in move:
        ni, nj = i + di, j + dj
        if ni < 0 or h <= ni or nj < 0 or w <= nj:
            continue
        if visited[ni][nj]:
            continue
        q.append((d + 1, ni, nj))

print(ans)


Fixed submission:
from collections import deque

h, w = list(map(int, input().split()))
f = []
q = deque()
for i in range(h):
    l = input()
    for j, c in enumerate(l):
        if c == '#':
            q.append((0, i, j))
    f.append(l)

visited = [[False] * w for _ in [0] * h]
for d, i, j in q:
    visited[i][j] = True

ans = 0
move = [(0, 1), (0, -1), (1, 0), (-1, 0)]

while q:
    d, i, j = q.popleft()
    ans = d
    for di, dj in move:
        ni, nj = i + di, j + dj
        if ni < 0 or h <= ni or nj < 0 or w <= nj:
            continue
        if visited[ni][nj]:
            continue
        visited[ni][nj] = True
        q.append((d + 1, ni, nj))

print(ans)
Buggy python submission (verdict: Runtime Error):
def solve(n, m, s):
    f = s[0]
    MOD = 10 ** 9 + 7

    p = None
    seq = 0
    max_seq = 0
    for c in s + '*':
        if c == p:
            seq += 1
        else:
            if p == f:
                max_seq = max(max_seq, seq)
            seq = 1
            p = c

    if max_seq == m:
        a, b = 1, 0
        for _ in range(n - 2):
            a, b = (a + b) % MOD, a % MOD
        return (3 * a + b) % MOD

    if n % 2 == 1:
        return 0

    ms = max_seq // 2 + 1

    dp = [0] * (n // 2 + 1)
    ap = [0] * (n // 2 + 1)
    dp[0] = 0
    ap[0] = 1
    for i in range(1, n // 2 + 1):
        j = max(-1, i - ms - 1)
        dp[i] = (ap[i - 1] - ap[j]) % MOD
        ap[i] = (ap[i - 1] + dp[i]) % MOD

    ans = 0
    for d in range(2, ms * 2 + 1, 2):
        ans = (ans + dp[(n - d) // 2] * d) % MOD

    return ans


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

print(solve(n, m, s))


Fixed submission:
def solve(n, m, s):
    f = s[0]
    MOD = 10 ** 9 + 7
    INF = 10 ** 6

    p = None
    seq = 0
    min_seq = INF
    for c in s:
        if c == p:
            seq += 1
        else:
            if p == f and (min_seq == INF or seq % 2 == 1):
                min_seq = min(min_seq, seq)
            seq = 1
            p = c

    if min_seq == INF:
        a, b = 1, 0
        for _ in range(n - 2):
            a, b = (a + b) % MOD, a
        return (3 * a + b) % MOD

    if n % 2 == 1:
        return 0

    n2 = n // 2
    ms = min(n2, min_seq // 2 + 1)

    dp = [0] * (n2 + 1)
    dp[0] = 1
    acc, reject = 1, 0
    for i in range(1, n2 + 1):
        dp[i] = (acc - reject) % MOD
        acc = (acc + dp[i]) % MOD
        if i >= ms:
            reject = (reject + dp[i - ms]) % MOD

    ans = 0
    for d in range(1, ms + 1):
        ans = (ans + dp[n2 - d] * d * 2) % MOD

    return ans


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

print(solve(n, m, s))
Buggy python submission (verdict: Runtime Error):
import sys
from collections import defaultdict

n = int(input())
aaa = list(map(int, sys.stdin))
MOD = 998244353

d2 = [1]
tmp = 1
for i in range(n):
    tmp = tmp * 2 % MOD
    d2.append(tmp)

s = sum(aaa)
dp = [defaultdict(lambda: 0) for _ in [0] * (s + 1)]
dp[0][0] = 1
tmp_sum = 0
for a in aaa:
    for i in range(tmp_sum, -1, -1):
        dd = dp[i]
        if not dd:
            continue
        for k, p in dd.items():
            dp[i + a][k + 1] += p
    tmp_sum += a

ans = pow(3, n, MOD)
sub = 0
h = (s + 1) // 2
for dd in dp[h:]:
    for k, p in dd.items():
        sub += d2[n - k] * p * 3
    sub %= MOD
if s % 2 == 0:
    dd = dp[h]
    for k, p in dd.items():
        sub -= p * 3

ans = (ans - sub) % MOD
print(ans)


Fixed submission:
n = int(input())
s = input()

f = [0]
tmp = 0
for c in s:
    if c == '#':
        tmp += 1
    f.append(tmp)

b = [0]
tmp = 0
for c in s[::-1]:
    if c == '.':
        tmp += 1
    b.append(tmp)
b.reverse()

# print(f)
# print(b)
print(min(map(sum, zip(f, b))))
Buggy python submission (verdict: Runtime Error):
import sys


def simulate(i, check_out_bound):
    for t, d in commands:
        if t == s[i]:
            if d == 'L':
                i -= 1
            else:
                i += 1
        if check_out_bound(i):
            return False
    return True


def check(is_left, check_out_bound):
    l, r = 0, n - 1
    while l < r:
        m = (l + r) // 2
        if simulate(m, check_out_bound):
            if is_left:
                r = m
            else:
                l = m
        else:
            if is_left:
                l = m + 1
            else:
                r = m - 1
        # print(is_left, l, r)
    return l


n, q = map(int, input().split())
s = input()
commands = [line.rstrip().split() for line in sys.stdin]

check_out_bound_l = lambda i: i < 0
check_out_bound_r = lambda i: i >= n
if simulate(0, check_out_bound_l):
    bl = 0
else:
    bl = check(True, check_out_bound_l)
if simulate(n - 1, check_out_bound_r):
    br = n - 1
else:
    br = check(False, check_out_bound_r)
print(br - bl + 1)


Fixed submission:
import sys

n, q = map(int, input().split())
s = input()
commands = [line.rstrip().split() for line in sys.stdin]
commands.reverse()

l, r = 0, n - 1
for t, d in commands:
    if s[l] == t and d == 'L':
        l += 1
    elif 0 < l and s[l - 1] == t and d == 'R':
        l -= 1
    if s[r] == t and d == 'R':
        r -= 1
    elif r < n - 1 and s[r + 1] == t and d == 'L':
        r += 1
    if l > r:
        break

print(r - l + 1)

Buggy python submission (verdict: Runtime Error):
import sys

n, q = map(int, input().split())
s = input()
commands = [line.rstrip().split() for line in sys.stdin]
commands.reverse()
lim_l, lim_r = 0, n - 1
ans = 0
for t, d in commands:
    if d == 'L':
        if t == s[lim_l]:
            ans += 1
            lim_l += 1
        elif lim_r < n - 1 and t == s[lim_r + 1]:
            ans -= 1
            lim_r += 1
    else:
        if t == s[lim_r]:
            ans += 1
            lim_r -= 1
        elif lim_l > 0 and t == s[lim_l - 1]:
            ans -= 1
            lim_l -= 1
    # print(t, d, lim_l, lim_r, ans)
print(n - ans)


Fixed submission:
import sys

n, q = map(int, input().split())
s = input()
commands = [line.rstrip().split() for line in sys.stdin]
commands.reverse()

l, r = 0, n - 1
for t, d in commands:
    if s[l] == t and d == 'L':
        l += 1
    elif 0 < l and s[l - 1] == t and d == 'R':
        l -= 1
    if s[r] == t and d == 'R':
        r -= 1
    elif r < n - 1 and s[r + 1] == t and d == 'L':
        r += 1
    if l > r:
        break

print(r - l + 1)
Buggy python submission (verdict: Time Limit Exceeded):
import sys


def solve(n, m, links):
    v4 = set()
    v6 = 0
    for v, link in enumerate(links):
        l = len(link)
        if l % 2 == 1:
            return False
        if l == 4:
            v4.add(v)
        elif l >= 6:
            v6 += 1
    if v6 > 0:
        return True
    if len(v4) > 2:
        return True
    if len(v4) < 2:
        return False
    s = v4.pop()
    t = v4.pop()
    visited = set()
    q = [s]
    while q:
        v = q.pop()
        if v == t:
            continue
        if v == s:
            if s in visited:
                return True
        elif v in visited:
            continue
        visited.add(s)
        q.extend(u for u in links[v] if u not in visited)
    return False


n, m = map(int, input().split())
links = [set() for _ in [0] * n]
for line in sys.stdin:
    a, b = map(int, line.split())
    a -= 1
    b -= 1
    links[a].add(b)
    links[b].add(a)
print('Yes' if solve(n, m, links) else 'No')


Fixed submission:
import sys


def solve(n, m, links):
    v4 = set()
    v6 = 0
    for v, link in enumerate(links):
        l = len(link)
        if l % 2 == 1:
            return False
        if l == 4:
            v4.add(v)
        elif l >= 6:
            v6 += 1
    if v6 > 0:
        return True
    if len(v4) > 2:
        return True
    if len(v4) < 2:
        return False
    s = v4.pop()
    t = v4.pop()
    for c in links[s]:
        visited = {s, t}
        q = [c]
        while q:
            v = q.pop()
            if v in visited:
                continue
            visited.add(v)
            for u in links[v]:
                if v != c and u == s:
                    return True
                if u not in visited:
                    q.append(u)
    return False


n, m = map(int, input().split())
links = [set() for _ in [0] * n]
for line in sys.stdin:
    a, b = map(int, line.split())
    a -= 1
    b -= 1
    links[a].add(b)
    links[b].add(a)
print('Yes' if solve(n, m, links) else 'No')
Buggy python submission (verdict: Runtime Error):
n, k = map(int, input().split())
ppp = list(map(int, input().split()))
qqq = list(map(int, input().split()))
ini_p = ppp.copy()
ini_q = qqq.copy()
i = 0
for i in range(k - 2):
    rrr = [q for p, q in sorted(zip(ppp, qqq))]
    if rrr == ini_p:
        break
    ppp = qqq
    qqq = rrr
else:
    print(*qqq)
    exit()

k = k % (i + 2)
ppp = ini_p
qqq = ini_q
for i in range(k - 2):
    rrr = [q for p, q in sorted(zip(ppp, qqq))]
    ppp = qqq
    qqq = rrr
print(*qqq)


Fixed submission:
from operator import itemgetter


def get_identity():
    return list(range(1, n + 1))


def composition(ppp, qqq):
    return [ppp[q - 1] for q in qqq]


def reverse_composition(ppp, qqq):
    return [ppp[i] for i, q in sorted(enumerate(qqq), key=itemgetter(1))]


def solve(k, ppp, qqq):
    qp = reverse_composition(qqq, ppp)
    qpq = reverse_composition(qp, qqq)
    qpqp = composition(qpq, ppp)
   
    l, m = divmod(k - 1, 6)
    res = get_identity()
    tmp = qpqp
    while l:
        if l % 2 == 1:
            res = composition(res, tmp)
        tmp = composition(tmp, tmp)
        l >>= 1

    m = (k - 1) % 6
    if m == 0:
        base = ppp
    elif m == 1:
        base = qqq
    elif m == 2:
        base = reverse_composition(qqq, ppp)
    elif m == 3:
        res = composition(res, qqq)
        base = reverse_composition(get_identity(), ppp)
    elif m == 4:
        res = composition(res, qqq)
        res = reverse_composition(res, ppp)
        base = reverse_composition(get_identity(), qqq)
    elif m == 5:
        res = composition(res, qqq)
        res = reverse_composition(res, ppp)
        base = reverse_composition(get_identity(), qqq)
        base = composition(base, ppp)
    else:
        raise NotImplementedError

    ans = composition(res, base)
    ans = reverse_composition(ans, res)
    return ans


n, k = map(int, input().split())
ppp = list(map(int, input().split()))
qqq = list(map(int, input().split()))
print(*solve(k, ppp, qqq))