Harryxun/testdata huggingface dataset:
Buggy python submission (verdict: Runtime Error):
N = int(input().strip())

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

if all([a // 2 == 1 for a in A]):
  print(0)
else:
  A = [bin(a) for a in A]
  for a in A:
    if a[-1] == '1':
      continue
    for b, i in zip(a[2:], reversed(range(len(a) - 2))):
      c += int(b) * i
  print(c)

Fixed submission:
N = int(input().strip())
 
A = list(map(int, input().strip().split()))
 
c = 0
if not all([a // 2 == 1 for a in A]):
  binA = [bin(a) for a in A]
  for a in binA:
    c += len(a) - a.rindex('1') - 1
print(c)

Buggy python submission (verdict: Runtime Error):
N = int(input().strip())

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

if all([a // 2 == 1 for a in A]):
  print(0)
else:
  A = [bin(a) for a in A]
  for a in A:
    if a[-1] == '1':
      continue
    for b, i in zip(a[2:], reversed(range(len(a) - 2))):
      c += int(b) * i
  print(c)

Fixed submission:
N = int(input().strip())
 
A = list(map(int, input().strip().split()))
 
c = 0
if not all([a // 2 == 1 for a in A]):
  binA = [bin(a) for a in A]
  for a in binA:
    c += len(a) - a.rindex('1') - 1
print(c)
Buggy python submission (verdict: Runtime Error):
n, k, q = map(int, input().split())
alist = list(map(int, input().split()))
ans = None
for i, a in enumerate(alist):
  spla = []
  preva = -1
  ailist = alist[:i] + alist[i+1:]
  for j, _a in enumerate(ailist):
    if _a < a:
      spla.append(ailist[preva+1:j])
      preva = j
  if not preva == len(ailist)-1:
    spla.append(ailist[preva+1:])
  conda = []
  for sa in spla:
    if len(sa) >= k:
      conda.extend(list(sorted(sa))[:len(sa)-k+1])
  if len(conda) < q-1:
    continue
  else:
    y = list(sorted(conda))[q-2]
    if ans is None or ans > y - a:
      ans = y - a

print(ans)


Fixed submission:
n, k, q = map(int, input().split())
alist = list(map(int, input().split()))
ans = None
if q == 1:
  print(0)
else:
  for i, a in enumerate(alist):
    spla = []
    preva = -1
    ailist = alist[:i] + alist[i+1:]
    for j, _a in enumerate(ailist):
      if _a < a:
        spla.append(ailist[preva+1:j])
        preva = j
    if not preva == len(ailist)-1:
      spla.append(ailist[preva+1:])
    conda = []
    for sa in spla:
      if len(sa) >= k:
        conda.extend(list(sorted(sa))[:len(sa)-k+1])
    if len(conda) < q-1:
      continue
    else:
      y = list(sorted(conda))[q-2]
      if ans is None or ans > y - a:
        ans = y - a

  print(ans)
Buggy python submission (verdict: Time Limit Exceeded):
s = input().strip()
k = int(input())
subs = set()
ds = {}

for i, c in enumerate(s):
  if c in ds:
    ds[c].append(i)
  else:
    ds[c] = [i]

for c, il in sorted(ds.items(), key=lambda x: x[0]):
  subs |= set([c])
  i = 2
  while il[0]+i <= len(s):
    for j in il:
      if j + i > len(s):
        break
      subs.add(s[j:j+i])
    i += 1
  if len(subs) >= k:
    break
print(list(sorted(subs))[k-1])


Fixed submission:
s = input().strip()
k = int(input())
subs = set()
for i in range(1, 6):
  for j in range(0, len(s)-i+1):
    subs |= set([s[j:j+i]])
print(list(sorted(subs))[k-1])
Buggy python submission (verdict: Runtime Error):
A, B = map(int, input().strip().split())

def is_palind(x):
  sx = str(x)
  if x[0]==x[-1] and x[1]==x[-2]:
    return True
  else:
    return False

c = 0
for a in range(A, B+1):
  if is_palind(a):
    c+=1

print(c)

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

def is_palind(x):
  sx = str(x)
  if sx[0]==sx[-1] and sx[1]==sx[-2]:
    return True
  else:
    return False

c = 0
for a in range(A, B+1):
  if is_palind(a):
    c+=1

print(c)
Buggy python submission (verdict: Time Limit Exceeded):
h, w, d = map(int, input().split())
amd = {}
for i in range(h):
  al = map(int, input().split())
  for j, a in enumerate(al):
    amd[a] = (j+1, i+1)

q = int(input())
for _ in range(q):
  ans = 0
  ansd = {}
  l, r = map(int, input().split())
  if (l, r) in ansd:
    print(ansd[(l, r)])
  else:
    prevx, prevy = amd[l]
    for x in range(l+d, r+1, d):
      _x, _y = amd[x]
      ans += abs(_x - prevx) + abs(_y - prevy)
      prevx, prevy = _x, _y
    print(ans)
    ansd[(l, r)] = ans

Fixed submission:
h, w, d = map(int, input().split())
amd = {}
for i in range(h):
  al = map(int, input().split())
  for j, a in enumerate(al):
    amd[a] = (j+1, i+1)

dsum = [0 for _ in range(d+1)]
for i in range(d+1, h*w+1):
  dsum.append(dsum[-d] + abs(amd[i][0]-amd[i-d][0]) + abs(amd[i][1]-amd[i-d][1]))

q = int(input())
for _ in range(q):
  ans = 0
  l, r = map(int, input().split())
  print(dsum[r] - dsum[l])
Buggy python submission (verdict: Runtime Error):
A = int(input().strip().split())
B = int(input().strip().split())
C = int(input().strip().split())
X = int(input().strip().split())

k = 0
for a in range(A + 1):
  for b in range(B + 1):
    for c in range(C + 1):
      if 500 * a + 100 * b + 50 * c == X:
        k += 1

print(k)


Fixed submission:
A = int(input().strip())
B = int(input().strip())
C = int(input().strip())
X = int(input().strip())

k = 0
for a in range(A + 1):
  for b in range(B + 1):
    for c in range(C + 1):
      if 500 * a + 100 * b + 50 * c == X:
        k += 1

print(k)
Buggy python submission (verdict: Runtime Error):
#!/usr/bin/python3
# -*- coding: utf-8 -*-
a, b = map(int, input().strip().split())
if a % 2 == 0 || b % 2 == 0:
    print("Even")
else:
    print("Odd")

Fixed submission:
a, b = map(int, input().strip().split())
if a % 2 == 0 or b % 2 == 0:
    print("Even")
else:
    print("Odd")
Buggy python submission (verdict: Runtime Error):
#!/usr/bin/python3
# -*- coding: utf-8 -*-
a, b = input().strip().split()
a = int(a)
for _ in range(len(b)):
    a = a*10
ab = a+int(b)
for i in range(ab):
    if i * i = ab:
        print("Yes")
    elif i * i < ab:
        continue
    else:
        print("No")
        break

Fixed submission:
#!/usr/bin/python3
# -*- coding: utf-8 -*-
a, b = input().strip().split()
a = int(a)
for _ in range(len(b)):
    a = a*10
ab = a+int(b)
for i in range(ab):
    if i * i == ab:
        print("Yes")
        break
    elif i * i < ab:
        continue
    else:
        print("No")
        break
Buggy python submission (verdict: Time Limit Exceeded):
elst = [True for _ in range(10**5+1)]
elst[0] = False

for i, e in enumerate(elst):
  if e:
    for j in range(2*i+1, 10**5+1, i+1):
      elst[j] = False

q = int(input())
for _ in range(q):
  l, r = map(int, input().split())
  l = l if l % 2 == 1 else l + 1
  r = r if r % 2 == 0 else r + 1
  c = 0
  for x in range(l, r, 2):
    if elst[x-1] and elst[(x + 1) // 2 - 1]:
      c += 1
  print(c)


Fixed submission:
elst = [True for _ in range(10**5+2)]
elst[0] = False
elst[1] = False

for i, e in enumerate(elst):
  if e:
    for j in range(2 * i, 10**5+2, i):
      elst[j] = False

c = [0 for _ in range(10**5+2)]
for i in range(1, 10**5+2, 2):
  if elst[i] and elst[(i+1)//2]:
    c[i] += 1
for i in range(2, 10**5+2):
  c[i] += c[i-1]

q = int(input())
for _ in range(q):
  l, r = map(int, input().split())
  print(c[r] - c[l-1])
Buggy python submission (verdict: Runtime Error):
N = int(input().strip())
A = list(map(int, input().strip().split()))

mindiv = -1
for a in range(A):
  if mindiv == -1:
    mindiv = bin(a) - bin(a).rstrip('0')
  else:
    tmp = bin(a) - bin(a).rstrip('0')
    if tmp < mindiv:
      mindiv = tmp

print(mindiv)

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

mindiv = -1
for a in A:
  if mindiv == -1:
    mindiv = len(bin(a)) - len(bin(a).rstrip('0'))
  else:
    tmp = len(bin(a)) - len(bin(a).rstrip('0'))
    if tmp < mindiv:
      mindiv = tmp

print(mindiv)
Buggy python submission (verdict: Runtime Error):
n = int(input())
alist = list(map(int, input().split()))
maxa, mina = max(alist), min(alist)

ans = []
if abs(maxa) >= abs(mina):
  p = alist.index(maxa) + 1
  for i in range(n):
    alist[i] += maxa
    ans.append((p, i+1))
  for i in range(n-1):
    alist[i+1] += alist[i]
    ans.append((i+1, i+2))
else:
  p = alist.index(mina) + 1
  for i in range(n):
    alist[i] += mina
    ans.append((pos, i+1))
  for i in range(n-2, -1, -1):
    alist[i] += alist[i+1]
    ans.append((i+2, i+1))

print(len(ans))
for a in ans:
  print(*a)

Fixed submission:
n = int(input())
alist = list(map(int, input().split()))
maxa, mina = max(alist), min(alist)

ans = []
if abs(maxa) >= abs(mina):
  p = alist.index(maxa) + 1
  for i in range(n):
    alist[i] += maxa
    ans.append((p, i+1))
  for i in range(n-1):
    alist[i+1] += alist[i]
    ans.append((i+1, i+2))
else:
  p = alist.index(mina) + 1
  for i in range(n):
    alist[i] += mina
    ans.append((p, i+1))
  for i in range(n-2, -1, -1):
    alist[i] += alist[i+1]
    ans.append((i+2, i+1))

print(len(ans))
for a in ans:
  print(*a)

Buggy python submission (verdict: Runtime Error):
n = int(input())
alist = list(map(int, input().split()))
maxa, mina = max(alist), min(alist)

ans = []
if abs(maxa) >= abs(mina):
  p = alist.index(maxa) + 1
  for i in range(n):
    alist[i] += maxa
    ans.append((p, i+1))
  for i in range(n-1):
    alist[i+1] += a[i]
    ans.append((i+1, i+2))
else:
  p = alist.index(mina) + 1
  for i in range(n):
    alist[i] += mina
    ans.append((pos, i+1))
  for i in range(n-2, -1, -1):
    alist[i] += a[i+1]
    ans.append((i+2, i+1))

print(len(ans))
for a in ans:
  print(*a)

Fixed submission:
n = int(input())
alist = list(map(int, input().split()))
maxa, mina = max(alist), min(alist)

ans = []
if abs(maxa) >= abs(mina):
  p = alist.index(maxa) + 1
  for i in range(n):
    alist[i] += maxa
    ans.append((p, i+1))
  for i in range(n-1):
    alist[i+1] += alist[i]
    ans.append((i+1, i+2))
else:
  p = alist.index(mina) + 1
  for i in range(n):
    alist[i] += mina
    ans.append((p, i+1))
  for i in range(n-2, -1, -1):
    alist[i] += alist[i+1]
    ans.append((i+2, i+1))

print(len(ans))
for a in ans:
  print(*a)
Buggy python submission (verdict: Runtime Error):
n = int(input())
f = []
p = []
for _ in range(n):
  f.append(list(map(int, input().strip().split())))
for _ in range(n):
  p.append(list(map(int, input().strip().split())))

maxt = None
for i in range(1, 2**10):
  total = 0
  for _f, _p in zip(f, p):
    c = 0
    for bi, fi in zip(bin(i)[2::-1], _f):
      if fi==1 and int(bi)==1:
        c += 1
    total += _p[c]
  maxt = max(total, maxt) if maxt is not None else total
print(maxt)


Fixed submission:
n = int(input())
f = []
p = []
for _ in range(n):
  f.append(list(map(int, input().strip().split())))
for _ in range(n):
  p.append(list(map(int, input().strip().split())))

maxt = None
for i in range(1, 2**10):
  total = 0
  for _f, _p in zip(f, p):
    c = 0
    for bi, fi in zip(reversed(bin(i)[2:]), _f):
      if fi==1 and int(bi)==1:
        c += 1
    total += _p[c]
  maxt = max(total, maxt) if maxt is not None else total
print(maxt)
Buggy python submission (verdict: Runtime Error):
from heapq import heapify, heappush, heappop

n, c = map(int, input().split())
stc = []
for _ in range(n):
  s, t, c = map(int, input().split())
  stc.append((s, t, c))
stc.sort()

ans = 1
recl = [(stc[0][1], stc[0][2])]
for s, t, c in stc[1:]:
  endr = heappop(recl)
  if c == endr[1]:
    heappush(recl, (t, c))
  else:
    if s <= endr[0]:
      if len(recl + 1) == ans:
        ans += 1
      heappush(recl, (t, c))
      heappush(recl, endr)
    else:
      heappush(recl, (t, c))

print(ans)


Fixed submission:
n, c = map(int, input().split())
stc = []
for _ in range(n):
  stc.append(tuple(map(int, input().split())))

sm = [0 for _ in range(2 * (pow(10, 5) + 1))]
for _c in range(c):
  tt = [0 for _ in range(2 * (pow(10, 5) + 1))]
  for s, t, c in stc:
    if c == _c + 1:
      tt[s * 2 - 1] += 1
      tt[t * 2] -= 1
  for i in range(1, len(tt)):
    tt[i] += tt[i-1]
  for i in range(len(sm)):
    if tt[i] > 0:
      sm[i] += 1

print(max(sm))
Buggy python submission (verdict: Runtime Error):
s = input().strip()

i = 0
if s[i] == 'A':
  i += 1
if s[i] == 'K':
  i += 1
  if s[i] == 'I':
    i += 1
    if s[i] == 'H':
      i += 1
      if s[i] == 'A':
        i += 1
      if s[i] == 'B':
        i += 1
        if s[i] == 'A':
          i += 1
        if s[i] == 'R':
          i += 1
          if i >= len(s):
            print('YES')
          elif s[i] == 'A' and i+1 >= len(s):
            print('YES')
          else:
            print('NO')


Fixed submission:
s = input().strip()

i = 0
isa = False
if i < len(s):
  if i < len(s) and s[i] == 'A':
    i += 1
  if i < len(s) and s[i] == 'K':
    i += 1
    if i < len(s) and s[i] == 'I':
      i += 1
      if i < len(s) and s[i] == 'H':
        i += 1
        if i < len(s) and s[i] == 'A':
          i += 1
        if i < len(s) and s[i] == 'B':
          i += 1
          if i < len(s) and s[i] == 'A':
            i += 1
          if i < len(s) and s[i] == 'R':
            i += 1
            if i >= len(s):
              print('YES')
              isa = True
            elif i < len(s) and s[i] == 'A' and i+1 >= len(s):
              print('YES')
              isa = True
            else:
              print('NO')
              isa = True
  if not isa:
    print('NO')
elif not isa:
  print('NO')
Buggy python submission (verdict: Time Limit Exceeded):
import bisect

n = int(input())
alist = list(sorted(map(int, input().split())))
blist = list(sorted(map(int, input().split())))
clist = list(sorted(map(int, input().split())))

ans = 0
for a in alist:
  bi = bisect.bisect_right(blist, a)
  if bi == len(blist):
    break
  for b in blist[bi:]:
    ci = bisect.bisect_right(clist, b)
    if ci == len(clist):
      break
    ans += len(clist) - ci
print(ans)


Fixed submission:
import bisect

n = int(input())
alist = list(sorted(map(int, input().split())))
blist = list(map(int, input().split()))
clist = list(sorted(map(int, input().split())))

ans = 0
for b in blist:
  ai = bisect.bisect_left(alist, b)
  ci = bisect.bisect_right(clist, b)
  if ai == 0 or ci == len(clist):
    continue
  else:
    ans += ai * (len(clist) - ci)
print(ans)
Buggy python submission (verdict: Time Limit Exceeded):
n = int(input())

amap = [list(map(int, input().split())) for _ in range(n)]
isnt_const = False
cost = 0
for i in range(n):
  for j in range(i+1, n):
    isnt_add = False
    for k in range(n):
      if i == k or k == j:
        continue
      if amap[i][j] > amap[i][k] + amap[k][j]:
        isnt_const = True
        break
      elif amap[i][j] == amap[i][k] + amap[k][j]:
        isnt_add = True
    if not isnt_add:
      cost += amap[i][j]

if isnt_const:
  print(-1)
else:
  print(cost)


Fixed submission:
n = int(input())

amap = [list(map(int, input().split())) for _ in range(n)]
isnt_const = False
cost = 0
for i in range(n):
  for j in range(i+1, n):
    isnt_add = False
    for k in range(n):
      if i == k or k == j:
        continue
      if amap[i][j] > amap[i][k] + amap[k][j]:
        isnt_const = True
        break
      elif amap[i][j] == amap[i][k] + amap[k][j]:
        isnt_add = True
    if not isnt_add:
      cost += amap[i][j]

if isnt_const:
  print(-1)
else:
  print(cost)
Buggy python submission (verdict: Time Limit Exceeded):
import itertools

n, m, r = map(int, input().split())
rlist = list(map(int, input().split()))
dist = [[-1 for _ in range(n)] for _ in range(n)]
for i in range(n):
  dist[i][i] = 0
abcl = []
for _ in range(m):
  a, b, c = map(int, input().split())
  dist[a-1][b-1] = c
  dist[b-1][a-1] = c

for k in range(0, n):
  for i in range(0, n):
    for j in range(0, n):
      if dist[i][k] != -1 and dist[k][j] != -1:
        if dist[i][j] == -1:
          dist[i][j] = dist[i][k] + dist[k][j]
        elif dist[i][j] > dist[i][k] + dist[k][j]:
          dist[i][j] = dist[i][k] + dist[k][j]

ans = pow(10, 15)
minp = None
for rperm in itertools.permutations(rlist):
  tmp = 0
  tmpp = [rperm[0]-1]
  for pr, nr in zip(rperm, rperm[1:]):
    tmp += dist[pr-1][nr-1]
    tmpp.append(nr-1)
  if ans > tmp:
    ans = tmp
    minp = tmpp
print(ans)


Fixed submission:
import itertools

n, m, r = map(int, input().split())
rlist = list(map(int, input().split()))
dist = [[-1 for _ in range(n)] for _ in range(n)]
for i in range(n):
  dist[i][i] = 0
abcl = []
for _ in range(m):
  a, b, c = map(int, input().split())
  dist[a-1][b-1] = c
  dist[b-1][a-1] = c

for k in range(0, n):
  for i in range(0, n):
    for j in range(0, n):
      if dist[i][k] != -1 and dist[k][j] != -1:
        if dist[i][j] == -1:
          dist[i][j] = dist[i][k] + dist[k][j]
        elif dist[i][j] > dist[i][k] + dist[k][j]:
          dist[i][j] = dist[i][k] + dist[k][j]

ans = pow(10, 15)
minp = None
for rperm in itertools.permutations(rlist):
  tmp = 0
  tmpp = [rperm[0]-1]
  for pr, nr in zip(rperm, rperm[1:]):
    tmp += dist[pr-1][nr-1]
    tmpp.append(nr-1)
  if ans > tmp:
    ans = tmp
    minp = tmpp
print(ans)
Buggy python submission (verdict: Runtime Error):
n = int(input())
ablist = [[] for _ in range(n)]
for _ in range(n-1):
  a, b, c = map(int, input().split())
  ablist[a-1].append((b-1, c))
  ablist[b-1].append((a-1, c))

q, k = map(int, input().split())
depth = [0 for _ in range(n)]

def dfs(v, p, d):
  depth[v] = d
  for to, c in ablist[v]:
    if to == p:
      continue
    dfs(to, v, d + c)

dfs(k-1, -1, 0)
for _ in range(q):
  x, y = map(int, input().split())
  print(depth[x-1] + depth[y-1])


Fixed submission:
import sys
sys.setrecursionlimit(1000000)

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

q, k = map(int, input().split())
depth = [-1 for _ in range(n)]

def dfs(v, p, d):
  depth[v] = d
  for to, c in ablist[v]:
    if to == p:
      continue
    dfs(to, v, d + c)

dfs(k-1, -1, 0)
for _ in range(q):
  x, y = map(int, input().split())
  print(depth[x-1] + depth[y-1])
Buggy python submission (verdict: Runtime Error):
import sys
sys.setrecursionlimit(1000000)

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

def gcd(a, b):
  if b == 0:
    return a
  else:
    return gcd(b, a % b)

if n == 1:
  if k == a[0]:
    print('POSSIBLE')
  else:
    print('IMPOSSIBLE')
else:
  r = gcd(alist[1], alist[0])
  for _a in alist[2:]:
    r = gcd(_a, r)
  if k <= alist[-1] and k % r == 0:
    print('POSSIBLE')
  else:
    print('IMPOSSIBLE')


Fixed submission:
n, k = map(int, input().split())
alist = list(sorted(map(int, input().split())))

def gcd(a, b):
  if b == 0:
    return a
  else:
    return gcd(b, a % b)

if n == 1:
  if k == alist[0]:
    print('POSSIBLE')
  else:
    print('IMPOSSIBLE')
else:
  r = gcd(alist[1], alist[0])
  for _a in alist[2:]:
    r = gcd(_a, r)
  if k <= alist[-1] and k % r == 0:
    print('POSSIBLE')
  else:
    print('IMPOSSIBLE')

Buggy python submission (verdict: Runtime Error):
n, k = map(int, input().split())
alist = list(sorted(map(int, input().split())))

def gcd(a, b):
  if b == 0:
    return a
  else:
    return gcd(b, a % b)

if n == 1:
  if k == a[0]:
    print('POSSIBLE')
  else:
    print('IMPOSSIBLE')
else:
  r = gcd(alist[1], alist[0])
  for _a in alist[2:]:
    r = gcd(_a, r)
  if k <= alist[-1] and k % r == 0:
    print('POSSIBLE')
  else:
    print('IMPOSSIBLE')


Fixed submission:
n, k = map(int, input().split())
alist = list(sorted(map(int, input().split())))

def gcd(a, b):
  if b == 0:
    return a
  else:
    return gcd(b, a % b)

if n == 1:
  if k == alist[0]:
    print('POSSIBLE')
  else:
    print('IMPOSSIBLE')
else:
  r = gcd(alist[1], alist[0])
  for _a in alist[2:]:
    r = gcd(_a, r)
  if k <= alist[-1] and k % r == 0:
    print('POSSIBLE')
  else:
    print('IMPOSSIBLE')
Buggy python submission (verdict: Time Limit Exceeded):
import heapq
import copy

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

ante, post = alist[:n], alist[-n:]
startante = sum(ante)
startpost = sum(post)
post = list(map(lambda x: -x, post))
heapq.heapify(ante)
heapq.heapify(post)

ans = None
for k in range(n+1):
  _ante = copy.deepcopy(ante)
  _post = copy.deepcopy(post)
  antesum = startante
  postsum = startpost
  for a in alist[n: n+k]:
    minante = heapq.heappushpop(_ante, a)
    antesum = antesum + a - minante
  for a in alist[n+k: 2*n]:
    maxpost = heapq.heappushpop(_post, -a)
    postsum = postsum + a + maxpost
  if ans is None:
    ans = antesum - postsum
  elif ans < antesum - postsum:
    ans = antesum - postsum
print(ans)


Fixed submission:
import heapq

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

ante, post = alist[:n], alist[-n:]
antesums = [0 for _ in range(n+1)]
postsums = [0 for _ in range(n+1)]
antesums[0] = sum(ante)
postsums[0] = sum(post)
post = list(map(lambda x: -x, post))
heapq.heapify(ante)
heapq.heapify(post)

for k in range(n):
  minante = heapq.heappushpop(ante, alist[n+k])
  antesums[k+1] = antesums[k] + alist[n+k] - minante
  maxpost = heapq.heappushpop(post, -alist[2*n-k-1])
  postsums[k+1] = postsums[k] + alist[2*n-k-1] + maxpost

ans = -sum(alist)
for k in range(n+1):
  if ans < antesums[k] - postsums[-k-1]:
    ans = antesums[k] - postsums[-k-1]
print(ans)

Buggy python submission (verdict: Time Limit Exceeded):
import heapq
import copy

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

ante, post = alist[:n], alist[-n:]
startante = sum(ante)
startpost = sum(post)
post = list(map(lambda x: -x, post))
heapq.heapify(ante)
heapq.heapify(post)

ans = None
for k in range(n+1):
  _ante = copy.deepcopy(ante)
  _post = copy.deepcopy(post)
  antesum = startante
  postsum = startpost
  for a in alist[n: n+k]:
    minante = heapq.heappushpop(_ante, a)
    antesum = antesum + a - minante
  for a in alist[n+k: 2*n]:
    maxpost = heapq.heappushpop(_post, -a)
    postsum = postsum + a + maxpost
  if ans is None:
    ans = antesum - postsum
  elif ans < antesum - postsum:
    ans = antesum - postsum
print(ans)


Fixed submission:
import heapq

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

ante, post = alist[:n], alist[-n:]
antesums = [0 for _ in range(n+1)]
postsums = [0 for _ in range(n+1)]
antesums[0] = sum(ante)
postsums[0] = sum(post)
post = list(map(lambda x: -x, post))
heapq.heapify(ante)
heapq.heapify(post)

for k in range(n):
  minante = heapq.heappushpop(ante, alist[n+k])
  antesums[k+1] = antesums[k] + alist[n+k] - minante
  maxpost = heapq.heappushpop(post, -alist[2*n-k-1])
  postsums[k+1] = postsums[k] + alist[2*n-k-1] + maxpost

ans = -sum(alist)
for k in range(n+1):
  if ans < antesums[k] - postsums[-k-1]:
    ans = antesums[k] - postsums[-k-1]
print(ans)
Buggy python submission (verdict: Runtime Error):
N, K = map(int, input().strip().split())
ab = []
for _ in range(N):
  ab.append(tuple(map(int, input().strip())))

kc = 0
for a, b in sorted(ab, key=lambda x: x[0]):
  if kc + b >= K:
    break
  kc += b

print(a)


Fixed submission:
N, K = map(int, input().strip().split())
ab = []
for _ in range(N):
  ab.append(tuple(map(int, input().strip().split())))

kc = 0
for a, b in sorted(ab, key=lambda x: x[0]):
  if kc + b >= K:
    break
  kc += b

print(a)