Harryxun/testdata huggingface dataset:
Buggy python submission (verdict: Time Limit Exceeded):
def main():
    N = int(input())
    A = list(map(int, input().split()))
    mod = 10**9+7

    A_bin_cnt = [0] * 60
    for i in range(N):
        a = A[i]
        for j in range(60):
            if a >> j & 1:
                A_bin_cnt[j] += 1
    ans = 0
    for i in range(N):
        a = A[i]
        for j in range(60):
            if not a >> j & 1:
                ans = (ans + (pow(2, j, mod) * A_bin_cnt[j]) % mod) % mod
            else:
                ans = (ans + (pow(2, j, mod) * (N - A_bin_cnt[j])) % mod) % mod
    inv2 = pow(2, mod-2, mod)
    print((ans*inv2)%mod)


if __name__ == '__main__':
    main()


Fixed submission:
def main():
    N = int(input())
    A = list(map(int, input().split()))
    mod = 10**9+7

    A_bin_cnt = [0] * 60
    for i in range(N):
        a = A[i]
        for j in range(60):
            if a >> j & 1:
                A_bin_cnt[j] += 1

    ans = 0
    for i in range(60):
        ans = (ans + (pow(2, i, mod) * (A_bin_cnt[i] * (N - A_bin_cnt[i])))%mod) % mod
    print(ans)


if __name__ == '__main__':
    main()

Buggy python submission (verdict: Time Limit Exceeded):
N = int(input())
A = list(map(int, input().split()))
mod = 10**9+7

A_bin_cnt = [0] * 60
for i in range(N):
    a = A[i]
    for j in range(60):
        if a >> j & 1:
            A_bin_cnt[j] += 1
ans = 0
for i in range(N):
    a = A[i]
    for j in range(60):
        if not a >> j & 1:
            ans = (ans + (pow(2, j, mod) * A_bin_cnt[j]) % mod) % mod
        else:
            ans = (ans + (pow(2, j, mod) * (N - A_bin_cnt[j])) % mod) % mod
inv2 = pow(2, mod-2, mod)
print((ans*inv2)%mod)



Fixed submission:
def main():
    N = int(input())
    A = list(map(int, input().split()))
    mod = 10**9+7

    A_bin_cnt = [0] * 60
    for i in range(N):
        a = A[i]
        for j in range(60):
            if a >> j & 1:
                A_bin_cnt[j] += 1

    ans = 0
    for i in range(60):
        ans = (ans + (pow(2, i, mod) * (A_bin_cnt[i] * (N - A_bin_cnt[i])))%mod) % mod
    print(ans)


if __name__ == '__main__':
    main()
Buggy python submission (verdict: Runtime Error):
def main():
    import sys
    from collections import defaultdict
    input = sys.stdin.readline

    N, x, d = map(int, input().split())
    
    assert d != 0
    if d == 0:
        if x:
            print(N+1)
        else:
            print(0)
        exit()

    if d < 0:
        x = x + d * (N-1)
        d *= -1
    dic = defaultdict(list)

    for i in range(N+1):
        mod = (x * i) % d
        st = (i * (i+1)) // 2
        ed = (i * (N + N-i+1)) // 2
        st = st * d + (x-d) * i
        ed = ed * d + (x-d) * i
        dic[mod].append((st, ed))
    ans = 0
    for mod in dic:
        L = dic[mod]
        L.sort(key=lambda t: t[0])
        ed_prev = L[0][0] - 1
        for st, ed in L:
            if st > ed_prev:
                ans += (ed - st) // d + 1
                ed_prev = ed
            elif ed > ed_prev:
                ans += (ed - ed_prev) // d
                ed_prev = ed
    print(ans)


if __name__ == '__main__':
    main()


Fixed submission:
def main():
    import sys
    from collections import defaultdict
    input = sys.stdin.readline

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

    if d == 0:
        if x:
            print(N+1)
        else:
            print(1)
        exit()

    if d < 0:
        x = x + d * (N-1)
        d *= -1
    dic = defaultdict(list)

    for i in range(N+1):
        mod = (x * i) % d
        st = (i * (i+1)) // 2
        ed = (i * (N + N-i+1)) // 2
        st = st * d + (x-d) * i
        ed = ed * d + (x-d) * i
        dic[mod].append((st, ed))
    ans = 0
    for mod in dic:
        L = dic[mod]
        L.sort(key=lambda t: t[0])
        ed_prev = L[0][0] - 1
        for st, ed in L:
            if st > ed_prev:
                ans += (ed - st) // d + 1
                ed_prev = ed
            elif ed > ed_prev:
                ans += (ed - ed_prev) // d
                ed_prev = ed
    print(ans)


if __name__ == '__main__':
    main()

Buggy python submission (verdict: Runtime Error):
N, X, D = map(int, input().split())

M = (N * (N+1)) // 2 + 1
print(M) if X // D != 0 else 0


Fixed submission:
def main():
    import sys
    from collections import defaultdict
    input = sys.stdin.readline

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

    if d == 0:
        if x:
            print(N+1)
        else:
            print(1)
        exit()

    if d < 0:
        x = x + d * (N-1)
        d *= -1
    dic = defaultdict(list)

    for i in range(N+1):
        mod = (x * i) % d
        st = (i * (i+1)) // 2
        ed = (i * (N + N-i+1)) // 2
        st = st * d + (x-d) * i
        ed = ed * d + (x-d) * i
        dic[mod].append((st, ed))
    ans = 0
    for mod in dic:
        L = dic[mod]
        L.sort(key=lambda t: t[0])
        ed_prev = L[0][0] - 1
        for st, ed in L:
            if st > ed_prev:
                ans += (ed - st) // d + 1
                ed_prev = ed
            elif ed > ed_prev:
                ans += (ed - ed_prev) // d
                ed_prev = ed
    print(ans)


if __name__ == '__main__':
    main()
Buggy python submission (verdict: Time Limit Exceeded):
import heapq

n, k = map(int, input().split())
a = list(map(int, input().split()))
f = list(map(int, input().split()))
a.sort(reverse=True)
f.sort()

pq = []
for i in range(n):
    heapq.heappush(pq, (-a[i]*f[i], a[i], f[i]))

for i in range(k):
    score, ai, fi = heapq.heappop(pq)
    if score == 0:
        heapq.heappush(pq, (0, 0, 0))
        break
    heapq.heappush(pq, (-((ai-1) * fi), ai-1, fi))

print(-heapq.heappop(pq)[0])


Fixed submission:
n, k = map(int, input().split())
a = list(map(int, input().split()))
f = list(map(int, input().split()))
a.sort(reverse=True)
f.sort()

ori = [0] * n
for i in range(n):
    ori[i] = a[i]*f[i]

ok = max(ori)
ng = -1
mid = (ok+ng) // 2
while ok - ng > 1:
    cnt = 0
    flg = 0
    for i in range(n):
        if ori[i] > mid:
            cnt += (a[i] - mid // f[i])
        if cnt > k:
            flg = 1
            break
    if flg:
        ng = mid
    else:
        ok = mid
    mid = (ok + ng) // 2

print(ok)
Buggy python submission (verdict: Time Limit Exceeded):
from fractions import gcd

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

d = gcd(A, B)
ans = 1
k = 2
while d > 2:
    plus = 0
    while d % k == 0:
        d //= k
        plus = 1
    ans += plus
    k += 1
print(ans)


Fixed submission:
from fractions import gcd

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

d = gcd(A, B)
d_ori = d
ans = 1
k = 2
while d > 2:
    plus = 0
    while d % k == 0:
        d //= k
        plus = 1
    ans += plus
    k += 1
    if k > d_ori**(1/2):
        ans += 1
        break
print(ans)
Buggy python submission (verdict: Time Limit Exceeded):
import bisect

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

place = [-1] * (N + 1)
for i in range(N):
    p = P[i]
    place[p] = i

ans = 0
big_place = [place[N]]
for i in range(N - 1, 0, -1):
    pos = bisect.bisect_left(big_place, place[i])
    if pos < len(big_place):
        right = big_place[pos]
    else:
        right = N
    if pos + 1 < len(big_place):
        right2 = big_place[pos + 1]
    else:
        right2 = N
    if pos - 1 >= 0:
        left = big_place[pos - 1]
    else:
        left = -1
    if pos - 2 >= 0:
        left2 = big_place[pos - 2]
    else:
        left2 = -1

    if left >= 0:
        left_cand = left - left2
    else:
        left_cand = 0
    right_cand = right - place[i]

    ans += i * left_cand * right_cand

    if right < N:
        right_cand = right2 - right
    else:
        right_cand = 0
    left_cand = place[i] - left
    ans += i * left_cand * right_cand

    big_place1 = big_place[:pos]
    big_place2 = big_place[pos:]
    big_place = big_place1 + [place[i]] + big_place2

print(ans)


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

    # i+1番目からR番目まで足すとt以上となるような最小のRを返す
    def bisect_plus(self, i, t):
        L = i
        R = self.size
        R_prev = R
        sum_i = self.sum(i)
        while True:
            range_sum = self.sum(R) - sum_i
            if range_sum < t:
                if R == self.size:
                    return self.size + 1
                L, R = R, R_prev
            else:
                R_prev = R
                R = (L + R + 1) // 2
                if R == R_prev:
                    return R

    # L番目からi-1番目まで足すとt以上となるような最小のRを返す
    def bisect_minus(self, i, t):
        L = 1
        R = i
        L_prev = L
        sum_i = self.sum(i - 1)
        while True:
            range_sum = sum_i - self.sum(L - 1)
            if range_sum < t:
                if L == 1:
                    return 0
                L, R = L_prev, L
            else:
                L_prev = L
                L = (L + R) // 2
                if L == L_prev:
                    return L


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

place = [-1] * (N + 1)
for i in range(N):
    p = P[i]
    place[p] = i

ans = 0
big_place = Bit(N)
big_place.add(place[N] + 1, 1)
for i in range(N - 1, 0, -1):
    pos = place[i] + 1
    right = big_place.bisect_plus(pos, 1)
    right2 = big_place.bisect_plus(pos, 2)
    left = big_place.bisect_minus(pos, 1)
    left2 = big_place.bisect_minus(pos, 2)
    if left > 0:
        left_cand = left - left2
    else:
        left_cand = 0
    right_cand = right - pos
    ans += i * left_cand * right_cand

    if right < N + 1:
        right_cand = right2 - right
    else:
        right_cand = 0
    left_cand = pos - left
    ans += i * left_cand * right_cand

    big_place.add(pos, 1)

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

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

point = [0] * (N + 1)
for _ in range(Q):
    p, x = map(int, input().split())
    point[p] += x

ans = [0] * (N + 1)
queue = deque()
queue.append(1)
visited = [0] * (N + 1)
visited[1] = 1
while queue:
    v = queue.pop()
    if v == 1:
        ans[v] = point[v]
    for u in adj[v]:
        if visited[u] == 0:
            queue.append(u)
            visited[u] = 1
            ans[u] = ans[v] + point[u]

print(' '.join(map(str, ans[1:])))



Fixed submission:
from collections import deque

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

point = [0] * (N + 1)
for _ in range(Q):
    p, x = map(int, input().split())
    point[p] += x

ans = [0] * (N + 1)
queue = deque()
queue.append(1)
visited = [0] * (N + 1)
visited[1] = 1
ans[1] = point[1]
while queue:
    v = queue.pop()
    for u in adj[v]:
        if visited[u] == 0:
            queue.appendleft(u)
            visited[u] = 1
            ans[u] = ans[v] + point[u]

print(' '.join(map(str, ans[1:])))
Buggy python submission (verdict: Time Limit Exceeded):
def main():
    import sys
    input = sys.stdin.readline

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

    ans = [0] * p
    for i, a in enumerate(A):
        if not a:
            continue
        ans[-1] += 1
        for j in range(p):
            ans[j] = (ans[j] - pow(i, j, p))%p
    ans.reverse()
    print(*ans)


if __name__ == '__main__':
    main()


Fixed submission:
def main():
    import sys
    input = sys.stdin.readline

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

    ans = [0] * p
    for i, a in enumerate(A):
        if not a:
            continue
        ans[-1] += 1
        ij = 1
        ans[0] = (ans[0] - ij)%p
        for j in range(1, p):
            ij = (ij * i)%p
            ans[j] = (ans[j] - ij)%p
    ans.reverse()
    print(*ans)


if __name__ == '__main__':
    main()
Buggy python submission (verdict: Time Limit Exceeded):
S = input()
S = S[::-1]
mod = 10 ** 9 + 7

dp = [[0 for _ in range(13)] for _ in range(len(S) + 1)]
dp[0][0] = 1
for i, S_i in enumerate(S):
    rem13 = (10 ** i) % 13
    for j in range(13):
        if S_i == '?':
            for k in range(10):
                dp[i + 1][j] = (dp[i + 1][j] + dp[i][(j - rem13 * k) % 13]) % mod
        else:
            dp[i + 1][j] = (dp[i + 1][j] + dp[i][(j - rem13 * int(S_i)) % 13]) % mod

print(dp[-1][5])


Fixed submission:
S = input()
S = S[::-1]
mod = 10 ** 9 + 7

dp = [[0 for _ in range(13)] for _ in range(len(S) + 1)]
dp[0][0] = 1
rem13 = 1
for i in range(len(S)):
    rem13 = (10 ** (i % 6)) % 13
    for j in range(13):
        if S[i] == '?':
            for k in range(10):
                dp[i + 1][j] += dp[i][(j - rem13 * k) % 13]
        else:
            dp[i + 1][j] += dp[i][(j - rem13 * int(S[i])) % 13]
    for j in range(13):
        dp[i+1][j] %= mod

print(dp[-1][5])
Buggy python submission (verdict: Time Limit Exceeded):
import bisect

N = int(input())

largest = []
for i in range(N):
    A_i = int(input())
    idx = bisect.bisect_left(largest, A_i)
    if idx == 0:
        largest.insert(0, A_i)
    else:
        largest[idx - 1] = A_i
print(len(largest))


Fixed submission:
import bisect
from collections import deque

N = int(input())

largest = deque([])
for i in range(N):
    A_i = int(input())
    idx = bisect.bisect_left(largest, A_i)
    if idx == 0:
        largest.appendleft(A_i)
    else:
        largest[idx - 1] = A_i
print(len(largest))
Buggy python submission (verdict: Runtime Error):
L, R = map(int, input().split())
mod = 2019

L = L % mod
R = R % mod
if L >= R:
    print(0)
else:
    ans = float('inf')
    for i in range(L, R+1):
        for j in range(i+1, R+1):
            ans = min(ans, (i*j)%mod)
print(ans)


Fixed submission:
L, R = map(int, input().split())
mod = 2019

if R - L >= mod:
    print(0)
else:
    L = L % mod
    R = R % mod
    if L >= R:
        print(0)
    else:
        ans = float('inf')
        for i in range(L, R+1):
            for j in range(i+1, R+1):
                ans = min(ans, (i * j) % mod)
        print(ans)
Buggy python submission (verdict: Time Limit Exceeded):
N, K = map(int, input().split())
mod = 10 ** 9 + 7

dp = [[[[0 for _ in range(K + 1)] for _ in range(2)] for _ in range(K + 1)] for _ in range(N - K + 1)]
for r in range(N - K + 1):
    for b in range(K + 1):
        for i in range(b + 1):
            if b == 0 and i == 0:
                dp[r][b][0][i] = 1
                dp[r][b][1][i] = 1
            if r > 0:
                dp[r][b][0][i] = (dp[r-1][b][0][i] + dp[r-1][b][1][i]) % mod
            if b > 0 and i > 0:
                dp[r][b][1][i] = (dp[r][b-1][0][i-1] + dp[r][b-1][1][i]) % mod

for i in range(1, K+1):
    print(dp[N-K][K][1][i])


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

from math import factorial

# comb init
nmax = 2000 + 1  # change here
fac = [0] * (nmax + 1)
finv = [0] * (nmax + 1)
inv = [0] * (nmax + 1)
fac[0] = 1
fac[1] = 1
finv[0] = 1
finv[1] = 1
inv[1] = 1
for i in range(2, nmax):
    fac[i] = fac[i - 1] * i % mod
    inv[i] = mod - inv[mod % i] * (mod // i) % mod
    finv[i] = finv[i - 1] * inv[i] % mod


def comb(n, r):
    if n < r:
        return 0
    else:
        return (fac[n] * ((finv[r] * finv[n - r]) % mod)) % mod


r = N - K
for b in range(1, K+1):
    print((comb(r+1, b) * comb(K - 1, b - 1)) % mod)

Buggy python submission (verdict: Time Limit Exceeded):
N, K = map(int, input().split())
mod = 10 ** 9 + 7

dp = [[[[0 for _ in range(K + 1)] for _ in range(2)] for _ in range(K + 1)] for _ in range(N - K + 1)]
for r in range(N - K + 1):
    for b in range(K + 1):
        for i in range(K + 1):
            if b == 0 and i == 0:
                dp[r][b][0][i] = 1
                dp[r][b][1][i] = 1
            if r > 0:
                dp[r][b][0][i] = (dp[r-1][b][0][i] + dp[r-1][b][1][i]) % mod
            if b > 0 and i > 0:
                dp[r][b][1][i] = (dp[r][b-1][0][i-1] + dp[r][b-1][1][i]) % mod

for i in range(1, K+1):
    print(dp[N-K][K][1][i])


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

from math import factorial

# comb init
nmax = 2000 + 1  # change here
fac = [0] * (nmax + 1)
finv = [0] * (nmax + 1)
inv = [0] * (nmax + 1)
fac[0] = 1
fac[1] = 1
finv[0] = 1
finv[1] = 1
inv[1] = 1
for i in range(2, nmax):
    fac[i] = fac[i - 1] * i % mod
    inv[i] = mod - inv[mod % i] * (mod // i) % mod
    finv[i] = finv[i - 1] * inv[i] % mod


def comb(n, r):
    if n < r:
        return 0
    else:
        return (fac[n] * ((finv[r] * finv[n - r]) % mod)) % mod


r = N - K
for b in range(1, K+1):
    print((comb(r+1, b) * comb(K - 1, b - 1)) % mod)
Buggy python submission (verdict: Runtime Error):
def main():
    import sys
    from collections import deque
    input = sys.stdin.buffer.readline

    N, M = map(int, input().split())
    adj = [[] for _ in range(N*3+5)]
    for _ in range(M):
        u, v = map(int, input().split())
        adj[u].append(v+N+1)
        adj[u+N+1].append(v+2*N+2)
        adj[u+2*N+2].append(v)

    s, t = map(int, input().split())
    que = deque()
    que.append(s)
    seen = [-1] * (N*3+5)
    seen[1] = 0
    while que:
        v = que.popleft()
        for u in adj[v]:
            if seen[u] == -1:
                seen[u] = seen[v] + 1
                que.append(u)

    if seen[t] == -1:
        print(-1)
    else:
        assert seen[t] % 3 == 0
        print(seen[t] // 3)


if __name__ == '__main__':
    main()


Fixed submission:
def main():
    import sys
    from collections import deque
    input = sys.stdin.buffer.readline

    N, M = map(int, input().split())
    adj = [[] for _ in range(N*3+5)]
    for _ in range(M):
        u, v = map(int, input().split())
        adj[u].append(v+N+1)
        adj[u+N+1].append(v+2*N+2)
        adj[u+2*N+2].append(v)

    s, t = map(int, input().split())
    que = deque()
    que.append(s)
    seen = [-1] * (N*3+5)
    seen[s] = 0
    while que:
        v = que.popleft()
        for u in adj[v]:
            if seen[u] == -1:
                seen[u] = seen[v] + 1
                que.append(u)

    if seen[t] == -1:
        print(-1)
    else:
        assert seen[t] % 3 == 0
        print(seen[t] // 3)


if __name__ == '__main__':
    main()
Buggy python submission (verdict: Runtime Error):
from collections import Counter


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

vector = [0] * ((N * (N - 1)) // 2)
cnt = 0
for i in range(N):
    for j in range(i + 1, N):
        x = ball[i][0] - ball[j][0]
        y = ball[i][1] - ball[j][1]
        if x < 0:
            x, y = -x, -y
        elif x == 0:
            if y < 0:
                y = -y
        vector[cnt] = x * (10 ** 10) + y
        cnt += 1
C = Counter(vector)
print(N - C.most_common()[0][1])

Fixed submission:
from collections import Counter


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

vector = [0] * ((N * (N - 1)) // 2)
cnt = 0
for i in range(N):
    for j in range(i + 1, N):
        x = ball[i][0] - ball[j][0]
        y = ball[i][1] - ball[j][1]
        if x < 0:
            x, y = -x, -y
        elif x == 0:
            if y < 0:
                y = -y
        vector[cnt] = x * (10 ** 10) + y
        cnt += 1
C = Counter(vector)
if N == 1:
    print(1)
else:
    print(N - C.most_common()[0][1])
Buggy python submission (verdict: Runtime Error):
from collections import deque


N = int(input())
A = list(map(int, input().split()))
A.sort()
queue_plus = deque()
queue_minus = deque()
for A_i in A:
    if A_i < 0:
        queue_minus.append(A_i)
    else:
        queue_plus.append(A_i)

xy = [[] for _ in range(N-1)]
cnt = 0
if len(queue_plus) == 0:
    x = queue_minus.pop()
    y = queue_minus.pop()
    xy[cnt] = [x, y]
    cnt += 1
    queue_plus.append(x - y)
if len(queue_minus) == 0:
    x = queue_plus.popleft()
    y = queue_plus.popleft()
    xy[cnt] = [x, y]
    cnt += 1
    queue_minus.append(x - y)
while len(queue_plus) > 0 and len(queue_minus) > 1:
    x = queue_plus.pop()
    y = queue_minus.pop()
    xy[cnt] = [x, y]
    cnt += 1
    queue_plus.append(x - y)
while len(queue_plus) > 1 and len(queue_minus) > 0:
    x = queue_minus.pop()
    y = queue_plus.pop()
    xy[cnt] = [x, y]
    cnt += 1
    queue_minus.append(x - y)
x = queue_plus.pop()
y = queue_minus.pop()
xy[cnt] = [x, y]
ans = x - y
print(ans)
for x_y in xy:
    print(" ".join(map(str, x_y)))


Fixed submission:
from collections import deque


N = int(input())
A = list(map(int, input().split()))
A.sort()
queue_plus = deque()
queue_minus = deque()
for A_i in A:
    if A_i < 0:
        queue_minus.append(A_i)
    else:
        queue_plus.append(A_i)

xy = [[] for _ in range(N-1)]
cnt = 0
if len(queue_plus) == 0:
    x = queue_minus.pop()
    y = queue_minus.pop()
    xy[cnt] = [x, y]
    cnt += 1
    queue_plus.append(x - y)
elif len(queue_minus) == 0:
    x = queue_plus.popleft()
    y = queue_plus.popleft()
    xy[cnt] = [x, y]
    cnt += 1
    queue_minus.append(x - y)
while len(queue_plus) > 0 and len(queue_minus) > 1:
    x = queue_plus.pop()
    y = queue_minus.pop()
    xy[cnt] = [x, y]
    cnt += 1
    queue_plus.append(x - y)
while len(queue_plus) > 1 and len(queue_minus) > 0:
    x = queue_minus.pop()
    y = queue_plus.pop()
    xy[cnt] = [x, y]
    cnt += 1
    queue_minus.append(x - y)
if len(queue_plus) + len(queue_minus) == 1:
    if len(queue_plus) > 0:
        print(queue_plus.pop())
    else:
        print(queue_minus.pop())
else:
    x = queue_plus.pop()
    y = queue_minus.pop()
    xy[cnt] = [x, y]
    ans = x - y
    print(ans)
for x_y in xy:
    print(" ".join(map(str, x_y)))
Buggy python submission (verdict: Time Limit Exceeded):
N = int(input())
A_rate = list(map(int, input().split()))
B_rate = list(map(int, input().split()))


def trade(d, buy_rate, sell_rate):
    stuff = []
    for i in range(3):
        stuff.extend([[buy_rate[i], sell_rate[i]] for _ in range((d // buy_rate[i]))])
    stuff.extend([[1, 1] for _ in range(d)])
    dp = [[0 for j in range(d+1)] for i in range((len(stuff) + 1))]
    for i in range(1, len(stuff) + 1):
        for j in range(d+1):
            if j-stuff[i-1][0] >= 0:
                dp[i][j] = max(dp[i-1][j], dp[i-1][j-stuff[i-1][0]] + stuff[i-1][1])
            else:
                dp[i][j] = dp[i-1][j]
    return max(dp[-1])


print(trade(trade(N, A_rate, B_rate), B_rate, A_rate))


Fixed submission:
mod = 1000000007
eps = 10**-9
inf = 10**9


def main():
    import sys
    input = sys.stdin.readline

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

    dp = [[-inf] * (N+1) for _ in range(4)]
    dp[0][N] = N
    for i in range(3):
        for j in range(N, -1, -1):
            dp[i+1][j] = max(dp[i+1][j], dp[i][j])
            if j - A[i] >= 0:
                dp[i+1][j-A[i]] = max(dp[i+1][j-A[i]], dp[i+1][j] + B[i] - A[i])
    N = max(dp[-1])
    dp = [[-inf] * (N + 1) for _ in range(4)]
    dp[0][N] = N
    for i in range(3):
        for j in range(N, -1, -1):
            dp[i + 1][j] = max(dp[i + 1][j], dp[i][j])
            if j - B[i] >= 0:
                dp[i + 1][j - B[i]] = max(dp[i + 1][j - B[i]], dp[i+1][j] + A[i] - B[i])
    print(max(dp[-1]))


if __name__ == '__main__':
    main()