Problem: Takahashi and Aoki will play a game with a number line and some segments. Takahashi is standing on the number line and he is initially at coordinate 0. Aoki has N segments. The i-th segment is [L_i,R_i], that is, a segment consisting of points with coordinates between L_i and R_i (inclusive).  The game has N steps. The i-th step proceeds as follows:    * First, Aoki chooses a segment that is still not chosen yet from the N segments and tells it to Takahashi.   * Then, Takahashi walks along the number line to some point within the segment chosen by Aoki this time.  After N steps are performed, Takahashi will return to coordinate 0 and the game ends.  Let K be the total distance traveled by Takahashi throughout the game. Aoki will choose segments so that K will be as large as possible, and Takahashi walks along the line so that K will be as small as possible. What will be the value of K in the end?

Submission attempts (in order):
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()


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()

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))

Verdicts: Runtime Error

Runtime Error

Accepted