Problem p03333 (python)

Fixed submission:
import sys
stdin = sys.stdin

def li(): return map(int, stdin.readline().split())
def li_(): return map(lambda x: int(x)-1, stdin.readline().split())
def lf(): return map(float, stdin.readline().split())
def ls(): return stdin.readline().split()
def ns(): return stdin.readline().rstrip()
def lc(): return list(ns())
def ni(): return int(stdin.readline())
def nf(): return float(stdin.readline())

n = ni()
left = []
right = []
for _ in range(n):
    l,r = li()
    left.append(l)
    right.append(r)
    
left.sort(reverse=True)
right.sort(reverse=False)

ans_l = 0
last = 0
for i in range(n):
    if left[i] - last < 0:
        ans_l += abs(last)
        break
    else:
        ans_l += (left[i] - last) 
        last = left[i]
    
    if last - right[i] < 0:
        ans_l += abs(last)
        break
    else:
        ans_l += (last - right[i])
        last = right[i]
    
ans_r = 0
last = 0
for i in range(n):
    if last - right[i] < 0:
        ans_r += abs(last)
        break
    else:
        ans_r += (last - right[i])
        last = right[i]
    
    if left[i] - last < 0:
        ans_r += abs(last)
        break
    else:
        ans_r += (left[i] - last) 
        last = left[i]
    


print(max(ans_l, ans_r))

Buggy submission (verdict: Runtime Error):
def main():
    
    #0 input
    N = int(input())
    LR = []
    for i in range(N):
        LR.append(list(map(int, input().split())))
    

    
    LR_sorted_tmp = sorted(LR, reverse=False)
    LR_sorted = []
    for part in LR_sorted_tmp:
        if part[0] > 0:
            LR_sorted.append(part)

    LR_sorted_R = sorted(LR, key=lambda x:x[1], reverse=True)
    LR_sorted_R_tmp = []
    for part in LR_sorted_R_tmp:
        if part[0] < 0:
            LR_sorted_R.append(part)
    
    takahashi = 0
    moved = 0

    while 1:
        #1 choose maximum L
        next_interval  = LR_sorted[-1]
        
        # finish if Takahashi is in the next interval
        if next_interval[0] <= takahashi and takahashi <= next_interval[1]:
            break
        
        moved += next_interval[0] - takahashi
        takahashi = next_interval[0]
        
        # delete the current interval
        del LR_sorted[-1]

        # finish if no items in the list
        if len(LR_sorted) == 0:
            break


        
        #2 choose minimum R      
        next_interval = LR_sorted_R[-1]
        
        # finish if Takahashi is in the next interval
        if next_interval[0] <= takahashi and takahashi <= next_interval[1]:
            break
        
        moved += takahashi - next_interval[1]
        takahashi = next_interval[1]
        
        # delete the current interval
        del LR_sorted_R[-1]


        # finish if no items in the list
        if len(LR_sorted) == 0:
            break
        
        
    moved += abs(0-takahashi)
    print(moved)


if __name__ == "__main__":
    main()

Buggy submission (verdict: Runtime Error):
def main():
    
    #0 input
    N = int(input())
    LR = []
    for i in range(N):
        LR.append(list(map(int, input().split())))
    

    
    LR_sorted_tmp = sorted(LR, reverse=False)
    LR_sorted = []
    for part in LR_sorted_tmp:
        if part[0] > 0:
            LR_sorted.append(part)

    LR_sorted_R = sorted(LR, key=lambda x:x[1], reverse=True)
    LR_sorted_R_tmp = []
    for part in LR_sorted_R_tmp:
        if part[0] < 0:
            LR_sorted_R.append(part)
    
    takahashi = 0
    moved = 0

    while 1:
        #1 choose maximum L
        next_interval  = LR_sorted[-1]
        
        # finish if Takahashi is in the next interval
        if next_interval[0] <= takahashi and takahashi <= next_interval[1]:
            break
        
        moved += next_interval[0] - takahashi
        takahashi = next_interval[0]
        
        # delete the current interval
        del LR_sorted[-1]

        # finish if no items in the list
        if len(LR_sorted) == 0:
            break


        
        #2 choose minimum R      
        next_interval = LR_sorted_R[-1]
        
        # finish if Takahashi is in the next interval
        if next_interval[0] <= takahashi and takahashi <= next_interval[1]:
            break
        
        moved += takahashi - next_interval[1]
        takahashi = next_interval[1]
        
        # delete the current interval
        del LR_sorted_R[-1]


        # finish if no items in the list
        if len(LR_sorted) == 0:
            break
        
        
    moved += abs(0-takahashi)
    print(moved)


if __name__ == "__main__":
    main()