Harryxun/testdata huggingface dataset:
Buggy python submission (verdict: Time Limit Exceeded):
def main():
    N = int(input())
    a = list(map(int, input().split()))
    l = [[10**9+1] * (N+1) for _ in range(N+1)]
    s = [[0] * (N+1) for _ in range(N+1)]
    for i in range(N):
        for j in range(N+1):
            s[i][j] = sum(a[k] for k in range(i, j))
    for i in range(N):
        l[i][i+1] = 0
    for j in range(2, N+1):
        for i in range(N):
            m = i + j
            if m > N:
                continue
            l[i][m] = min(l[i][k] + l[k][m] for k in range(i+1, m)) + s[i][m]
    return l[0][-1]

print(main())


Fixed submission:
def main():
    N = int(input())
    a = list(map(int, input().split()))
    l = [[10**9+1] * (N+1) for _ in range(N+1)]
    s = [[0] * (N+1) for _ in range(N+1)]
    for i in range(N-1):
        for j in range(i+1, N+1):
            s[i][j] = s[i][j-1] + a[j-1]
    for i in range(N):
        l[i][i+1] = 0
    for j in range(2, N+1):
        for i in range(N):
            m = i + j
            if m > N:
                continue
            l[i][m] = min(l[i][k] + l[k][m] for k in range(i+1, m)) + s[i][m]
    return l[0][-1]

print(main())

Buggy python submission (verdict: Time Limit Exceeded):
def main():
    N = int(input())
    a = list(map(int, input().split()))
    l = [[10**9+1] * (N+1) for _ in range(N+1)]
    for i in range(N):
        l[i][i+1] = 0
    for j in range(2, N+1):
        for i in range(N):
            m = i + j
            if m > N:
                continue
            l[i][m] = min(l[i][k] + l[k][m] for k in range(i+1, m)) + sum(a[k] for k in range(i, m))
    return l[0][-1]

print(main())


Fixed submission:
def main():
    N = int(input())
    a = list(map(int, input().split()))
    l = [[10**9+1] * (N+1) for _ in range(N+1)]
    s = [[0] * (N+1) for _ in range(N+1)]
    for i in range(N-1):
        for j in range(i+1, N+1):
            s[i][j] = s[i][j-1] + a[j-1]
    for i in range(N):
        l[i][i+1] = 0
    for j in range(2, N+1):
        for i in range(N):
            m = i + j
            if m > N:
                continue
            l[i][m] = min(l[i][k] + l[k][m] for k in range(i+1, m)) + s[i][m]
    return l[0][-1]

print(main())
Buggy python submission (verdict: Runtime Error):
import sys
sys.setrecursionlimit(10000000)
def main():
    N = int(input())
    d = {}
    v = set()
    for _ in range(N-1):
        x, y = map(int, input().split())
        if x in d:
            d[x].append(y)
        else:
            d[x] = [y]
        if y in d:
            d[y].append(x)
        else:
            d[y] = [x]
    w, b = dfs(1, d, v)
    return w + b
def dfs(node, d, v):
    v.add(node)
    l = []
    for i in d[node]:
        if i in v:
            continue
        w, b = dfs(i, d, v)
        l.append((w,b))
    w = 1
    b = 1
    for tw, tb in l:
        w *= (tw + tb)
        b *= tw
    return w, b

print(main() % (10**9 + 7))


Fixed submission:
import sys
sys.setrecursionlimit(10000000)
def main():
    N = int(input())
    d = {}
    v = set()
    if N == 1:
        return 2
    for _ in range(N-1):
        x, y = map(int, input().split())
        if x in d:
            d[x].append(y)
        else:
            d[x] = [y]
        if y in d:
            d[y].append(x)
        else:
            d[y] = [x]
    w, b = dfs(1, d, v)
    return w + b
def dfs(node, d, v):
    v.add(node)
    l = []
    for i in d[node]:
        if i in v:
            continue
        w, b = dfs(i, d, v)
        l.append((w,b))
    w = 1
    b = 1
    for tw, tb in l:
        w *= (tw + tb)
        b *= tw
    return w, b

print(main() % (10**9 + 7))

Buggy python submission (verdict: Runtime Error):
import sys
sys.setrecursionlimit(10000000)
def main():
    N = int(input())
    d = {}
    v = set()
    for _ in range(N-1):
        x, y = map(int, input().split())
        if x in d:
            d[x].append(y)
        else:
            d[x] = [y]
        if y in d:
            d[y].append(x)
        else:
            d[y] = [x]
    w, b = dfs(1, d, v)
    return w + b
def dfs(node, d, v):
    v.add(node)
    l = []
    for i in d[node]:
        if i in v:
            continue
        w, b = dfs(i, d, v)
        l.append((w,b))
    w = 1
    b = 1
    for tw, tb in l:
        w *= (tw + tb)
        b *= tw
    return w, b

print(main() % (10**9 + 7))


Fixed submission:
import sys
sys.setrecursionlimit(10000000)
def main():
    N = int(input())
    d = {}
    v = set()
    if N == 1:
        return 2
    for _ in range(N-1):
        x, y = map(int, input().split())
        if x in d:
            d[x].append(y)
        else:
            d[x] = [y]
        if y in d:
            d[y].append(x)
        else:
            d[y] = [x]
    w, b = dfs(1, d, v)
    return w + b
def dfs(node, d, v):
    v.add(node)
    l = []
    for i in d[node]:
        if i in v:
            continue
        w, b = dfs(i, d, v)
        l.append((w,b))
    w = 1
    b = 1
    for tw, tb in l:
        w *= (tw + tb)
        b *= tw
    return w, b

print(main() % (10**9 + 7))
Buggy python submission (verdict: Time Limit Exceeded):
def main():
    N = int(input())
    A = list(map(int, input().split()))
    A.sort(reverse=True)
    d = {}
    for i in A:
        if i in d:
            d[i] += 1
        else:
            d[i] = 1
    r = 0
    for i in A:
        if d[i] == 0:
            continue
        for j in reversed(range(1,32)):
            t = pow(2, j) - i
            if t == i:
                if d[t] > 1:
                    d[t] -= 2
                    r += 1
            elif t in d and d[t] > 0:
                d[t] -= 1
                d[i] -= 1
                r += 1
    return r
print(main())


Fixed submission:
def main():
    N = int(input())
    A = list(map(int, input().split()))
    A.sort(reverse=True)
    d = {}
    for i in A:
        if i in d:
            d[i] += 1
        else:
            d[i] = 1
    r = 0
    for i in A:
        if d[i] == 0:
            continue
        for j in reversed(range(1,32)):
            t = pow(2, j) - i
            if t == i:
                if d[t] > 1:
                    d[t] -= 2
                    r += 1
            elif t in d and d[t] > 0:
                d[t] -= 1
                d[i] -= 1
                r += 1
    return r
print(main())
Buggy python submission (verdict: Runtime Error):
import fractions
from functools import reduce
def main():
    N, X = map(int, input().split())
    x = list(map(int, input().split()))
    x.sort()
    l = []
    for i in range(N-1):
        l.append(x[i+1]-x[i])
    print(reduce(fractions.gcd, l))
main()


Fixed submission:
import fractions
from functools import reduce
def main():
    N, X = map(int, input().split())
    x = list(map(int, input().split()))
    x.append(X)
    x.sort()
    l = []
    for i in range(N):
        l.append(x[i+1]-x[i])
    print(reduce(fractions.gcd, l))
main()

Buggy python submission (verdict: Runtime Error):
import math
from functools import reduce
def main():
    N, X = map(int, input().split())
    x = list(map(int, input().split()))
    x.sort()
    l = []
    for i in range(N-1):
        l.append(x[i+1]-x[i])
    print(reduce(math.gcd, l))
main()


Fixed submission:
import fractions
from functools import reduce
def main():
    N, X = map(int, input().split())
    x = list(map(int, input().split()))
    x.append(X)
    x.sort()
    l = []
    for i in range(N):
        l.append(x[i+1]-x[i])
    print(reduce(fractions.gcd, l))
main()
Buggy python submission (verdict: Runtime Error):
def main():
    N, K = map(int, input().split())
    x = list(map(int, input().split()))
    s = N * (10 ** 8)
    for i in range(N-K):
        if x[i] * x[K - 1 + i] < 0:
            s = min(s, min(x[K - 1 + i], abs(x[i])) * 2 + max(x[K - 1 + i], abs(x[i])))
        else:
            s = min(s, max(abs(x[K - 1 + i], x[i])))
    print(s)
main()


Fixed submission:
def main():
    N, K = map(int, input().split())
    x = list(map(int, input().split()))
    s = 5 * (10 ** 8)
    for i in range(N-K+1):
        if x[i] * x[K - 1 + i] < 0:
            s = min(s, min(x[K - 1 + i], abs(x[i])) * 2 + max(x[K - 1 + i], abs(x[i])))
        else:
            s = min(s, max(abs(x[K - 1 + i]), abs(x[i])))
    print(s)
main()
Buggy python submission (verdict: Runtime Error):
import numpy as np
def main():
    N = int(input())
    A = map(int, input().split())
    l = []
    for i, v in enumerate(A):
        l.append(v-i-1)
    x = int(np.median(l))
    y = x + 1
    tx = 0
    ty = 0
    for i in l:
        tx += abs(i-x)
        ty += abs(i-y)
    print(min(ty, tx))
main()


Fixed submission:
import numpy as np
def main():
    N = int(input())
    A = map(int, input().split())
    l = []
    for i, v in enumerate(A):
        l.append(v-i-1)
    x = int(np.median(l))
    y = x + 1
    tx = 0
    ty = 0
    for i in l:
        tx += abs(i-x)
        ty += abs(i-y)
    print(min(ty, tx))
main()
Buggy python submission (verdict: Time Limit Exceeded):
#dame datta
def main():
    N = int(input())
    A = list(map(int, input().split()))
    Al = [A[0]]
    r = (10**9)*(2*(10**5))
    for i in A[1:]:
        Al.append(Al[-1] + i)
    for t in range(2, N-1):
        t1 = -1
        m = (10**9)*(2*(10**5))
        for i in range(1,t):
            ii = abs(Al[t-1] - Al[i-1] - Al[i-1])
            if m > ii:
                t1 = i
                m = ii
        t2 = -1
        m = (10**9)*(2*(10**5))
        for i in range(t+1, N):
            ii = abs(Al[-1] - Al[i-1] - Al[i-1] + Al[t-1])
            if m > ii:
                t2 = i
                m = ii
        x = Al[-1] - Al[t2-1]
        y = Al[t2-1] - Al[t-1]
        z = Al[t-1] - Al[t1-1]
        w = Al[t1-1]
        r = min(max(x,y,z,w) - min(x,y,z,w), r)
    return r
print(main())


Fixed submission:
#dame datta
def binsearch(l, r, fn):
    while r - l > 1:
        m = (l + r) // 2
        t = fn(m+1) - fn(m)
        if t == 0:
            return fn(m)
        if t < 0:
            l = m + 1
        else:
            r = m
    if fn(l) < fn(r):
        return l
    else:
        return r
def main():
    N = int(input())
    A = list(map(int, input().split()))
    Al = [A[0]]
    r = (10**9)*(2*(10**5))
    for i in A[1:]:
        Al.append(Al[-1] + i)
    for t in range(2, N-1):
        def fn1(i):
            return abs(Al[t-1] - Al[i-1] - Al[i-1])
        t1 = binsearch(1, t, fn1)
        def fn2(i):
            return abs(Al[-1] - Al[i-1] - Al[i-1] + Al[t-1])
        t2 = binsearch(t+1, N, fn2)
        x = Al[-1] - Al[t2-1]
        y = Al[t2-1] - Al[t-1]
        z = Al[t-1] - Al[t1-1]
        w = Al[t1-1]
        r = min(max(x,y,z,w) - min(x,y,z,w), r)
    return r
print(main())
Buggy python submission (verdict: Runtime Error):
def main():
    N, M = map(int, input().split())
    for _ in range(N):
        x, y, z = map(int, input().split())
        l.append((x,y,z))
    r = 0
    l2 = [i+j+k for i, j, k in l]
    l2.sort(reverse=True)
    r = max(r, sum(l2[:M]))
    l2 = [i+j-k for i, j, k in l]
    l2.sort(reverse=True)
    r = max(r, sum(l2[:M]))
    l2 = [i-j+k for i, j, k in l]
    l2.sort(reverse=True)
    r = max(r, sum(l2[:M]))
    l2 = [i-j-k for i, j, k in l]
    l2.sort(reverse=True)
    r = max(r, sum(l2[:M]))
    l2 = [-i+j+k for i, j, k in l]
    l2.sort(reverse=True)
    r = max(r, sum(l2[:M]))
    l2 = [-i+j-k for i, j, k in l]
    l2.sort(reverse=True)
    r = max(r, sum(l2[:M]))
    l2 = [-i-j+k for i, j, k in l]
    l2.sort(reverse=True)
    r = max(r, sum(l2[:M]))
    l2 = [-i-j-k for i, j, k in l]
    l2.sort(reverse=True)
    r = max(r, sum(l2[:M]))
    return r
print(main())


Fixed submission:
def main():
    N, M = map(int, input().split())
    l = []
    for _ in range(N):
        x, y, z = map(int, input().split())
        l.append((x,y,z))
    r = 0
    l2 = [i+j+k for i, j, k in l]
    l2.sort(reverse=True)
    r = max(r, sum(l2[:M]))
    l2 = [i+j-k for i, j, k in l]
    l2.sort(reverse=True)
    r = max(r, sum(l2[:M]))
    l2 = [i-j+k for i, j, k in l]
    l2.sort(reverse=True)
    r = max(r, sum(l2[:M]))
    l2 = [i-j-k for i, j, k in l]
    l2.sort(reverse=True)
    r = max(r, sum(l2[:M]))
    l2 = [-i+j+k for i, j, k in l]
    l2.sort(reverse=True)
    r = max(r, sum(l2[:M]))
    l2 = [-i+j-k for i, j, k in l]
    l2.sort(reverse=True)
    r = max(r, sum(l2[:M]))
    l2 = [-i-j+k for i, j, k in l]
    l2.sort(reverse=True)
    r = max(r, sum(l2[:M]))
    l2 = [-i-j-k for i, j, k in l]
    l2.sort(reverse=True)
    r = max(r, sum(l2[:M]))
    return r
print(main())
Buggy python submission (verdict: Runtime Error):
def main():
    n, m = map(int, input().split())
    w = [0] * n
    v = [0] * n
    for i in range(n):
        wt, vt = list(map(int, input().split()))
        w[i] = wt
        v[i] = vt
    l = [0] * (m + 1)
    for i in range(n):
        l= [max(v[i] + l[j-w[i]], l[j]) if j - w[i] >= 0 else l[j] for j in range(m+1)]
    print(l[-1])
main()


Fixed submission:
#dame datta
def main():
    N = int(input())
    A = list(map(int, input().split()))
    l = 0
    ans = 0
    t = 0
    for i, v in enumerate(A):
        if t & v == 0:
            t |= v
        else:
            while t & v > 0:
                t ^= A[l]
                l += 1
            t |= v
        ans += i - l + 1
    return ans
print(main())
Buggy python submission (verdict: Time Limit Exceeded):
def main():
    N, K = map(int, input().split())
    l = []
    xl = []
    yl = []
    for _ in range(N):
        x, y = map(int, input().split())
        l.append((x, y))
        xl.append(x)
        yl.append(y)
    xl.sort()
    yl.sort()
    xd = {}
    dx = {}
    t = 0
    p = xl[0]
    for i in xl:
        if i != p:
            t += 1
        xd[i] = t
        dx[t] = i
        p = i
    yd = {}
    dy = {}
    t = 0
    p = yl[0]
    for i in yl:
        if i != p:
            t += 1
        yd[i] = t
        dy[t] = i
        p = i
    l2 = []
    for x, y in l:
        l2.append((xd[x], yd[y]))
    m = 10**100
    for si in range(len(xd)-1):
        for ei in range(si+1, len(xd)):
            for sj in range(len(yd)-1):
                for ej in range(sj+1, len(yd)):
                    t = 0
                    for x, y in l2:
                        if si <= x <= ei and sj <= y <= ej:
                            t += 1
                    if t >= K:
                        m = min(m, (dx[ei]-dx[si]) * (dy[ej]-dy[sj]))
    return m
print(main())
                


Fixed submission:
def main():
    N, K = map(int, input().split())
    l = []
    xl = []
    yl = []
    for _ in range(N):
        x, y = map(int, input().split())
        l.append((x, y))
        xl.append(x)
        yl.append(y)
    xl.sort()
    yl.sort()
    xd = {}
    dx = {}
    t = 0
    p = xl[0]
    for i in xl:
        if i != p:
            t += 1
        xd[i] = t
        dx[t] = i
        p = i
    yd = {}
    dy = {}
    t = 0
    p = yl[0]
    for i in yl:
        if i != p:
            t += 1
        yd[i] = t
        dy[t] = i
        p = i
    l2 = []
    for x, y in l:
        l2.append((xd[x], yd[y]))
    m = 10**100
    for si in range(len(xd)-1):
        for ei in range(si+1, len(xd)):
            for sj in range(len(yd)-1):
                for ej in range(sj+1, len(yd)):
                    t = 0
                    for x, y in l2:
                        if si <= x <= ei and sj <= y <= ej:
                            t += 1
                    if t >= K:
                        m = min(m, (dx[ei]-dx[si]) * (dy[ej]-dy[sj]))
    return m
print(main())
Buggy python submission (verdict: Runtime Error):
def main():
    N, W = map(int, input().split())
    l = []
    w, v = map(int, input().split())
    ww = w
    dp = [[[0] * (3*N) for _ in range(N+1) ]for _ in range(N)]
    dp[0][1][0] = v
    l.append((0, v))
    for _ in range(N-1):
        w, v = map(int, input().split())
        l.append((w-ww, v))
    if W < ww:
        return 0
    for i in range(1, N):
        w, v = l[i]
        for j in range(1, N+1):
            for k in range(w):
                dp[i][j][k] = dp[i-1][j][k]
            for k in range(w, 3*N):
                dp[i][j][k] = max(dp[i-1][j][k], dp[i-1][j-1][k-w] + v)
    r = 0
    for i in range(N+1):
        wt = W - i * ww
        r = max(r, max(dp[-1][i][:wt+1]))
    return r
print(main())


Fixed submission:
def main():
    N, W = map(int, input().split())
    l = []
    w, v = map(int, input().split())
    ww = w
    dp = [[[0] * (3*N) for _ in range(N+1) ]for _ in range(N)]
    dp[0][1][0] = v
    l.append((0, v))
    for _ in range(N-1):
        w, v = map(int, input().split())
        l.append((w-ww, v))
    if W < ww:
        return 0
    for i in range(1, N):
        w, v = l[i]
        for j in range(1, N+1):
            for k in range(w):
                dp[i][j][k] = dp[i-1][j][k]
            for k in range(w, 3*N):
                dp[i][j][k] = max(dp[i-1][j][k], dp[i-1][j-1][k-w] + v)
    r = 0
    for i in range(N+1):
        wt = W - i * ww
        if wt < 0:
            break
        r = max(r, max(dp[-1][i][:wt+1]))
    return r
print(main())
Buggy python submission (verdict: Runtime Error):
class SegTree:
    def __init__(self, a, func):
        self.n = 2**(len(a) - 1).bit_length()
        self.f = func
        self.d = [0] * (2 * self.n - 1)
        self.d[self.n - 1:self.n - 1 + len(a)] = a
        for i in reversed(range(self.n - 1)):
            self.d[i] = self.f(self.d[2 * i + 1], self.d[2 * i + 2])

    def update(self, i, v):
        i += self.n - 1
        self.d[i] = v
        while i:
            i = (i - 1) // 2
            self.d[i] = self.f(self.d[2 * i + 1], self.d[2 * i + 2])
    # l <= i < r
    def query(self, l, r):
        assert l <= r
        l += self.n
        r += self.n
        s = self.d[l - 1]
        while l < r:
            if l & 1:
                s = self.f(s, self.d[l - 1])
                l += 1
            if r & 1:
                r -= 1
                s = self.f(s, self.d[r - 1])
            l //= 2
            r //= 2
        return s
def main():
    N, T = map(int, input().split())
    A = list(map(int, input().split()))
    def func(a, b):
        if a >= b:
            return a
        return b
    s = SegTree(A, func)
    d = {}
    for i in range(N):
        t = s.query(i+1, N) - A[i]
        d[t] = d.get(t, 0) + 1
    m = 0
    for i in d:
        if i > m:
            m = i
    return d[m]
print(main())


Fixed submission:
class SegTree:
    def __init__(self, a, func):
        self.n = 2**(len(a) - 1).bit_length()
        self.f = func
        self.d = [0] * (2 * self.n - 1)
        self.d[self.n - 1:self.n - 1 + len(a)] = a
        for i in reversed(range(self.n - 1)):
            self.d[i] = self.f(self.d[2 * i + 1], self.d[2 * i + 2])

    def update(self, i, v):
        i += self.n - 1
        self.d[i] = v
        while i:
            i = (i - 1) // 2
            self.d[i] = self.f(self.d[2 * i + 1], self.d[2 * i + 2])
    # l <= i < r
    def query(self, l, r):
        assert l <= r
        l += self.n
        r += self.n
        s = self.d[l - 1]
        while l < r:
            if l & 1:
                s = self.f(s, self.d[l - 1])
                l += 1
            if r & 1:
                r -= 1
                s = self.f(s, self.d[r - 1])
            l //= 2
            r //= 2
        return s
def main():
    N, T = map(int, input().split())
    A = list(map(int, input().split()))
    if N == 2:
        return 1
    def func(a, b):
        if a >= b:
            return a
        return b
    s = SegTree(A, func)
    d = {}
    for i in range(N):
        t = s.query(i+1, N) - A[i]
        d[t] = d.get(t, 0) + 1
    m = 0
    for i in d:
        if i > m:
            m = i
    return d[m]
print(main())
Buggy python submission (verdict: Runtime Error):
nmx=list(map(int, input().split()))
N=nmx[0]
M=nmx[1]
X=nmx[2]
mylist=[]
for i in range(N):
    mylist.append(list(map(int, input().split())))
Ans=10000*12+1
for i in range(2**N):
    A=[[0]*M]
    C=0
    for j in range(N):
        if (i>>j) & 1:
            C+=mylist[j][0]
            for k in range(M):
                A[k]=A[k]+mylist[j][k+1]
    if min(A)>=X:
        #Ans=min(Ans, C)
if Ans==10000*12+1:
    print(-1)
else:
    print(Ans)



Fixed submission:
nmx=list(map(int, input().split()))
N=nmx[0]
M=nmx[1]
X=nmx[2]
mylist=[]
for i in range(N):
    mylist.append(list(map(int, input().split())))
Ans=100000*12+1
for i in range(2**N):
    A=[0]*M
    C=0
    for j in range(N):
        if (i>>j) & 1:
            C=C+mylist[j][0]
            for k in range(M):
                A[k]=A[k]+mylist[j][k+1]
    if min(A)>=X:
        Ans=min(Ans, C)
if Ans==100000*12+1:
    print(-1)
else:
    print(Ans)
Buggy python submission (verdict: Time Limit Exceeded):
N = int(input())
arr = list(map(int, input().split()))
INF = 10 ** 18
ans = 1
for num in arr:
    ans *= num
 
print(ans) if ans <= INF else print(-1)

Fixed submission:
N = int(input())
arr = sorted(map(int, input().split()))
INF = 10 ** 18
ans = 1
for num in arr:
    ans *= num
    if num > 0 and ans > INF:
        ans = -1
        break
    elif ans == 0:
        break

print(ans)
Buggy python submission (verdict: Runtime Error):
from decimal import Decimal
a, b, c = map(int, input().split())
if Decimal(a) ** Decimal('0.5') + Decimal(b) ** Decimal('0.5') < Decimal(c)c ** Decimal('0.5'):
    print('Yes')
else:
    print('No')


Fixed submission:
from decimal import Decimal
a, b, c = map(int, input().split())
if Decimal(a) ** Decimal('0.5') + Decimal(b) ** Decimal('0.5') < Decimal(c) ** Decimal('0.5'):
    print('Yes')
else:
    print('No')
Buggy python submission (verdict: Time Limit Exceeded):
N, A, B = map(int, input().split())
capacity = N
is_a = True
cnt = 0

while capacity > 0:
    if is_a:
        ball = A
        cnt += min(capacity, ball)
    else:
        ball = B

    is_a = not is_a
    capacity -= min(capacity, ball)

print(cnt)


Fixed submission:
N, A, B = map(int, input().split())

combo = A + B
num_of_combo = N // combo
remaining = N - combo * num_of_combo

print(num_of_combo * A + min(remaining, A))
Buggy python submission (verdict: Time Limit Exceeded):
s = input()
roop = int(input())
is_head = True
for i in range(roop):
    q_li = input().split()
    t = int(q_li[0])
    if t == 1:
        is_head = not is_head
    else:
        f = int(q_li[1])
        c = q_li[2]
        if f == 1:
            # 先頭に追加
            s = c + s if is_head else s + c
        else:
            # 末尾に追加
            s = s + c if is_head else c + s

print(s) if is_head else print(s[::-1])


Fixed submission:
# TLE
s = input()
loop = int(input())
is_head = True
to_head = []
to_tail = []
for i in range(loop):
    q_li = input().split()
    t = int(q_li[0])
    if t == 1:
        is_head = not is_head
    else:
        f = int(q_li[1])
        c = q_li[2]
        if f == 1:
            # 先頭に追加
            to_head.append(c) if is_head else to_tail.append(c)
        else:
            # 末尾に追加
            to_tail.append(c) if is_head else to_head.append(c)

if is_head:
    print("".join(to_head[::-1]) + s + "".join(to_tail))
else:
    print("".join(to_tail[::-1]) + s[::-1] + "".join(to_head))
Buggy python submission (verdict: Time Limit Exceeded):
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)


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