Harryxun/testdata huggingface dataset:
Buggy python submission (verdict: Time Limit Exceeded):
#!/usr/bin/env python
# -*- coding: utf-8 -*-
import heapq


def solve():
  N, A, B, C, D = map(int, input().split())
  min_cost = 1e+18
  min_value = N + 1
  queue = []
  heapq.heappush(queue, (0, N))

  while True:
    if len(queue) == 0:
      print(min_cost)
      return

    c, n = heapq.heappop(queue)

    if n > min_value * 2:
      continue

    if n == 0:
      print(c)
      return

    min_value = n

    if n < 1e+9:
      min_cost = min(c + D * n, min_cost)

    if n < 2:
      continue

    m2 = n % 2

    if m2 == 0:
      heapq.heappush(queue, (c + A, n // 2))
    else:
      heapq.heappush(queue, (c + A + D, (n - 1) // 2))

    if n < 3:
      continue

    m3 = n % 3

    if m3 == 0:
      heapq.heappush(queue, (c + B, n // 3))
    elif m3 == 1:
      heapq.heappush(queue, (c + B + D, (n - 1) // 3))
    else:
      heapq.heappush(queue, (c + B + D, (n + 1) // 3))

    if n < 5:
      continue

    m5 = n % 5

    if m5 == 0:
      heapq.heappush(queue, (c + C, n // 5))
    elif m5 == 1:
      heapq.heappush(queue, (c + C + D, (n - 1) // 5))
    elif m5 == 2:
      heapq.heappush(queue, (c + C + D + D, (n - 2) // 5))
    elif m5 == 3:
      heapq.heappush(queue, (c + C + D + D, (n + 2) // 5))
    else:
      heapq.heappush(queue, (c + C + D, (n + 1) // 5))


def main():
  for _ in range(int(input())):
    solve()


if __name__ == '__main__':
  main()


Fixed submission:
#!/usr/bin/env python
# -*- coding: utf-8 -*-
import heapq


def solve():
  N, A, B, C, D = map(int, input().split())
  seen_values = set()
  queue = []
  heapq.heappush(queue, (0, N))

  while True:
    cost, value = heapq.heappop(queue)

    if value in seen_values:
      continue

    seen_values.add(value)

    if value == 1:
      print(cost + D)
      return

    for k, c in ((2, A), (3, B), (5, C)):
      v, m = divmod(value, k)

      if v == 0:
        continue
      elif m == 0:
        heapq.heappush(queue, (cost + c, v))
      else:
        heapq.heappush(queue, (cost + c + D * m, v))
        heapq.heappush(queue, (cost + c + D * (k - m), v + 1))

      heapq.heappush(queue, (cost + D * (value - v), v))


def main():
  for _ in range(int(input())):
    solve()


if __name__ == '__main__':
  main()
Buggy python submission (verdict: Time Limit Exceeded):
#!/usr/bin/env python
# -*- coding: utf-8 -*-


def main():
  _, m = map(int, input().split())
  traps = [v == '1' for v in input().strip()]
  values = [None] * len(traps)
  values[0] = -1

  for i in range(len(values) - 1):
    if values[i] is None:
      continue

    for j in range(i + m, i, -1):
      if j >= len(values) or traps[j]:
        continue

      if values[j] is not None:
        break

      values[j] = i

  if values[-1] is None:
    print(-1)
    return

  index = len(values) - 1
  route = []

  while values[index] != -1:
    route.append(index - values[index])
    index = values[index]

  print(' '.join(str(v) for v in route[::-1]))


if __name__ == '__main__':
  main()


Fixed submission:
#!/usr/bin/env python
# -*- coding: utf-8 -*-


def main():
  _, m = map(int, input().split())
  traps = [v == '1' for v in input().strip()]
  values = [None] * len(traps)
  values[0] = -1

  for i in range(len(values) - 1):
    if values[i] is None:
      continue

    for j in range(min(i + m, len(values) - 1), i, -1):
      if j >= len(values) or traps[j]:
        continue

      if values[j] is not None:
        break

      values[j] = i

  if values[-1] is None:
    print(-1)
    return

  index = len(values) - 1
  route = []

  while values[index] != -1:
    route.append(index - values[index])
    index = values[index]

  print(' '.join(str(v) for v in route[::-1]))


if __name__ == '__main__':
  main()
Buggy python submission (verdict: Runtime Error):
def flood(s, v, idx, val):
  v[idx] = max(val, v[idx])
  
  if idx < len(v) - 1 and s[idx + 1] == '<':
    flood(s, v, idx + 1, val + 1)
  if idx > 0 and s[idx] == '>':
    flood(s, v, idx - 1, val + 1)

def main():
  s = '>' + input().strip() + '<'
  v = [-1] * (len(s) - 1)
  
  for i, (b, a) in enumerate(zip(s[:-1], s[1:])):
    if b == '>' and a == '<':
      flood(s, v, i, 0)

  #print(v)
  print(sum(v))
  
if __name__ == '__main__':
  main()


Fixed submission:
def do_left(s, v, idx):
  while idx > 0 and s[idx] == '>':
    v[idx - 1] = max(v[idx] + 1, v[idx - 1])
    idx -= 1

def do_right(s, v, idx):
  while idx < len(v) - 1 and s[idx + 1] == '<':
    v[idx + 1] = max(v[idx] + 1, v[idx + 1])
    idx += 1
    
def main():
  s = '>' + input().strip() + '<'
  v = [-1] * (len(s) - 1)
  
  for i, (b, a) in enumerate(zip(s[:-1], s[1:])):
    if b == '>' and a == '<':
      v[i] = 0
      do_left(s, v, i)
      do_right(s, v, i)

  #print(v)
  print(sum(v))
  
if __name__ == '__main__':
  main()
Buggy python submission (verdict: Time Limit Exceeded):
#!/usr/bin/env python
# -*- coding: utf-8 -*-


def main():
  input()
  values = list(map(int, input().split()))
  values.sort()
  result = 0

  for i in range(len(values) - 2):
    for j in range(i + 1, len(values) - 1):
      for k in range(j + 1, len(values)):
        if (values[i] < values[j] + values[k]
            and values[j] < values[i] + values[k]
            and values[k] < values[i] + values[j]):
          result += 1

  print(result)


if __name__ == '__main__':
  main()


Fixed submission:
#!/usr/bin/env python
# -*- coding: utf-8 -*-
import bisect


def main():
  input()
  values = list(map(int, input().split()))
  values.sort()
  result = 0

  for i in range(len(values) - 2):
    for j in range(i + 1, len(values) - 1):
      targets = values[j + 1:]
      index = bisect.bisect_left(targets, values[i] + values[j])
      result += index

  print(result)


if __name__ == '__main__':
  main()
Buggy python submission (verdict: Runtime Error):
n, k, q = map(int, input().split())
v = [0] * n

for _ in range(q):
  v[int(input())+1] += 1

for x in v:
  if x + k - q > 0:
    print('Yes')
  else:
    print('No')


Fixed submission:
n, k, q = map(int, input().split())
v = [0] * n

for _ in range(q):
  v[int(input())-1] += 1

for x in v:
  if x + k - q > 0:
    print('Yes')
  else:
    print('No')
Buggy python submission (verdict: Time Limit Exceeded):
import bisect


def main():
  x = [int(input()) for _ in range(int(input()))]
  c = [x[-1]]

  for i in range(len(x) - 2, -1, -1):
    j = bisect.bisect(c, x[i])

    if j == len(c):
      c.append(x[i])
    else:
      c = c[:j] + c[j + 1:]
      bisect.insort(c, x[i])

  print(len(c))


if __name__ == '__main__':
  main()


Fixed submission:
import bisect


def main():
  x = [int(input()) for _ in range(int(input()))]
  c = [x[-1]]

  for i in range(len(x) - 2, -1, -1):
    j = bisect.bisect(c, x[i])

    if j == len(c):
      c.append(x[i])
    else:
      c[j] = x[i]

  print(len(c))


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


def main():
  n = int(input())
  p = [tuple(map(int, input().split())) for _ in range(n)]
  c = {}

  if n == 1:
    print(1)
    return

  raise NotImplementedError()
  for i, j in itertools.combinations(range(len(p)), 2):
    d = (p[j][0] - p[i][0], p[j][1] - p[i][1])

    if d not in c:
      c[d] = 1
    else:
      c[d] += 1

  print(len(p) - max(c.values()))


if __name__ == '__main__':
  main()


Fixed submission:
import itertools


def main():
  n = int(input())
  p = [tuple(map(int, input().split())) for _ in range(n)]
  c = {}

  if n == 1:
    print(1)
    return

  for i, j in itertools.product(range(len(p)), repeat=2):
    if i == j:
      continue

    d = (p[j][0] - p[i][0], p[j][1] - p[i][1])

    if d not in c:
      c[d] = 1
    else:
      c[d] += 1

  print(len(p) - max(c.values()))


if __name__ == '__main__':
  main()

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

n = int(input())
p = [tuple(map(int, input().split())) for _ in range(n)]
c = {}

for i, j in itertools.combinations(range(len(p)), 2):
  d = (p[j][0] - p[i][0], p[j][1] - p[i][1])
  
  if d not in c:
    c[d] = 1
  else:
    c[d] += 1  

print(len(p) - max(c.values()))


Fixed submission:
import itertools


def main():
  n = int(input())
  p = [tuple(map(int, input().split())) for _ in range(n)]
  c = {}

  if n == 1:
    print(1)
    return

  for i, j in itertools.product(range(len(p)), repeat=2):
    if i == j:
      continue

    d = (p[j][0] - p[i][0], p[j][1] - p[i][1])

    if d not in c:
      c[d] = 1
    else:
      c[d] += 1

  print(len(p) - max(c.values()))


if __name__ == '__main__':
  main()
Buggy python submission (verdict: Time Limit Exceeded):
def exchange(n, s, d):
  v = [n] * (n + 1)
  s = [1] + s
  d = [1] + d

  for i in range(len(v) - 1):
    for a, b in zip(s, d):
      if i + a < len(v):
        v[i + a] = max(v[i] - a + b, v[i + a])

  return v[-1]


def main():
  n = int(input())
  a = list(map(int, input().split()))
  b = list(map(int, input().split()))

  n = exchange(n, a, b)
  n = exchange(n, b, a)

  print(n)


if __name__ == '__main__':
  main()


Fixed submission:
def exchange(n, s, d):
  v = [n] * (n + 1)
  r = [(1, 1)] + [(a, b) for a, b in zip(s, d) if a < b]

  for i in range(len(v) - 1):
    for a, b in r:
      if i + a < len(v):
        v[i + a] = max(v[i] - a + b, v[i + a])

  return v[-1]


def main():
  n = int(input())
  a = list(map(int, input().split()))
  b = list(map(int, input().split()))

  n = exchange(n, a, b)
  n = exchange(n, b, a)

  print(n)


if __name__ == '__main__':
  main()

Buggy python submission (verdict: Time Limit Exceeded):
def exchange(n, s, d):
  v = n

  for i in range(n // s[0] + 1):
    x0 = n - s[0] * i
    y0 = d[0] * i

    for j in range(x0 // s[1] + 1):
      x1 = x0 - s[1] * j
      y1 = y0 + d[1] * j

      k = x1 // s[2]
      x1 -= s[2] * k
      y1 += d[2] * k

      v = max(x1 + y1, v)

  return v


def main():
  n = int(input())
  a = list(map(int, input().split()))
  b = list(map(int, input().split()))

  n = exchange(n, a, b)
  n = exchange(n, b, a)

  print(n)


if __name__ == '__main__':
  main()


Fixed submission:
def exchange(n, s, d):
  v = [n] * (n + 1)
  r = [(1, 1)] + [(a, b) for a, b in zip(s, d) if a < b]

  for i in range(len(v) - 1):
    for a, b in r:
      if i + a < len(v):
        v[i + a] = max(v[i] - a + b, v[i + a])

  return v[-1]


def main():
  n = int(input())
  a = list(map(int, input().split()))
  b = list(map(int, input().split()))

  n = exchange(n, a, b)
  n = exchange(n, b, a)

  print(n)


if __name__ == '__main__':
  main()

Buggy python submission (verdict: Time Limit Exceeded):
def exchange(n, s, d):
  v = n

  for i in range(n // s[0] + 1):
    x0 = n - s[0] * i
    y0 = d[0] * i

    for j in range(x0 // s[1] + 1):
      x1 = x0 - s[1] * j
      y1 = y0 + d[1] * j

      k = x1 // s[2]
      x1 -= s[2] * k
      y1 += d[2] * k

      v = max(x1 + y1, v)

  return v


def main():
  n = int(input())
  a = list(map(int, input().split()))
  b = list(map(int, input().split()))

  n = exchange(n, a, b)
  n = exchange(n, b, a)

  print(n)


if __name__ == '__main__':
  main()


Fixed submission:
def exchange(n, s, d):
  v = [n] * (n + 1)
  r = [(1, 1)] + [(a, b) for a, b in zip(s, d) if a < b]

  for i in range(len(v) - 1):
    for a, b in r:
      if i + a < len(v):
        v[i + a] = max(v[i] - a + b, v[i + a])

  return v[-1]


def main():
  n = int(input())
  a = list(map(int, input().split()))
  b = list(map(int, input().split()))

  n = exchange(n, a, b)
  n = exchange(n, b, a)

  print(n)


if __name__ == '__main__':
  main()
Buggy python submission (verdict: Time Limit Exceeded):
#!/usr/bin/env python
# -*- coding: utf-8 -*-

MOD = 1000003


class ModInt(object):

  def __init__(self, num, mod):
    self.num = num
    self.mod = mod

    if self.num >= self.mod:
      self.num %= self.mod

  def inv(self):
    a, b, u, v = self.mod, self.num, 0, 1

    while b != 0:
      t = a // b
      a -= t * b
      u -= t * v
      a, b = b, a
      u, v = v, u

    return ModInt(u % self.mod, self.mod)

  def __add__(self, n):
    raise NotImplementedError()

  def __sub__(self, n):
    raise NotImplementedError()

  def __mul__(self, n):
    return ModInt((self.num * int(n)) % self.mod, self.mod)

  def __div__(self, n):
    raise NotImplementedError()

  def __pow__(self, n):
    v = 1
    m = self.num

    while n != 0:
      if n & 1 != 0:
        v = (v * m) % self.mod

      m = (m * m) % self.mod
      n = n >> 1

    return ModInt(v, self.mod)

  def __int__(self):
    return self.num

  def __str__(self):
    return str(self.num)


def main():
  facs = [None] * MOD
  facs[0] = ModInt(1, MOD)

  for i in range(1, MOD):
    facs[i] = facs[i - 1] * i

  for _ in range(int(input())):
    x, d, n = map(int, input().split())

    if d == 0:
      print(ModInt(x, MOD) ** n)
      continue

    x = int(ModInt(x, MOD) * ModInt(d, MOD).inv())

    if x + n - 1 >= MOD:
      print(0)
      continue

    v = facs[x + n - 1]

    if x != 0:
      v *= facs[x - 1].inv()

    if d != 1:
      v *= (ModInt(d, MOD) ** n)

    print(v)


if __name__ == '__main__':
  main()



Fixed submission:
#!/usr/bin/env python
# -*- coding: utf-8 -*-

MOD = 1000003


def inv(x, m):
  y, u, v = m, 1, 0

  while y != 0:
    t = x // y
    x -= t * y
    u -= t * v
    x, y = y, x
    u, v = v, u

  return u % m


def main():
  facs = [None] * MOD
  invs = [None] * MOD

  facs[0] = 1
  for i in range(1, MOD):
    facs[i] = facs[i - 1] * i % MOD

  invs[-1] = facs[-1]
  for i in range(MOD - 1, 0, -1):
    invs[i - 1] = invs[i] * i % MOD

  for _ in range(int(input())):
    x, d, n = map(int, input().split())

    if d == 0:
      print(pow(x, n, MOD))
      continue

    x = x * inv(d, MOD) % MOD

    if x + n - 1 >= MOD:
      print(0)
      continue

    v = facs[x + n - 1]

    if x != 0:
      v = v * invs[x - 1] % MOD

    if d != 1:
      v = v * pow(d, n, MOD) % MOD

    print(v)


if __name__ == '__main__':
  main()
Buggy python submission (verdict: Time Limit Exceeded):
#!/usr/bin/env python
# -*- coding: utf-8 -*-


def calc(a, b):
  if a == 1 or b == 1:
    return 1

  while a != 0 and b != 0:
    if a < b:
      b %= a
    else:
      a %= b

  return a if a != 0 else b


def main():
  input()
  a = list(map(int, input().split()))
  p = [a[0], a[1], calc(a[0], a[1])]

  for v in a[2:]:
    for j in range(len(p) - 1):
      p[j] = calc(p[j], v)

    p.append(calc(p[-1], v))

  print(max(p), flush=True)


if __name__ == '__main__':
  main()



Fixed submission:
#!/usr/bin/env python
# -*- coding: utf-8 -*-


def cd(a, b):
  if a == 1 or b == 1:
    return 1

  while a != 0 and b != 0:
    if a < b:
      b %= a
    else:
      a %= b

  return a if a != 0 else b


def main():
  input()
  prevs = list(map(int, input().split()))
  posts = prevs[::-1]

  for i in range(1, len(prevs)):
    prevs[i] = cd(prevs[i - 1], prevs[i])
    posts[i] = cd(posts[i - 1], posts[i])

  posts = posts[::-1]

  gcd = max(prevs[-2], posts[1])

  for i in range(1, len(prevs) - 1):
    gcd = max(cd(prevs[i - 1], posts[i + 1]), gcd)

  print(gcd, flush=True)


if __name__ == '__main__':
  main()

Buggy python submission (verdict: Time Limit Exceeded):
#!/usr/bin/env python
# -*- coding: utf-8 -*-


def calc(a, b):
  while a != 0 and b != 0:
    if a < b:
      b -= a
    else:
      a -= b

  return a if a != 0 else b


def main():
  input()
  a = list(map(int, input().split()))
  p = [a[0], a[1], calc(a[0], a[1])]

  for v in a[2:]:
    for j in range(len(p) - 1):
      p[j] = calc(p[j], v)

    p.append(calc(p[-1], v))

  print(max(p), flush=True)


if __name__ == '__main__':
  main()



Fixed submission:
#!/usr/bin/env python
# -*- coding: utf-8 -*-


def cd(a, b):
  if a == 1 or b == 1:
    return 1

  while a != 0 and b != 0:
    if a < b:
      b %= a
    else:
      a %= b

  return a if a != 0 else b


def main():
  input()
  prevs = list(map(int, input().split()))
  posts = prevs[::-1]

  for i in range(1, len(prevs)):
    prevs[i] = cd(prevs[i - 1], prevs[i])
    posts[i] = cd(posts[i - 1], posts[i])

  posts = posts[::-1]

  gcd = max(prevs[-2], posts[1])

  for i in range(1, len(prevs) - 1):
    gcd = max(cd(prevs[i - 1], posts[i + 1]), gcd)

  print(gcd, flush=True)


if __name__ == '__main__':
  main()
Buggy python submission (verdict: Runtime Error):
#!/usr/bin/env python
# -*- coding: utf-8 -*-


def main():
  n = int(input())
  prev = -1
  nums = []
  table = {}

  for _ in range(n):
    v = int(input())

    if v != prev:
      nums.append(v)
      prev = v

  for i, n in enumerate(nums):
    if n not in table:
      table[n] = []

    table[n].append(i)

  values = [0] * len(nums)
  value = 1

  for i in range(len(nums)):
    values[i] += value

    for j in table[nums[i]]:
      if j <= i:
        continue

      values[j] += value

    value = values[i]

  print(value % int(1e+9 + 7), flush=True)


if __name__ == '__main__':
  main()



Fixed submission:
#!/usr/bin/env python
# -*- coding: utf-8 -*-


def main():
  input()
  string = input().strip()

  count = [0] * 26

  for c in string:
    count[ord(c) - 97] += 1

  table = count[:]
  value = sum(count)

  for _ in range(len(count) - 1):
    for i in range(len(count)):
      table[i] = count[i] * sum(table[i + 1:])

    value = (value + sum(table)) % int(1.0e+9 + 7)

  print(value, flush=True)


if __name__ == '__main__':
  main()

Buggy python submission (verdict: Runtime Error):
#!/usr/bin/env python
# -*- coding: utf-8 -*-


def main():
  n = int(input())
  prev = -1
  nums = []
  table = {}

  for _ in range(n):
    v = int(input())

    if v != prev:
      nums.append(v)
      prev = v

  for i, n in enumerate(nums):
    if n not in table:
      table[n] = []

    table[n].append(i)

  values = [0] * len(nums)
  value = 1

  for i in range(len(nums)):
    values[i] += value

    for j in table[nums[i]]:
      if j <= i:
        continue

      values[j] += value

    value = values[i] % int(1e+9 + 7)

  print(value, flush=True)


if __name__ == '__main__':
  main()



Fixed submission:
#!/usr/bin/env python
# -*- coding: utf-8 -*-


def main():
  input()
  string = input().strip()

  count = [0] * 26

  for c in string:
    count[ord(c) - 97] += 1

  table = count[:]
  value = sum(count)

  for _ in range(len(count) - 1):
    for i in range(len(count)):
      table[i] = count[i] * sum(table[i + 1:])

    value = (value + sum(table)) % int(1.0e+9 + 7)

  print(value, flush=True)


if __name__ == '__main__':
  main()
Buggy python submission (verdict: Time Limit Exceeded):
#!/usr/bin/env python
# -*- coding: utf-8 -*-


def main():
  n = int(input())
  prev = -1
  nums = []
  table = {}

  for _ in range(n):
    v = int(input())

    if v != prev:
      nums.append(v)
      prev = v

  for i, n in enumerate(nums):
    if n not in table:
      table[n] = []

    table[n].append(i)

  values = [0] * len(nums)
  value = 1

  for i in range(len(nums)):
    values[i] += value

    for j in table[nums[i]]:
      if j <= i:
        continue

      values[j] += value

    value = values[i]

  print(value % int(1e+9 + 7), flush=True)


if __name__ == '__main__':
  main()



Fixed submission:
#!/usr/bin/env python
# -*- coding: utf-8 -*-


def main():
  value = 1
  skips = {}
  prev = -1

  for _ in range(int(input())):
    n = int(input())

    if n == prev:
      continue

    prev = n

    if n in skips:
      value += skips[n]

    skips[n] = value

  print(value % int(1e+9 + 7), flush=True)


if __name__ == '__main__':
  main()

Buggy python submission (verdict: Time Limit Exceeded):
#!/usr/bin/env python
# -*- coding: utf-8 -*-


def main():
  n = int(input())
  prev = -1
  nums = []
  table = {}

  for _ in range(n):
    v = int(input())

    if v != prev:
      nums.append(v)
      prev = v

  for i, n in enumerate(nums):
    if n not in table:
      table[n] = []

    table[n].append(i)

  values = [0] * len(nums)
  value = 1

  for i in range(len(nums)):
    values[i] += value

    for j in table[nums[i]]:
      if i == j:
        continue

      values[j] = (values[j] + value) % int(1e+9 + 7)

    value = values[i] % int(1e+9 + 7)

  print(value, flush=True)


if __name__ == '__main__':
  main()



Fixed submission:
#!/usr/bin/env python
# -*- coding: utf-8 -*-


def main():
  value = 1
  skips = {}
  prev = -1

  for _ in range(int(input())):
    n = int(input())

    if n == prev:
      continue

    prev = n

    if n in skips:
      value += skips[n]

    skips[n] = value

  print(value % int(1e+9 + 7), flush=True)


if __name__ == '__main__':
  main()

Buggy python submission (verdict: Time Limit Exceeded):
#!/usr/bin/env python
# -*- coding: utf-8 -*-


def main():
  n = int(input())
  nums = list(map(int, [input() for _ in range(n)]))
  values = [0] * len(nums)
  value = 1

  for i in range(len(nums)):
    values[i] += value

    for j in range(i + 1, len(nums)):
      if nums[i] != nums[j] or nums[j - 1] == nums[j]:
        continue

      values[j] += value

    value = values[i]

  print(value, flush=True)


if __name__ == '__main__':
  main()



Fixed submission:
#!/usr/bin/env python
# -*- coding: utf-8 -*-


def main():
  value = 1
  skips = {}
  prev = -1

  for _ in range(int(input())):
    n = int(input())

    if n == prev:
      continue

    prev = n

    if n in skips:
      value += skips[n]

    skips[n] = value

  print(value % int(1e+9 + 7), flush=True)


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

stdin = sys.stdin

ni = lambda: int(ns())
na = lambda: list(map(int, stdin.readline().split()))
ns = lambda: stdin.readline().rstrip()  # ignore trailing spaces

n = ni()
a = na()
q = ni()
c = [na() for _ in range(q)]

for i in c:
  a = [i[1] if i[0] == l else l for l in a]
  print(sum(a))

Fixed submission:
import sys

stdin = sys.stdin

ni = lambda: int(ns())
na = lambda: list(map(int, stdin.readline().split()))
ns = lambda: stdin.readline().rstrip()  # ignore trailing spaces

n = ni()
a = na()
q = ni()
l = [na() for _ in range(q)]

import collections

c = collections.Counter(a)
ans = sum(a)
for i in l:
  if c[i[0]] >0:
    ans = ans - c[i[0]]*i[0] + i[1]*(c[i[0]])
    c[i[1]] = c[i[1]] + c[i[0]]
    c[i[0]] = 0
    print(ans)
  else:
    print(ans)
Buggy python submission (verdict: Runtime Error):
import sys
from collections import Counter

stdin = sys.stdin

ni = lambda: int(ns())
na = lambda: list(map(int, stdin.readline().split()))
ns = lambda: stdin.readline().rstrip()  # ignore trailing spaces

n = ni()
a = na()
a.sort()
cnt = Counter(a)
elem = [True]*(a[-1]+1)

for i in a:
  j = i*2
  while j <= a[-1]+1:
    elem[j] = False
    j += i
ans = 0
for v in a:
  if elem[v] == True and cnt[v] == 1:
    ans += 1
    
print(ans)

Fixed submission:
import sys
from collections import Counter

stdin = sys.stdin

ni = lambda: int(ns())
na = lambda: list(map(int, stdin.readline().split()))
ns = lambda: stdin.readline().rstrip()  # ignore trailing spaces

n = ni()
a = na()
a.sort()
cnt = Counter(a)
elem = [True]*(a[-1]+1)

for i in a:
  for j in range(i*2,a[-1]+1,i):
    elem[j] = False
ans = 0
for v in a:
  if elem[v] == True and cnt[v] == 1:
    ans += 1
    
print(ans)

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

stdin = sys.stdin

ni = lambda: int(ns())
na = lambda: list(map(int, stdin.readline().split()))
ns = lambda: stdin.readline().rstrip()  # ignore trailing spaces

n = ni()
a = na()
a.sort()
cnt = collections.Counter(a)


elem = [True]*(a[-1]+1)
for i in a:
  for j in range(i*2,len(elem)+1,i):
    elem[j] = False
ans = 0
for v in a:
  if elem[v] == True and cnt[v] == 1:
    ans += 1
    
print(ans)

Fixed submission:
import sys
from collections import Counter

stdin = sys.stdin

ni = lambda: int(ns())
na = lambda: list(map(int, stdin.readline().split()))
ns = lambda: stdin.readline().rstrip()  # ignore trailing spaces

n = ni()
a = na()
a.sort()
cnt = Counter(a)
elem = [True]*(a[-1]+1)

for i in a:
  for j in range(i*2,a[-1]+1,i):
    elem[j] = False
ans = 0
for v in a:
  if elem[v] == True and cnt[v] == 1:
    ans += 1
    
print(ans)