Problem p03333 (python)

Fixed submission:
import sys
input = sys.stdin.readline
N = int(input())
a = []
l = [i for i in range(N)]
r = [i for i in range(N)]
for i in range(N):
  x, y = map(int, input().split())
  a.append((x, y))
l.sort(key = lambda x: -a[x][1])
r.sort(key = lambda x: a[x][0])
#print(l, r)
res = 0
vis = [0] * N
x = 0
for i in range(N):
  #print(l, r, vis, x, res)
  if i % 2 == 1 and (len(l) > 0):
    t = l[-1]
    y = a[l.pop()][1]
    while vis[t]:
      t = l.pop()
      y = a[t][1]
      vis[t] = 1
      if len(l) == 0: break
    vis[t] = 1
    if not x in range(a[t][0], a[t][1]):
      res += x - min(x, y)
      x = min(x, y)
  elif i % 2 == 0 and (len(r) > 0):
    t = r[-1]
    y = a[r.pop()][0]
    while vis[t]:
      t = r.pop()
      y = a[t][0]
      vis[t] = 1
      if len(r) == 0: break
    vis[t] = 1
    if not x in range(a[t][0], a[t][1]):
      res += max(x, y) - x
      x = max(x, y)
res += abs(x)
#print(res)
rres = 0
l = [i for i in range(N)]
r = [i for i in range(N)]
l.sort(key = lambda x: -a[x][1])
r.sort(key = lambda x: a[x][0])
vis = [0] * N
x = 0
for i in range(N):
  #print(l, r, vis, x, rres)
  if i % 2 == 0 and (len(l) > 0):
    t = l[-1]
    y = a[l.pop()][1]
    while vis[t]:
      t = l.pop()
      y = a[t][1]
      vis[t] = 1
      if len(l) == 0: break
    vis[t] = 1
    if not x in range(a[t][0], a[t][1]):
      rres += x - min(x, y)
      x = min(x, y)
  elif i % 2 == 1 and (len(r) > 0):
    t = r[-1]
    y = a[r.pop()][0]
    while vis[t]:
      t = r.pop()
      y = a[t][0]
      vis[t] = 1
      if len(r) == 0: break
    vis[t] = 1
    if not x in range(a[t][0], a[t][1]):
      rres += max(x, y) - x
      x = max(x, y)
rres += abs(x)
print(max(res, rres))

Buggy submission (verdict: Runtime Error):
import sys
input = sys.stdin.readline
N = int(input())
a = []
l = [i for i in range(N)]
r = [i for i in range(N)]
for i in range(N):
  x, y = map(int, input().split())
  a.append((x, y))
l.sort(key = lambda x: -a[x][1])
r.sort(key = lambda x: a[x][0])
#print(l, r)
res = 0
vis = [0] * N
x = 0
for i in range(N):
  #print(l, r, vis, x, res)
  if i % 2:
    t = l[-1]
    y = a[l.pop()][1]
    while vis[t]:
      t = l.pop()
      y = a[t][1]
      vis[t] = 1
      if len(l) == 0: break
    vis[t] = 1
    if not x in range(a[t][0], a[t][1]):
      res += x - min(x, y)
      x = min(x, y)
  else:
    t = r[-1]
    y = a[r.pop()][0]
    while vis[t]:
      t = r.pop()
      y = a[t][0]
      vis[t] = 1
      if len(r) == 0: break
    vis[t] = 1
    if not x in range(a[t][0], a[t][1]):
      res += max(x, y) - x
      x = max(x, y)
res += abs(x)
rres = 0
l = [i for i in range(N)]
r = [i for i in range(N)]
l.sort(key = lambda x: -a[x][1])
r.sort(key = lambda x: a[x][0])
vis = [0] * N
x = 0
for i in range(N):
  #print(l, r, vis, x, rres)
  if i % 2 == 0:
    t = l[-1]
    y = a[l.pop()][1]
    while vis[t]:
      t = l.pop()
      y = a[t][1]
      vis[t] = 1
      if len(l) == 0: break
    vis[t] = 1
    if not x in range(a[t][0], a[t][1]):
      rres += x - min(x, y)
      x = min(x, y)
  else:
    t = r[-1]
    y = a[r.pop()][0]
    while vis[t]:
      t = r.pop()
      y = a[t][0]
      vis[t] = 1
      if len(r) == 0: break
    vis[t] = 1
    if not x in range(a[t][0], a[t][1]):
      rres += max(x, y) - x
      x = max(x, y)
rres += abs(x)
print(max(res, rres))

Buggy submission (verdict: Runtime Error):
import sys
input = sys.stdin.readline
N = int(input())
a = []
l = [i for i in range(N)]
r = [i for i in range(N)]
for i in range(N):
  x, y = map(int, input().split())
  a.append((x, y))
l.sort(key = lambda x: -a[x][1])
r.sort(key = lambda x: a[x][0])
#print(l, r)
res = 0
vis = [0] * N
x = 0
for i in range(N):
  if i % 2:
    t = l[-1]
    y = a[l.pop()][1]
    while vis[t]:
      t = l.pop()
      y = a[t][1]
      vis[t] = 1
      if len(l) == 0: break
    vis[t] = 1
    if not x in range(a[t][0], a[t][1]):
      res += x - min(x, y)
      x = min(x, y)
  else:
    t = r[-1]
    y = a[r.pop()][0]
    while vis[t]:
      t = r.pop()
      y = a[t][0]
      vis[t] = 1
      if len(r) == 0: break
    vis[t] = 1
    if not x in range(a[t][0], a[t][1]):
      res += max(x, y) - x
      x = max(x, y)
res += abs(x)
rres = 0
l = [i for i in range(N)]
r = [i for i in range(N)]
l.sort(key = lambda x: -a[x][1])
r.sort(key = lambda x: a[x][0])
vis = [0] * N
x = 0
for i in range(N):
  if i % 2 == 0:
    t = l[-1]
    y = a[l.pop()][1]
    while vis[t]:
      t = l.pop()
      y = a[t][1]
      vis[t] = 1
      if len(l) == 0: break
    vis[t] = 1
    if not x in range(a[t][0], a[t][1]):
      rres += x - min(x, y)
      x = min(x, y)
  else:
    t = r[-1]
    y = a[r.pop()][1]
    while vis[t]:
      t = r.pop()
      y = a[t][1]
      if len(r) == 0: break
    vis[t] = 1
    if not x in range(a[t][0], a[t][1]):
      rres += max(x, y) - x
      x = max(x, y)
rres += abs(x)
print(max(res, rres))

Buggy submission (verdict: Runtime Error):
import sys
input = sys.stdin.readline
N = int(input())
a = []
l = [i for i in range(N)]
r = [i for i in range(N)]
for i in range(N):
  x, y = map(int, input().split())
  a.append((x, y))
l.sort(key = lambda x: -a[x][1])
r.sort(key = lambda x: a[x][0])
#print(l, r)
res = 0
vis = [0] * N
x = 0
for i in range(N):
  if i % 2:
    t = l[-1]
    y = a[l.pop()][1]
    while vis[t]:
      t = l.pop()
      y = a[t][1]
      vis[t] = 1
      if len(l) == 0: break
    vis[t] = 1
    res += x - min(x, y)
    x = min(x, y)
  else:
    t = r[-1]
    y = a[r.pop()][0]
    while vis[t]:
      t = r.pop()
      y = a[t][0]
      vis[t] = 1
      if len(r) == 0: break
    vis[t] = 1
    res += max(x, y) - x
    x = max(x, y)
res += abs(x)
rres = 0
l = [i for i in range(N)]
r = [i for i in range(N)]
l.sort(key = lambda x: -a[x][1])
r.sort(key = lambda x: a[x][0])
vis = [0] * N
x = 0
for i in range(N):
  if i % 2 == 0:
    t = l[-1]
    y = a[l.pop()][1]
    while vis[l[-1]]:
      t = l.pop()
      y = a[t][1]
      vis[t] = 1
      if len(l) == 0: break
    vis[t] = 1
    rres += x - min(x, y)
    x = min(x, y)
  else:
    t = r[-1]
    y = a[r.pop()][1]
    while vis[r[-1]]:
      t = r.pop()
      y = a[t][1]
      if len(r) == 0: break
    vis[t] = 1
    rres += max(x, y) - x
    x = max(x, y)
rres += abs(x)
print(max(res, rres))