Harryxun/testdata huggingface dataset:
Buggy python submission (verdict: Time Limit Exceeded):
C, n = map(int, input().split())
mp = [input() for _ in range(n)]
diff_dic = set()
def change(x, y):
  global diff_dic
  if (x, y) in diff_dic:
    diff_dic = diff_dic - {(x, y)}
  else:
    diff_dic = diff_dic | {(x, y)}

for y in range(n // 2):
  for x in range(n // 2):
    base = mp[y][x]
    if mp[y][n - 1 - x] != base:
      change(n - 1 - x, y)
    if mp[n - 1 - y][x] != base:
      change(x, n - 1 - y)
    if mp[n - 1 - y][n - 1 - x] != base:
      change(n - 1 - x, n - 1 - y)
if n % 2:
  for x in range(n // 2):
    if mp[n // 2][x] != mp[n // 2][n - 1 - x]:
      chenge(n - 1 - x, n // 2)
  for y in range(n // 2):
    if mp[y][n // 2] != mp[n - 1 - y][n // 2]:
      change(n // 2, n - 1 - y)

ans = 0
if not diff_dic:
  ans += 1

for _ in range(C - 1):
  d = int(input())
  for _ in range(d):
    r, c = map(int, input().split())
    r -= 1
    c -= 1
    if r < n // 2 and c < n // 2:
      change(c, n - 1 - r)
      change(n - 1 - c, r)
      change(n - 1 - c, n - 1 - r)
    elif n % 2 and r == n // 2 and c != n // 2:
      change(max(c, n - 1 - c), r)
    elif n % 2 and r != n // 2 and c == n // 2:
      change(c, max(r, n - 1 - r))
    else:
      change(c, r)
  if not diff_dic:
      ans += 1
print(ans)



Fixed submission:
def main():
  C, n = map(int, input().split())
  mp = [input() for _ in range(n)]
  diff_dic = {}

  def change(x, y):
    if (x, y) in diff_dic:
      diff_dic.pop((x, y))
    else:
      diff_dic[(x, y)] = True

  for y in range(n // 2):
    for x in range(n // 2):
      base = mp[y][x]
      if mp[y][n - 1 - x] != base:
        change(n - 1 - x, y)
      if mp[n - 1 - y][x] != base:
        change(x, n - 1 - y)
      if mp[n - 1 - y][n - 1 - x] != base:
        change(n - 1 - x, n - 1 - y)
  if n % 2:
    for x in range(n // 2):
      if mp[n // 2][x] != mp[n // 2][n - 1 - x]:
        chenge(n - 1 - x, n // 2)
    for y in range(n // 2):
      if mp[y][n // 2] != mp[n - 1 - y][n // 2]:
        change(n // 2, n - 1 - y)
  
  ans = 0
  if not diff_dic:
    ans += 1
  
  for _ in range(C - 1):
    d = int(input())
    for _ in range(d):
      r, c = map(int, input().split())
      r -= 1
      c -= 1
      if r < n // 2 and c < n // 2:
        change(c, n - 1 - r)
        change(n - 1 - c, r)
        change(n - 1 - c, n - 1 - r)
      elif n % 2 and r == n // 2 and c != n // 2:
        change(max(c, n - 1 - c), r)
      elif n % 2 and r != n // 2 and c == n // 2:
        change(c, max(r, n - 1 - r))
      else:
        change(c, r)
    if not diff_dic:
        ans += 1
  print(ans)
  
main()
Buggy python submission (verdict: Runtime Error):
n = int(input())
alst = list(map(int, input().split()))
edges = [[] for _ in range(n)]
rev_edges = [[] for _ in range(n)]
for i in range(n):
  edges[i].append((i + alst[i]) % n)
  rev_edges[(i + alst[i]) % n].append(i)

def dfs(x, ret, edges, visited):
  visited[x] = True
  for e in edges[x]:
    if not visited[e]:
      dfs(e, ret, edges, visited)
  ret.append(x)

def dfs_rev(x, cycles, rev_edges, visited):
  visited[x] = True
  flag = False
  for e in rev_edges[x]:
    if not visited[e]:
      cycles.add(e)
      dfs_rev(e, cycles, rev_edges, visited)
      flag = True
    elif x == e:
      flag = True
  if flag:
    cycles.add(x)

order = []
visited = [False] * n
for i in range(n):
  if not visited[i]:
    dfs(i, order, edges, visited)

order.reverse()

visited = [False] * n
cycles = set()
for i in order:
  if not visited[i]:
    dfs_rev(i, cycles, rev_edges, visited)
print(len(cycles))


Fixed submission:
import sys
sys.setrecursionlimit(1000000)

def dfs(x, order, edges, visited):
  visited[x] = True
  for e in edges[x]:
    if not visited[e]:
      dfs(e, order, edges, visited)
  order.append(x)

def dfs_rev(x, rev_edges, visited):
  visited[x] = True
  ret = 0
  for e in rev_edges[x]:
    if not visited[e]:
      ret += 1
      ret += dfs_rev(e, rev_edges, visited)
  return ret


n = int(input())
alst = list(map(int, input().split()))
edges = [[] for _ in range(n)]
rev_edges = [[] for _ in range(n)]
for i in range(n):
  edges[i].append((i + alst[i]) % n)
  rev_edges[(i + alst[i]) % n].append(i)

order = []
visited = [False] * n
for i in range(n):
  if not visited[i]:
    dfs(i, order, edges, visited)
order.reverse()

visited = [False] * n
cycles = 0
for i in order:
  if not visited[i]:
    a = dfs_rev(i, rev_edges, visited)
    if a > 0 or i in rev_edges[i]:
      a += 1
    cycles += a
print(cycles)
Buggy python submission (verdict: Time Limit Exceeded):
n = int(input())
trs = [int(input()) for _ in range(n)]
cannot = False

visited = [False] * n
visited[0] = True
for i in range(n):
  if not visited[i]:
    cannot = True
  else:
    x = min(i + trs[i] // 10, n - 1)
    if not visited[x]:
      for j in range(i + 1, x + 1):
        visited[j] = True

visited = [False] * n
visited[n - 1] = True
for i in range(n - 1, -1, -1):
  if not visited[i]:
    cannot = True
  else:
    x = max(i - trs[i] // 10, 0)
    if not visited[x]:
      for j in range(i - 1, x - 1, -1):
        visited[j] = True

if cannot:
  print("no")
else:
  print("yes")


Fixed submission:
n = int(input())
trs = [int(input()) for _ in range(n)]
cum = []
cannot = False

acc = 0
for i in range(n):
  acc = max(acc, i + trs[i] // 10)
  acc = min(acc, n -  1)
  cum.append(acc)

rev = []
acc = 0
for i in range(n):
  acc = max(acc, i + trs[n - 1 - i] // 10)
  acc = min(acc, n - 1)
  rev.append(acc)

ind = 0
tmp = 0
while True:
  ind = cum[ind]
  if ind == n - 1:
    break
  if ind == tmp:
    cannot = True
    break
  tmp = ind

ind = 0
tmp = 0
while True:
  ind = rev[ind]
  if ind == n - 1:
    break
  if ind == tmp:
    cannot = True
    break
  tmp = ind

if cannot:
  print("no")
else:
  print("yes")
Buggy python submission (verdict: Runtime Error):
V, E = int(input()), int(input())
L = []
visited = [0 for i in range(V)]
edges = [[] for i in range(V)]
#flag = 0

def visit(x):
#  if visited[x] == 1:
#    flag = 1
  if not visited[x]:
    visited[x] = 1
    for e in edges[x]:
      visit(e)
    L.insert(0,x)

for i in range(E):
  s, t = map(int,input().split())
  edges[s - 1].append(t - 1)

for i in range(V):
  if not visited[i]:
    visit(i)

flag = 0
for i in range(V):
  print(L[i] + 1)
  if not flag and i < V - 1 and (L[i + 1] not in edges[L[i]]):
    flag = 1
print(flag)


Fixed submission:
import sys
sys.setrecursionlimit(100000)

def main():
  V, E = int(input()), int(input())
  L = []
  app = L.append
  visited = [0 for i in range(V)]
  edges = [[] for i in range(V)]
  
  def visit(x):
    if not visited[x]:
      visited[x] = 1
      for e in edges[x]:
        visit(e)
      app(x)
  
  for i in range(E):
    s, t = map(int,input().split())
    edges[s - 1].append(t - 1)
  
  for i in range(V):
    if not visited[i]:
      visit(i)
  L.reverse()
  
  flag = 0
  temp = L[0]
  print(temp + 1)
  for i in range(V - 1):
    v = L[i + 1]
    print(v + 1)
    if not flag and i < V - 1 and (v not in edges[temp]):
      flag = 1
    temp = v
  print(flag)
main()

Buggy python submission (verdict: Runtime Error):
sys.setrecursionlimit(100000)
V, E = int(input()), int(input())
L = []
visited = [0 for i in range(V)]
edges = [[] for i in range(V)]
#flag = 0

def visit(x):
#  if visited[x] == 1:
#    flag = 1
  if not visited[x]:
    visited[x] = 1
    for e in edges[x]:
      visit(e)
    L.insert(0,x)

for i in range(E):
  s, t = map(int,input().split())
  edges[s - 1].append(t - 1)

for i in range(V):
  if not visited[i]:
    visit(i)

flag = 0
for i in range(V):
  print(L[i] + 1)
  if not flag and i < V - 1 and (L[i + 1] not in edges[L[i]]):
    flag = 1
print(flag)


Fixed submission:
import sys
sys.setrecursionlimit(100000)

def main():
  V, E = int(input()), int(input())
  L = []
  app = L.append
  visited = [0 for i in range(V)]
  edges = [[] for i in range(V)]
  
  def visit(x):
    if not visited[x]:
      visited[x] = 1
      for e in edges[x]:
        visit(e)
      app(x)
  
  for i in range(E):
    s, t = map(int,input().split())
    edges[s - 1].append(t - 1)
  
  for i in range(V):
    if not visited[i]:
      visit(i)
  L.reverse()
  
  flag = 0
  temp = L[0]
  print(temp + 1)
  for i in range(V - 1):
    v = L[i + 1]
    print(v + 1)
    if not flag and i < V - 1 and (v not in edges[temp]):
      flag = 1
    temp = v
  print(flag)
main()
Buggy python submission (verdict: Runtime Error):
def main()
  while True:
    m = int(input())
    if not m: break
    seiza = []
    for i in range(m):
      seiza.append(list(map(int,input().split())))
    kijun = seiza.pop(0)
    
    n = int(input())
    hosi = []
    point = {}
    
    for i in range(n):
      x,y = map(int,input().split())
      point[x,y] = 1
      hosi.append([x,y])
  
    for a in hosi:
      dx,dy = a[0] - kijun[0], a[1] - kijun[1]
      for b in seiza:
        if not (b[0] + dx, b[1] + dy) in point:
          break
      else:
        print(dx,dy)
        break
main() 


Fixed submission:
def main():
  while True:
    m = int(input())
    if not m: break
    seiza = []
    for i in range(m):
      seiza.append(list(map(int,input().split())))
    kijun = seiza.pop(0)
    
    n = int(input())
    hosi = []
    point = {}
    
    for i in range(n):
      x,y = map(int,input().split())
      point[x,y] = 1
      hosi.append([x,y])
  
    for a in hosi:
      dx,dy = a[0] - kijun[0], a[1] - kijun[1]
      for b in seiza:
        if not (b[0] + dx, b[1] + dy) in point:
          break
      else:
        print(dx,dy)
        break
main()
Buggy python submission (verdict: Time Limit Exceeded):
while True:
  r,c = map(int,input().split())
  if not r and not c:
    break
  lst = []
  for i in range(r):
    lst.append(list(map(int,input().split())))
  def dfs(l,n):
    if n == r:
      ans = 0
      for i in range(c):
        count = 0
        for j in range(r):
          if l[j][i]:
            count += 1
        ans += max(count, r - count)
      return ans
    else:
      return max(dfs(l + [[0 if i else 1 for i in lst[n]]],n + 1),
                 dfs(l + [[1 if i else 0 for i in lst[n]]],n + 1))
  print(dfs([],0))



Fixed submission:
def f():
  while True:
    r,c = map(int,input().split())
    if not r:
      break
    lst = [list(map(int,input().split())) for i in range(r)]
    dic = [0 for i in range(2 ** r)]
    for i in range(c):
      num = 0
      for j in range(r):
        num *= 2
        num += lst[j][i]
      dic[num] += 1
    ans = 0
    for i in range(2 ** r):
      ret = 0
      for j in range(2 ** r):
        num = (i ^ j)
        cnt = 0
        for k in range(r):
          cnt += num % 2
          num //= 2
        ret += max(cnt, r - cnt) * dic[j]
      ans = max(ans, ret)
    print(ans)
f()

Buggy python submission (verdict: Time Limit Exceeded):
def main():
  while True:
    r,c = map(int,input().split())
    if not r and not c:
      break
    lst = []
    for i in range(r):
      lst.append(list(map(int,input().split())))
    def dfs(l,n):
      if n == r:
        ans = 0
        for i in range(c):
          count = 0
          for j in range(r):
            if l[j][i]:
              count += 1
          ans += max(count, r - count)
        return ans
      else:
        return max(dfs(l + [[0 if i else 1 for i in lst[n]]],n + 1),
                   dfs(l + [[1 if i else 0 for i in lst[n]]],n + 1))
    print(dfs([],0))

main()


Fixed submission:
def f():
  while True:
    r,c = map(int,input().split())
    if not r:
      break
    lst = [list(map(int,input().split())) for i in range(r)]
    dic = [0 for i in range(2 ** r)]
    for i in range(c):
      num = 0
      for j in range(r):
        num *= 2
        num += lst[j][i]
      dic[num] += 1
    ans = 0
    for i in range(2 ** r):
      ret = 0
      for j in range(2 ** r):
        num = (i ^ j)
        cnt = 0
        for k in range(r):
          cnt += num % 2
          num //= 2
        ret += max(cnt, r - cnt) * dic[j]
      ans = max(ans, ret)
    print(ans)
f()
Buggy python submission (verdict: Runtime Error):
while True:
  w, h = map(int, input().split())
  if not w:
    break

  n = int(input())

  xlst = [0, w - 1]
  ylst = [0, h - 1]
  plst = []

  for i in range(n):
    x1, y1, x2, y2 = map(int, input().split())
    plst.append([x1,y1,x2,y2])
    xlst.append(x1)
#    xlst.append(x1 + 1)
    xlst.append(x2)
#    xlst.append(x2 + 1)
    ylst.append(y1)
#    ylst.append(y1 + 1)
    ylst.append(y2)
#    ylst.append(y2 + 1)

  xlst = list(set(xlst))
  ylst = list(set(ylst))
  sorted_xlst = sorted(xlst)
  sorted_ylst = sorted(ylst)

  xdic = {}
  ydic = {}

  for i, v in enumerate(xlst):
    xdic[v] = i
  for i, v in enumerate(ylst):
    ydic[v] = i

  neww = len(xlst) - 1
  newh = len(ylst) - 1

  painted = [[0] * newh for _ in range(neww)]

  def paint_area(x, y):
    painted[x][y] = 1
    to_lst = [(x - 1, y), (x + 1, y), (x, y - 1), (x, y + 1)]
    for tx, ty in to_lst:
      if 0 <= tx <= neww - 1 and 0 <= ty <= newh - 1 and not painted[tx][ty]:
        paint_area(tx, ty)

  for p in plst:
    x1, y1, x2, y2 = p
    x1, y1, x2, y2 = xdic[x1], ydic[y1], xdic[x2], ydic[y2]
    for x in range(x1, x2):
      for y in range(y1, y2):
        painted[x][y] = 1

#  for area in painted:
#    print(area)

  ans = 0
  for x in range(neww):
    for y in range(newh):
      if not painted[x][y]:
        ans += 1
        paint_area(x, y)

  print(ans)


Fixed submission:
def main():
  while True:
    w, h = map(int, input().split())
    if not w:
      break
  
    n = int(input())
  
    xlst = [0, w - 1]
    ylst = [0, h - 1]
    plst = []
  
    for i in range(n):
      x1, y1, x2, y2 = map(int, input().split())
      plst.append([x1,y1,x2 - 1,y2 - 1])
      xlst.append(x1)
      xlst.append(x2)
      xlst.append(x2 - 1)
      ylst.append(y1)
      ylst.append(y2)
      ylst.append(y2 - 1)
  
    xlst = list(set(xlst))
    ylst = list(set(ylst))
    xlst.sort()
    ylst.sort()
  
    xdic = {}
    ydic = {}
  
    for i, v in enumerate(xlst):
      xdic[v] = i
    for i, v in enumerate(ylst):
      ydic[v] = i
    
    neww = xdic[xlst[-1]]
    newh = ydic[ylst[-1]]

    painted = [[1] * (newh + 2)]
    for _ in range(neww):
      painted.append([1] + [0] * newh +  [1])
    painted.append([1] * (newh + 2))  
  
    for p in plst:
      x1, y1, x2, y2 = p
      x1, y1, x2, y2 = xdic[x1] + 1, ydic[y1] + 1, xdic[x2] + 1, ydic[y2] + 1
      for x in range(x1, x2 + 1):
        for y in range(y1, y2 + 1):
          painted[x][y] = 1
    
    stack = []
    app = stack.append
    pp = stack.pop
    direct = ((-1, 0), (1, 0), (0, -1), (0, 1))
    ans = 0

    for x in range(1, neww + 1):
      for y in range(1, newh + 1):
        if not painted[x][y]:
          ans += 1
          painted[x][y] = 1
          app((x,y))

          while stack:
            px, py = pp()
            for dx, dy in direct:
              tx, ty = px + dx, py + dy
              if not painted[tx][ty]:
                painted[tx][ty] = 1
                app((tx,ty))
    print(ans)

main()
Buggy python submission (verdict: Time Limit Exceeded):
def solve():
  while True:
    m, n  = int(input()), int(input())
    if not m:
      break
    ices = [[0] + list(map(int,input().split())) + [0] for _ in range(n)]
    ices.insert(0, [0] * (m + 2),)
    ices.append([0] * (m + 2))
    score = [[0] * (m + 2) for _ in range(n + 2)]
    
    for x in range(1, n + 1):
      for y in range(1, m + 1):
        if ices[x][y]:
          score[x][y] += (ices[x - 1][y] + ices[x + 1][y] + ices[x][y - 1] + ices[x][y + 1])
    
    def bfs(x, y, imap, acc):
#      print(x, y)
      imap[x][y] = 0
      acc += 1
      flag = 0
      a = b = c = d = 0
      if imap[x - 1][y]:
        a = bfs(x - 1, y, [[imap[i][j] for j in range(m + 2)] for i in range(n + 2)], acc)
        flag = 1
      if imap[x + 1][y]:
        b = bfs(x + 1, y, [[imap[i][j] for j in range(m + 2)] for i in range(n + 2)], acc)
        flag = 1
      if imap[x][y - 1]:
        c = bfs(x, y - 1, [[imap[i][j] for j in range(m + 2)] for i in range(n + 2)], acc)
        flag = 1
      if imap[x][y + 1]:
        d = bfs(x, y + 1, [[imap[i][j] for j in range(m + 2)] for i in range(n + 2)], acc)
        flag = 1
      if not flag:
        return acc
      return max(a, b, c, d)
    
    ans = 0
    for x in range(1, n + 1):
      for y in range(1, m + 1):
        if score[x][y] in [1, 2] and ices[x][y]:
          a = bfs(x, y, [[ices[i][j] for j in range(m + 2)] for i in range(n + 2)], 0)
          #a = bfs(x, y, ices, 0)
          if ans < a:
            ans = a
    
    print(ans)

solve()


Fixed submission:
def search(x, y, imap, acc):
  imap[x][y] = 0
  acc += 1
  ret = acc
  a = b = c = d = 0
  if imap[x - 1][y]:
    a = search(x - 1, y, imap, acc)
    if ret < a:
      ret = a
  if imap[x + 1][y]:
    b = search(x + 1, y, imap, acc)
    if ret < b:
      ret = b
  if imap[x][y - 1]:
    c = search(x, y - 1, imap, acc)
    if ret < c:
      ret = c
  if imap[x][y + 1]:
    d = search(x, y + 1, imap, acc)
    if ret < d:
      ret = d
  imap[x][y] = 1
  return ret
 

def solve():
  while True:
    m, n  = int(input()), int(input())
    if not m:
      break
    ices = [[0] + list(map(int,input().split())) + [0] for _ in range(n)]
    ices.insert(0, [0] * (m + 2),)
    ices.append([0] * (m + 2))
    score = [[0] * (m + 2) for _ in range(n + 2)]
    
    for x in range(1, n + 1):
      for y in range(1, m + 1):
        if ices[x][y]:
          score[x - 1][y] += 1
          score[x + 1][y] += 1
          score[x][y - 1] += 1
          score[x][y + 1] += 1
   
    ans = 0
    for x in range(1, n + 1):
      for y in range(1, m + 1):
        if score[x][y] in [1, 2] and ices[x][y]:
          a = search(x, y, ices, 0)
          if ans < a:
            ans = a
    
    print(ans)

solve()
Buggy python submission (verdict: Time Limit Exceeded):
import sys
sys.setrecursionlimit(100000)
while True:
  n,m,h,k = map(int,input().split())
  if not n: break
  s_lst = [int(input()) for _ in range(n)]
  b_lst = [tuple(map(int,input().split())) for _ in range(m)]
  b_lst = sorted(b_lst,key=lambda x:-x[1])
  def amida(b_lst, s_lst, used, k):
    if not b_lst:
      return sum(s_lst[0:k])
    elif used:
      for bar in b_lst:
        a = bar[0]
        temp = s_lst[a]
        s_lst[a] = s_lst[a - 1]
        s_lst[a - 1] = temp
      return sum(s_lst[0:k])
    else:
      save_s_lst = [i for i in s_lst]
      a = b_lst[0][0]
      temp = s_lst[a]
      s_lst[a] = s_lst[a - 1]
      s_lst[a - 1] = temp
      return min(amida(b_lst[1:], s_lst, 0, k), amida(b_lst[1:], save_s_lst, 1, k))

  print(amida(b_lst, s_lst, 0, k))



Fixed submission:
while True:
  n,m,h,k = map(int,input().split())
  if not n: break
  s_lst = [int(input()) for _ in range(n)]
  i_lst = [i for i in range(n)]
  b_lst = [tuple(map(int,input().split())) for _ in range(m)]
  b_lst = sorted(b_lst,key=lambda x:x[1])
  pare_lst = []
  for bar in b_lst:
    ind = bar[0]
    temp = i_lst[ind]
    i_lst[ind] = i_lst[ind - 1]
    i_lst[ind - 1] = temp
    pare_lst.append((i_lst[ind], i_lst[ind - 1]))
#  print(i_lst)
  new_s_lst = [0] * n
  for i in range(n):
    new_s_lst[i_lst[i]] = s_lst[i]
#  print(s_lst)
  ans = sum(new_s_lst[:k])
#  print(ans)
#  print(pare_lst)
  max_dec = 0
  for pare in pare_lst:
    a = pare[0]
    b = pare[1]
#    print(a,b,s_lst[a],s_lst[b])
    if a < k and b >= k and new_s_lst[a] - new_s_lst[b] > max_dec:
      max_dec = new_s_lst[a] - new_s_lst[b]
    if b < k and a >= k and new_s_lst[b] - new_s_lst[a] > max_dec:
      max_dec = new_s_lst[b] - new_s_lst[a]
  print(ans - max_dec)
Buggy python submission (verdict: Time Limit Exceeded):
while True:
  h,w,n = map(int,input().split())
  if not h: break
  sss = [[True if s == "1" else False for s in input().split()] for i in range(h)]
  for i in range(n):
    x = y = 0
    while x < h and y < w:
      sss[x][y] = not sss[x][y]
      if sss[x][y]:
        x += 1
      else:
        y += 1
  print(x + 1,y + 1)



Fixed submission:
def solve():
  while True:
    h,w,n = map(int,input().split())
    if not h: break
    mp = [[int(s) for s in input().split()] for i in range(h)]
    dp = [[0 for i in range(w + 1)] for j in range(h + 1)]
    dp[0][0] = n - 1
    for x in range(h):
      dpx = dp[x]
      dpxp = dp[x + 1]
      mpx = mp[x]
      for y in range(w):
        a = dpx[y]
        b = mpx[y]
        if b:
          if a % 2:
            dpxp[y] += a // 2
            dpx[y + 1] += a // 2 + 1
          else:
            dpxp[y] += a // 2
            dpx[y + 1] += a // 2
        else:
          if a % 2:
            dpxp[y] += a // 2 + 1
            dpx[y + 1] += a // 2
          else:
            dpxp[y] += a // 2
            dpx[y + 1] += a // 2
        mpx[y] = (a + b) % 2
  
    x = y = 0
    while x < h and y < w:
      if mp[x][y]:
        y += 1
      else:
        x += 1
    print(x + 1,y + 1)
solve()
Buggy python submission (verdict: Runtime Error):
def solve():
  n,l = map(int,input().split())
  
  length = [int(input()) for i in range(n)]
  pare = [(length[i], i) for i in range(n)]
  pare.sort(reverse=True)
  dp = [0] * (n + 1)
  for p in pare:
    i = p[0]
    if i == 0:
      dp[i] = dp[i + 1] + (l - length[i])
    else:
      dp[i] = max(dp[i - 1], dp[i + 1]) + (l - length[i])
  print(max(dp))
solve()


Fixed submission:
def solve():
  n, l = map(int,input().split())
  ans = 0
  pre = 0
  trend = 1
  acc = 0
  for i in range(n):
    length = int(input())
    time = l - length
    if length > pre and trend >= 0:
      acc += time
    elif length < pre and trend <= 0:
      acc += time
    else:
      if ans < acc:
        ans = acc
      acc = time + (l - pre)
      trend = length - pre
    pre = length
  else:
    ans = max(ans, acc)
  print(ans)
solve()
Buggy python submission (verdict: Runtime Error):
from heapq import heappop as pop
from heapq import heappush as push
INF = 10 ** 20
n, k = map(int, input().split())

clst = []
rlst = []

for i in range(n):
  c, r = map(int, input().split())
  clst.append(c)
  rlst.append(r)

edges = [[0] * n for i in range(n)]

for i in range(k):
  a, b = map(int, input().split())
  a -= 1
  b -= 1
  edges[a][b] = edges[b][a] = 1

costs = [INF for i in range(n)]
costs[0] = 0

def make_to_lst(s_num):
  loop = rlst[s_num]
  temp = {i for i in range(n) if edges[s_num][i]}
  ret = {i for i in temp}
  while loop - 1:
    new = set()
    for p in temp:
      pto = {i for i in range(n) if edges[p][i]}
      new = new | pto
    ret = ret | temp
    temp = new - ret
    loop -= 1
  return ret

costs[0] = 0
break_flag = 0
que = [(clst[0], 0)]

while que and not break_flag:
  next_cost, s_num = pop(que)
  to_lst = make_to_lst(s_num)
  
  for num in to_lst:
    if costs[num] > next_cost:
      costs[num] = next_cost
    if num == n - 1:
      break_flag = 1
      break
    push(que, (next_cost + clst[num], num))

print(costs[n - 1])


Fixed submission:
from heapq import heappop as pop
from heapq import heappush as push

def main():
  n, k = map(int, input().split())
  
  clst = []
  rlst = []
  
  for i in range(n):
    c, r = map(int, input().split())
    clst.append(c)
    rlst.append(r)
  
  edges = [[] * n for i in range(n)]
  
  for i in range(k):
    a, b = map(int, input().split())
    a -= 1
    b -= 1
    edges[a].append(b)
    edges[b].append(a)
  
  costs = [None] * n
  used = [False] * n
  
  def make_to_lst(s_num):
    loop = rlst[s_num]
    temp = set(edges[s_num])
    ret = set()
    while loop and temp:
      new = set()
      for p in temp:
        pto = set(edges[p])
        new = new | pto
      ret = ret | temp
      temp = new - ret
      loop -= 1
    return ret
  
  used[0] = True
  costs[0] = 0
  que = [(clst[0], 0)]
  
  while que:
    next_cost, s_num = pop(que)
    to_lst = make_to_lst(s_num)

    for num in to_lst:
      if num == n - 1:
        print(next_cost)
        return

      costs[num] = next_cost

      if not used[num]:
        push(que, (next_cost + clst[num], num))
        used[num] = True
  

main()