Problem:


Score : 600 points

Problem Statement

10^9 contestants, numbered 1 to 10^9, will compete in a competition.
There will be two contests in this competition.

The organizer prepared N problems, numbered 1 to N, to use in these contests.
When Problem i is presented in a contest, it will be solved by all contestants from Contestant L_i to Contestant R_i (inclusive), and will not be solved by any other contestants.

The organizer will use these N problems in the two contests.
Each problem must be used in exactly one of the contests, and each contest must have at least one problem.

The joyfulness of each contest is the number of contestants who will solve all the problems in the contest.
Find the maximum possible total joyfulness of the two contests.

Constraints

2 \leq N \leq 10^5

1 \leq L_i \leq R_i \leq 10^9

All values in input are integers.

Input

Input is given from Standard Input in the following format:

N
L_1 R_1
L_2 R_2
\vdots
L_N R_N

Output

Print the maximum possible total joyfulness of the two contests.

Sample Input 1

4
4 7
1 4
5 8
2 5

Sample Output 1

6

The optimal choice is:

Use Problem 1 and 3 in the first contest. Contestant 5, 6, and 7 will solve both of them, so the joyfulness of this contest is 3.

Use Problem 2 and 4 in the second contest. Contestant 2, 3, and 4 will solve both of them, so the joyfulness of this contest is 3.

The total joyfulness of these two contests is 6. We cannot make the total joyfulness greater than 6.

Sample Input 2

4
1 20
2 19
3 18
4 17

Sample Output 2

34

Sample Input 3

10
457835016 996058008
456475528 529149798
455108441 512701454
455817105 523506955
457368248 814532746
455073228 459494089
456651538 774276744
457667152 974637457
457293701 800549465
456580262 636471526

Sample Output 3

540049931



Sample input:
4
4 7
1 4
5 8
2 5


Sample output:
6

Submission status: Runtime Error

Solution code:
n = int(input())
lr = [list(map(int, input().split())) for i in range(n)]

g1, g2, g3 = [], [], []
M = 0

dif = []

for i in range(n):
    l, r = lr[i][0], lr[i][1]
    dif.append([r-l+1, i])
    M = max(M, r-l+1)
    if g1 == []:
        g1.append(l)
        g1.append(r)
    if r <= g1[0] or l >= g1[1]:
        if g2 == []:
            g2.append(l)
            g2.append(r)
        elif r <= g2[0] or l >= g2[1]:
            g3.append([l, r])
        else:
            g2[0] = max(g2[0], l)
            g2[1] = min(g2[1], r)
    else:
        g1[0] = max(g1[0], l)
        g1[1] = min(g1[1], r)

if g2 == []:
    dif = sorted(dif, key=lambda x: x[0], reverse=True)
    if lr[dif[0][1]][0] == g1[0] or lr[dif[0][1]][1] == g1[1]:
        lr.pop(dif[0][1])
        g4 = []
        for i in range(n-1):
            l, r = lr[i][0], lr[i][1]
            if g4 == []:
                g4.append(l)
                g4.append(r)
            g4[0] = max(g4[0], l)
            g4[1] = min(g4[1], r)
        print(max(M + g1[1]-g1[0]+1, M + g4[1]-g4[0]+1))
    else:
        print(M + g1[1]-g1[0]+1)
elif g3 == []:
    print(max(M, g1[1]-g1[0]+1 + g2[1]-g2[0]+1))
else:
    g5, g6, g7 = [], [], []
    for i in range(n):
    l, r = lr[i][0], lr[i][1]
    if g5 == []:
        g5.append(l)
        g5.append(r)
    if r < g5[0] or l > g5[1]:
        if g6 == []:
            g6.append(l)
            g6.append(r)
        elif r < g6[0] or l > g6[1]:
            g7.append([l, r])
        else:
            g6[0] = max(g6[0], l)
            g6[1] = min(g6[1], r)
    else:
        g5[0] = max(g5[0], l)
        g5[1] = min(g5[1], r)
    if g7 == []:
        print(M+1)
    else:
        print(M)

Submission status: Runtime Error

Solution code:
N = int(input())

array = []

for i in range(N):
    L, R = [int(v) for v in input().split()]
    size = R - L + 1
    array.append((L, R))

def score(bit):
    if bit == 0:
        return 0
    i = 0
    l1 = 0
    r1 = None
    l2 = 0
    r2 = None
    while i < N:
        l, r = array[i]
        if (bit & 1) == 1:
            if l1 < l:
                l1 = l
            if r1 is None or r1 > r:
                r1 = r
        else:
            if l2 < l:
                l2 = l
            if r2 is None or r2 > r:
                r2 = r

        bit = (bit >> 1)
        i += 1

    if r1 is None:
        s1 = 0
    else:
        s1 = (r1 - l1 + 1)
        if s1 < 0:
            s1 = 0

    if r2 is None:
        s2 = 0
    else:
        s2 = (r2 - l2 + 1)
        if s2 < 0:
            s2 = 0
    return s1 + s2

def search(i, bit):
    if i == N:
        return score(bit)

    bit1 = bit + (1 << i)
    sc1 = search(i+1, bit)
    sc2 = search(i+1, bit1)
    return max(sc1, sc2)

best = search(1, 1)
print(best)

Submission status: Runtime Error

Solution code:
def f(li, new):
    tmp_li = [li[0], li[1], new]
    #tmp_li.sort(reverse=True, key=lambda x: x[2])
    l1, r1, d1 = tmp_li[0]
    l2, r2, d2 = tmp_li[1]
    l3, r3, d3 = tmp_li[2]
    l = [l1, l2, l3]
    r = [r1, r2, r3]
    d = [d1, d2, d3]

    mn = float("inf")
    for i in range(3):
        new_l = max(l[i-1], l[i-2])
        new_r = min(r[i-1], r[i-2])
        new_d = max(0, new_r - new_l + 1)
        if new_d == 0:
            raise Exception
        d_diff = d[i-1] + d[i-2] - new_d
        if d_diff < mn:
            mn = d_diff
            left = i
            new = [new_l, new_r, new_d]

    ret = [[l[left], r[left], d[left]], new]
    #ret.sort(reverse=True, key=lambda x: x[2])
    return ret


def main():
    n = int(input())
    lr = [list(map(int, input().split())) for _ in range(n)]

    lrd = []
    for l, r in lr:
        lrd.append([l, r, r - l + 1])

    #lrd.sort(reverse=True, key=lambda x: x[2])

    ans = [lrd[0], lrd[1]]

    for li in lrd[2:]:
        ans = f(ans, li)

    print(ans[0][2] + ans[1][2])


if __name__ == "__main__":
    main()

Submission status: Runtime Error

Solution code:
N = int(input())
LR = [list(map(int,input().split())) for _ in range(N)]

ML = max(LR)[0]
mR = min(LR,key=lambda x:x[1])[1]

if ML > mR:
    LR.sort(key=lambda x:[x[1],x[0]])
    L = [LR[i][0] for i in range(N)]
    for i in range(N):
        r = LR[i][1]
        if ML <= r:
            break

    L_ = [0]*N
    for j in range(N):
        L_[j] = max(L[j],L_[j-1])

    ans = LR[i][1]-ML+1
    for j in range(max(i,1),N):
        ans = max(ans,LR[j][1]-ML+1+max(mR-L_[j-1]+1,0))

else:
    l,r = min(LR,key=lambda x:x[1])
    LR.sort()
    R = [LR[i][1] for i in range(N)]
    for i in range(N):
        if LR[i][0] > l:
            break

    R_ = [0]*N
    R_[-1] = R[-1]
    for j in range(N-2,i-1,-1):
        R_[j] = min(R[j],R_[j+1])

    ans = mR-LR[i-1][0]+1
    for j in range(i-1,N-2):
        ans = max(ans,mR-L[j]+1+R_[j+1]-ML+1)

print(ans)

Submission status: Runtime Error

Solution code:
N = int(input())
LR = [list(map(int,input().split())) for _ in range(N)]

ML = max(LR)[0]
mR = min(LR,key=lambda x:x[1])[1]

if ML > mR:
    LR.sort(key=lambda x:[x[1],x[0]])
    L = [LR[i][0] for i in range(N)]
    for i in range(N):
        r = LR[i][1]
        if ML <= r:
            break

    L_ = [0]*N
    for j in range(N):
        L_[j] = max(L[j],L_[j-1])

    ans = LR[i][1]-ML+1
    for j in range(max(i,1),N):
        ans = max(ans,LR[j][1]-ML+1+max(mR-L_[j-1]+1,0))

else:
    l,r = min(LR,key=lambda x:x[1])
    LR.sort()
    R = [LR[i][1] for i in range(N)]
    for i in range(N):
        if LR[i][0] > l:
            break

    R_ = [0]*N
    R_[-1] = R[-1]
    for j in range(N-2,i-1,-1):
        R_[j] = min(R[j],R_[j+1])

    ans = mR-LR[i-1][0]+1
    for j in range(i-1,N-1):
        ans = max(ans,mR-L[j]+1+R_[j+1]-ML+1)

print(ans)

Submission status: Runtime Error

Solution code:
N = int(input())
LR = []
for i in range(N):
    L, R = map(int, input().split())
    LR.append([L, R])

LR.sort(key=lambda x:x[0])

Lmin = LR[0][0]
Lmax = LR[0][1]
Rmin = LR[-3][0]
Rmax = LR[-3][1]
lpre = Lmax - Lmin + 1
rpre = Rmax - Rmin + 1

for a in LR:
    lmin = max(Lmin, a[0])
    lmax = min(Lmax, a[1])

    rmin = max(Rmin, a[0])
    rmax = min(Rmax, a[1])

    lll = max(0, lmax - lmin + 1)
    rrr = max(0, rmax - rmin + 1)

    l = lll - lpre
    r = rrr - rpre

    if l >= r:
        Lmin = lmin
        Lmax = lmax
        lpre = lll
    
    else:
        Rmin = rmin
        Rmax = rmax
        rpre = rrr

lcnt = max(0, Lmax - Lmin + 1)
rcnt = max(0, Rmax - Rmin + 1)
print(lcnt + rcnt)

Submission status: Runtime Error

Solution code:
def get_p(l1,l2):
    l3 = [0,0]
    l3[0] = max(l1[0],l2[0])
    l3[1] = min(l1[1],l2[1])
    
    return l3
    
def add_a_b(a,b,lis,i,n):
    if i == n:
        global max_num
        num = a[1] - a[0] + b[1] - b[0] + 2
        max_num = max(max_num,num)
        return
    
    # aに追加
    pre_a0 = a[0]
    pre_a1 = a[1]
    if a == [0,0]:
        a[0] = lis[i][0]
        a[1] = lis[i][1]
    else:
        a = get_p(a,lis[i])
        
    if a[0] < a[1]:
        add_a_b(a,b,lis,i+1,n)
    a[0] = pre_a0
    a[1] = pre_a1
    
    # bに追加
    pre_b0 = b[0]
    pre_b1 = b[1]
    
    if b == [0,0]:
        b[0] = lis[i][0]
        b[1] = lis[i][1]
    else:
        b = get_p(b,lis[i])
        
    if b[0] < b[1]:
        add_a_b(a,b,lis,i+1,n)
    b[0] = pre_b0
    b[1] = pre_b1
    
n = int(input())
lis = [0 for i in range(n)]
for i in range(n):
    lis[i] = [int(x) for x in input().split()]
    
a = [0,0]
b = [0,0]

max_num = 0
i = 0
add_a_b(a,b,lis,i,n)
print(max_num)

Submission status: Runtime Error

Solution code:
n = int(input())
problem_list = []
r = 10**9+1
p_list = []
for i in range(n):
    a, b = [ int(v) for v in input().split() ]
    p_list.append(a)
    p_list.append(b)
    problem_list.append([b, a])
problem_list.sort()
problem_list = [ [i[1],i[0]] for i in problem_list ]
problem_list.sort()
p_list.sort()
problem_list = [ ( bisect.bisect_left(p_list,i[0]), bisect.bisect_left(p_list,i[1]) )  for i in problem_list ]

left_list = [ 0 for i in range(2*n) ]
right_list = [ 0 for i in range(2*n) ]
for a, b in problem_list:
    left_list[a] += 1
    right_list[b] += 1

ans = 0
la = problem_list[-1][0]
ra = None
lenr = len(right_list) 
for i in range(lenr):
    if right_list[i] > 0:
        ra = i
        break
lb, rb = -1, 10**6+1

for a, b in problem_list[:-1]:
    
    left_list[a] -= 1
    right_list[b] -= 1
    lb = max(lb,a)
    rb = min(rb,b)

    if left_list[la] == 0:
        for i in range(1,2**n):
            if left_list[la+i] != 0:
                la += i
                break
    if right_list[ra] == 0:
        for i in range(1,2**n):
            if right_list[ra+i] != 0:
                ra += i
                break

    ans = max(ans,(max(p_list[rb]-p_list[lb]+1,0)+max(p_list[ra]-p_list[la]+1,0)))

print(ans)

Submission status: Runtime Error

Solution code:
def main():
    n = int(input())
    lr = [list(map(int, input().split())) for _ in [0] * n]

    lr.sort(key=lambda x: (+x[1] - x[0]))
    box0 = [lr[0][0], lr[0][1]]
    box1 = [lr[1][0], lr[1][1]]
    for i in range(2, n):
        a1 = max(min(lr[i][1], box0[1]) - max(lr[i][0], box0[0])+1, 0) + max(box1[1] - box1[0]+1, 0)
        a2 = max(min(lr[i][1], box1[1]) - max(lr[i][0], box1[0])+1, 0) + max(box0[1] - box0[0]+1, 0)
        a3 = max(min(box0[1], box1[1]) - max(box0[0], box1[0])+1, 0) + max(lr[i][1] - lr[i][0]+1, 0)
        m = max(a1, a2, a3)
        if a1 == m:
            box0[0] = max(lr[i][0], box0[0])
            box0[1] = min(lr[i][1], box0[1])
        elif a2 == m:
            box1[0] = max(lr[i][0], box1[0])
            box1[1] = min(lr[i][1], box1[1])
        else:
            box0[0] = max(box0[0], box1[0])
            box0[1] = min(box0[1], box1[1])
            box1[0] = lr[i][0]
            box1[1] = lr[i][1]
    print(m)
main()

Submission status: Runtime Error

Solution code:
N = int(input())

array = []

min_size = None
max_size = 0

for i in range(N):
    L, R = [int(v) for v in input().split()]
    size = R - L + 1

    if max_size < size:
        max_size = size
    if min_size is None or min_size > size:
        min_size = size

    array.append((L, R))


def score(bit):
    if bit == 0:
        return 0
    i = 0
    max_L = 0
    min_R = None
    while i < N and bit > 0:
        if (bit & 1) == 1:
            l, r = array[i]
            if max_L < l:
                max_L = l
            if min_R is None or min_R > r:
                min_R = r

        bit = (bit >> 1)
        i += 1
    s = min_R - max_L + 1
    if s < 0:
        return 0
    return s

def search(i, bit):
    if i == N:
        return score(bit) + score((1 << N) - 1 - bit)

    bit1 = bit + (1 << i)
    sc1 = search(i+1, bit)
    sc2 = search(i+1, bit1)
    return max(sc1, sc2)

best = search(1, 1)
print(best)

Submission status: Runtime Error

Solution code:
import numpy as np
N = int(input())

L_R = []
for i in range(N):
    L_R.append(list(map(int, input().split())))
L_R_sort = sorted(L_R, key=lambda k: k[0])
np_L_R_sort = np.array(L_R_sort)
# print(np_L_R_sort[:2])
# print(np_L_R_sort[:2].min(axis=0)[1])
max_point = 0
for i in range(1, N):
    point = 0

    L_max = np_L_R_sort[:i].max(axis=0)[0]
    R_min = np_L_R_sort[:i].min(axis=0)[1]
    point += max(R_min - L_max + 1, 0)
    L_max = np_L_R_sort[i:].max(axis=0)[0]
    R_min = np_L_R_sort[i:].min(axis=0)[1]
    point += max(R_min - L_max + 1, 0)
    if max_point > point:
        continue
    else:
        max_point = point
print(max_point)

Submission status: Runtime Error

Solution code:
N = int(input())

array = []

min_size = None
max_size = 0

for i in range(N):
    L, R = [int(v) for v in input().split()]
    size = R - L + 1

    if max_size < size:
        max_size = size
    if min_size is None or min_size > size:
        min_size = size

    array.append((L, R))


def score(bit):
    if bit == 0:
        return 0
    i = 0
    max_L = 0
    min_R = None
    while i < N and bit > 0:
        if (bit & 1) == 1:
            l, r = array[i]
            if max_L < l:
                max_L = l
            if min_R is None or min_R > r:
                min_R = r

        bit = (bit >> 1)
        i += 1
    return min_R - max_L + 1

def search(i, bit):
    if i == N:
        return score(bit) + score((1 << N) - 1 - bit)

    bit1 = bit + (1 << i)
    sc1 = search(i+1, bit)
    sc2 = search(i+1, bit1)
    return max(sc1, sc2)

best = search(1, 1)
print(best)

Submission status: Runtime Error

Solution code:
import copy

def get_p(l1,l2):
    l3 = [0,0]
    l3[0] = max(l1[0],l2[0])
    l3[1] = min(l1[1],l2[1])
    
    return l3
    
    

def add_a_b(a,b,lis,i,n):
    if i == n:
        global max_num
        num = a[1] - a[0] + b[1] - b[0] + 2
        max_num = max(max_num,num)
        return
    
    # aに追加
    pre_a = copy.deepcopy(a)
    if a == [0,0]:
        a[0] = lis[i][0]
        a[1] = lis[i][1]
    else:
        a = get_p(a,lis[i])
        
    if a[0] < a[1]:
        add_a_b(a,b,lis,i+1,n)
    a = pre_a
    
    # bに追加
    pre_b = copy.deepcopy(b)
    if b == [0,0]:
        b[0] = lis[i][0]
        b[1] = lis[i][1]
    else:
        b = get_p(b,lis[i])
        
    if b[0] < b[1]:
        add_a_b(a,b,lis,i+1,n)
    b = pre_b
    
n = int(input())
lis = [0 for i in range(n)]
for i in range(n):
    lis[i] = [int(x) for x in input().split()]
    
a = [0,0]
b = [0,0]

max_num = 0
i = 0
add_a_b(a,b,lis,i,n)
print(max_num)

Submission status: Runtime Error

Solution code:
def overlap(l):
    start = max(l[0][0],l[1][0])
    end = min(l[0][1],l[1][1])
    if start>end:
        return [0,-1]
    else:
        return [start,end]
 
def solve(Q):
    if len(Q)==2:
        fan = (Q[0][1]-Q[0][0]+1)+(Q[1][1]-Q[1][0]+1)
        contestant = Q
    else:
        new=Q[-1]
        fan_s, contestant_s = solve(Q[:-1])
        over = overlap(contestant_s)
        # separate
        fan_1 = (new[1]-new[0]+1)+(over[1]-over[0]+1)
        cont_1 = [new,over]
        #joint 1
        over2 = overlap([new, contestant_s[0]])
        fan_2 = (over2[1]-over2[0]+1)+(contestant_s[1][1]-contestant_s[1][0]+1)
        cont_2 = [over2,contestant_s[1]]
        #joint 2
        over3 = overlap([new, contestant_s[1]])
        fan_3 = (over3[1]-over3[0]+1)+(contestant_s[0][1]-contestant_s[0][0]+1)
        cont_3 = [over3,contestant_s[0]]
        if fan_1 > fan_2 and fan_1 > fan_3:
            fan = fan_1
            contestant = cont_1
        elif fan_2>fan_3:
            fan = fan_2
            contestant = cont_2
        else:
            fan = fan_3
            contestant = cont_3
    return fan, contestant
 
N = int(input())
Q=[]
for i in range(N):
    l, r = map(int, input().split())
    Q.append([l,r])
 
fan, contestant = solve(Q)
print(fan)

Submission status: Runtime Error

Solution code:
from operator import itemgetter
import sys
input = sys.stdin.readline
N = int(input())
LR = []
for _ in range(N):
    l, r = map(int, input().split())
    LR.append([l, r+1])
ans = 0
for l, r in LR:
    ans = max(ans, r - l)

LR.sort(key=itemgetter(1))

sch = 0
rr = 0
for l, r in LR:
    if l >= rr:
        rr = r
        sch += 1
#print(sch)
if sch >= 3:
    print(ans)
elif sch == 2:
    # ここは合ってる
    LR_ = []
    pl = 0
    for l, r in LR:
        if pl >= l:
            pass
        else:
            LR_.append([l, r])
        pl = l
    assert len(LR_) >= 2
    LR = LR_
    Ll, Lr = LR[0]
    Rl, Rr = LR[-1][0], LR[1][1]
    ans = max(ans, max(Rr-Rl, 0) + Lr-Ll)
    for i, (l, r) in enumerate(LR[1:-1], 1):
        Ll = l
        Rr = LR[i+1][1]
        ans = max(ans, max(Rr-Rl, 0) + max(Lr-Ll, 0))
    print(ans)
else:
    assert False
    LR_ = []
    pl = 0
    for l, r in LR:
        if pl >= l:
            pass
        else:
            LR_.append([l, r])
        pl = l
    if len(LR_) >= 2:
        LR, LR_ = LR_, LR
        Ll, Lr = LR[0]
        Rl, Rr = LR[-1][0], LR[1][1]
        ans = max(ans, max(Rr-Rl, 0) + Lr-Ll)
        for i, (l, r) in enumerate(LR[1:-1], 1):
            Ll = l
            Rr = LR[i+1][1]
            ans = max(ans, max(Rr-Rl, 0) + max(Lr-Ll, 0))
        LR, LR_ = LR_, LR

    LR.sort(key=itemgetter(0))
    Lr = float("inf")
    Ls = []
    for l, r in LR[:-1]:
        Ll = l
        Lr = min(Lr, r)
        Ls.append(Lr-Ll)
    Rs = []
    Rl, Rr = 0, float("inf")
    for l, r in LR[:0:-1]:
        Rl = max(Rl, l)
        Rr = min(Rr, r)
        Rs.append(Rr - Rl)
    Rs.reverse()
    for l, r in zip(Ls, Rs):
        assert l>0 and r>0
        ans = max(ans, l+r)

    print(ans)

Submission status: Runtime Error

Solution code:
# import sys
#
# sys.setrecursionlimit(10 ** 7)
#
# input = sys.stdin.readline
# rstrip()

# int(input())
# map(int, input().split())

# 2回のコンテストに問題を分ける、一回だけ出題
# すべての問題を解く参加者の人数の和を最大化

from time import perf_counter


def solve(n, e, time_counter):
    from operator import itemgetter
    e.sort(key=itemgetter(2, 0))
    # print(e)

    ans = 0

    min_ = set()
    for j in range(n):
        if e[j][2] == e[0][2]:
            min_.add(j)
        else:
            break

    max_ = set()
    for j in range(n - 1, -1, -1):
        if e[j][2] == e[-1][2]:
            max_.add(j)
        else:
            break

    for mx in max_:
        for mn in min_:
            if mx == mn:
                continue
            if perf_counter() - time_counter == 1.8:
                print(ans)
                exit()

            ll, lr, area_l = e[mx]  # 最大
            rl, rr, area_r = e[mn]  # 最小

            for i in range(n):
                if i in (mx, mn):
                    continue
                l, r, _ = e[i]

                sr = max(0, (rr - rl + 1))
                mr = max(0, (min(rr, r) - max(rl, l) + 1))
                diff_r = sr - mr

                sl = max(0, (lr - ll + 1))
                ml = max(0, (min(lr, r) - max(ll, l) + 1))
                diff_l = sl - ml

                if diff_r < diff_l:
                    rl = max(rl, l)
                    rr = min(rr, r)
                else:
                    ll = max(ll, l)
                    lr = min(lr, r)

            ans = max(ans, max(0, (lr - ll + 1)) + max(0, (rr - rl + 1)))
    print(ans)


if __name__ == '__main__':
    import sys

    time_counter = perf_counter()

    input = sys.stdin.readline
    n = int(input().rstrip())
    e = []
    for _ in range(n):
        lr = tuple(int(x) - 1 for x in input().rstrip().split())
        lr = lr + (lr[1] - lr[0] + 1,)
        e.append(lr)
    solve(n, e, time_counter)

Submission status: Runtime Error

Solution code:
N = int(input())

array = []

min_size = None
max_size = 0

for i in range(N):
    L, R = [int(v) for v in input().split()]
    size = R - L + 1

    if max_size < size:
        max_size = size
    if min_size is None or min_size > size:
        min_size = size

    array.append((L, R))


def score(bit):
    if bit == 0:
        return 0
    i = 0
    max_L = None
    min_R = None
    while i < N and bit > 0:
        if (bit & 1) == 1:
            l, r = array[i]
            if max_L is None or max_L < l:
                max_L = l
            if min_R is None or min_R > r:
                min_R = r

        bit = (bit >> 1)
        i += 1
    return min_R - max_L + 1

def search(i, bit):
    if i == N:
        return score(bit) + score((1 << N) - 1 - bit)

    bit1 = bit + (1 << i)
    i += 1
    sc1 = search(i, bit)
    sc2 = search(i, bit1)
    return max(sc1, sc2)

best = search(0, 0)
print(best)

Submission status: Runtime Error

Solution code:
from operator import itemgetter
import sys
input = sys.stdin.readline
N = int(input())
LR = []
for _ in range(N):
    l, r = map(int, input().split())
    LR.append([l, r+1])
ans = 0
for l, r in LR:
    ans = max(ans, r - l)

LR.sort(key=itemgetter(1))

sch = 0
rr = 0
for l, r in LR:
    if l >= rr:
        rr = r
        sch += 1
#print(sch)
if sch >= 3:
    print(ans)
elif sch == 2:
    # ここは合ってる
    LR_ = []
    pl = 0
    for l, r in LR:
        if pl >= l:
            pass
        else:
            LR_.append([l, r])
        pl = l
    assert len(LR_) >= 2
    LR = LR_
    Ll, Lr = LR[0]
    Rl, Rr = LR[-1][0], LR[1][1]
    ans = max(ans, max(Rr-Rl, 0) + Lr-Ll)
    for i, (l, r) in enumerate(LR[1:-1], 1):
        Ll = l
        Rr = LR[i+1][1]
        ans = max(ans, max(Rr-Rl, 0) + max(Lr-Ll, 0))
    print(ans)
else:
    assert False
    LR_ = []
    pl = 0
    for l, r in LR:
        if pl >= l:
            pass
        else:
            LR_.append([l, r])
        pl = l
    if len(LR_) >= 2:
        LR, LR_ = LR_, LR
        Ll, Lr = LR[0]
        Rl, Rr = LR[-1][0], LR[1][1]
        ans = max(ans, max(Rr-Rl, 0) + Lr-Ll)
        for i, (l, r) in enumerate(LR[1:-1], 1):
            Ll = l
            Rr = LR[i+1][1]
            ans = max(ans, max(Rr-Rl, 0) + max(Lr-Ll, 0))
        LR, LR_ = LR_, LR

    LR.sort(key=itemgetter(0))
    Lr = float("inf")
    Ls = []
    for l, r in LR[:-1]:
        Ll = l
        Lr = min(Lr, r)
        Ls.append(Lr-Ll)
    Rs = []
    Rl, Rr = 0, float("inf")
    for l, r in LR[:0:-1]:
        Rl = max(Rl, l)
        Rr = min(Rr, r)
        Rs.append(Rr - Rl)
    Rs.reverse()
    for l, r in zip(Ls, Rs):
        ans = max(ans, l+r)

    print(ans)

Submission status: Runtime Error

Solution code:
n=int(input())
lr=[]
for i in range(n):
	lr.append(list(map(int,input().split())))

dp=[[0,0,0,0] for k in range(n)]
dp[0]=[lr[0][0],lr[0][1],lr[1][0],lr[1][1]]
for i in range(n-2):
	a=-max(dp[i][0],lr[i+2][0])+min(dp[i][1],lr[i+2][1])+dp[i][3]-dp[i][2]+2
	b=-max(dp[i][2],lr[i+2][0])+min(dp[i][3],lr[i+2][1])+dp[i][1]-dp[i][0]+2
	if a>b:
		dp[i+1][0]=max(dp[i][0],lr[i+2][0])
		dp[i+1][1]=min(dp[i][1],lr[i+2][1])
		dp[i+1][2]=dp[i][2]
		dp[i+1][3]=dp[i][3]
	else:
		dp[i+1][0]=dp[i][0]
		dp[i+1][1]=dp[i][1]
		dp[i+1][2]=max(dp[i][2],lr[i+2][0])
		dp[i+1][3]=min(dp[i][3],lr[i+2][1])
tanoshisa=-dp[n-2][0]+dp[n-2][1]-dp[n-2][2]+dp[n-2][3]+2

lr.append(lr.pop[0])
dp2=[[0,0,0,0] for k in range(n)]
dp2[0]=[lr[0][0],lr[0][1],lr[1][0],lr[1][1]]
for i in range(n-2):
	a=-max(dp2[i][0],lr[i+2][0])+min(dp2[i][1],lr[i+2][1])+dp2[i][3]-dp2[i][2]+2
	b=-max(dp2[i][2],lr[i+2][0])+min(dp2[i][3],lr[i+2][1])+dp2[i][1]-dp2[i][0]+2
	if a>b:
		dp2[i+1][0]=max(dp2[i][0],lr[i+2][0])
		dp2[i+1][1]=min(dp2[i][1],lr[i+2][1])
		dp2[i+1][2]=dp2[i][2]
		dp2[i+1][3]=dp2[i][3]
	else:
		dp2[i+1][0]=dp2[i][0]
		dp2[i+1][1]=dp2[i][1]
		dp2[i+1][2]=max(dp2[i][2],lr[i+2][0])
		dp2[i+1][3]=min(dp2[i][3],lr[i+2][1])
tanoshisa2=-dp2[n-2][0]+dp2[n-2][1]-dp2[n-2][2]+dp2[n-2][3]+2


print(max(tanoshisa,tanoshisa2))

Submission status: Runtime Error

Solution code:
import bisect, collections, heapq, sys
input = sys.stdin.readline

n = int(input())
problem_list = []
r = 10**9+1
p_list = []
for i in range(n):
    a, b = [ int(v) for v in input().split() ]
    p_list.append(a)
    p_list.append(b)
    problem_list.append([a,b])
problem_list.sort()
p_list.sort()
problem_list = [ ( bisect.bisect_left(p_list,i[0]), bisect.bisect_left(p_list,i[1]) )  for i in problem_list ]

left_list = [ 0 for i in range(2*n) ]
right_list = [ 0 for i in range(2*n) ]
for a, b in problem_list:
    left_list[a] += 1
    right_list[b] += 1

ans = 0
la = problem_list[-1][0]
ra = None
lenr = len(right_list) 
for i in range(lenr):
    if right_list[i] > 0:
        ra = i
lb, rb = -1, 10**6+1

for a, b in problem_list[:-1]:
    
    left_list[a] -= 1
    right_list[b] -= 1
    lb = max(lb,a)
    rb = min(rb, b)

    if left_list[la] == 0:
        for i in range(1,2**n):
            if left_list[la+i] != 0:
                la += i
                break
    if right_list[ra] == 0:
        for i in range(1,2**n):
            if right_list[ra+i] != 0:
                ra += i
                break

    ans = max(ans,(max(p_list[rb]-p_list[lb]+1,0)+max(p_list[ra]-p_list[la]+1,0)))

print(ans)

Submission status: Runtime Error

Solution code:
import sys

N = int(input())
inp = []
for k in range(N):
    L, R = map(int, sys.stdin.readline().split())
    inp.append([L, R])

A = inp[0]
B = None
now = float("inf")
now_len = None
for k in range(1, N):
    mx = max(inp[k][0], A[0])
    mn = min(inp[k][1], A[1])
    if mx <= mn:
        leng = mn - mx + 1
    else:
        leng = 0
    if now > leng:
        now = leng
        now_len = k

B = inp[now_len]

for k in range(1, N):
    if k == now_len:
        continue
    mxa = max(inp[k][0], A[0])
    mna = min(inp[k][1], A[1])
    mxb = max(inp[k][0], B[0])
    mnb = min(inp[k][1], B[1])

    if (A[1] - A[0] + 1) - (mna - mxa + 1) > (B[1] - B[0] + 1) - (mnb - mxb + 1):
        B = [mxb, mnb]
    else:
        A = [mxa, mna]

result = (A[1] - A[0] + 1) + (B[1] - B[0] + 1)

for i in inp:
    if result < i[1] - i[0] + 1:
        result = i

print(result)

Submission status: Runtime Error

Solution code:
N = int(input())
lr = []
L, R = [], []
for _ in range(N):
    l, r = map(int, input().split())
    lr.append((l, r))
    L.append(l)
    R.append(r)
lr.sort()
L.sort()
R.sort()
ans = 0
for l, r in lr:  
    m = L[-1]
    if L[-1] == l:
        m = L[-2]
    M = R[0]
    if R[0] == r:
        M = R[1]
    ans = max(ans, M-m+1+r-l+1)
tmp = [lr[0]]
for i in range(1, N):
    if lr[i][0] == lr[i][1]:
        continue
    else:
        tmp.append(lr[i])
lr = tmp
R = lr[-1][1]
tmp = [lr[-1]]
for l, r in lr[-2::-1]:
    if R<=r:
        continue
    tmp.append((l, r))
    R = r
lr = tmp[::-1]
ans1 = [0]
ans2 = [0]
R = lr[0][1]
for l, r in lr:
    if l>R:
        ans1.append(0)
    else:
        ans1.append(R-l+1)
L = lr[-1][0]
for l, r in lr1[::-1]:
    if L>r:
        ans2.append(0)
    else:
        ans2.append(r-L+1)
for a, b in zip(ans1, ans2[::-1]):
    ans = max(ans, a+b)
print(ans)

Submission status: Runtime Error

Solution code:
from operator import itemgetter
import sys
input = sys.stdin.readline
N = int(input())
LR = []
for _ in range(N):
    l, r = map(int, input().split())
    LR.append([l, r+1])
ans = 0
for l, r in LR:
    ans = max(ans, r - l)

LR.sort(key=itemgetter(1))

sch = 0
rr = 0
for l, r in LR:
    if l >= rr:
        rr = r
        sch += 1
#print(sch)
if sch >= 3:
    print(ans)
elif sch == 2:
    assert False
    LR_ = []
    pl = 0
    for l, r in LR:
        if pl >= l:
            pass
        else:
            LR_.append([l, r])
        pl = l
    assert len(LR_) >= 2
    LR = LR_
    Ll, Lr = LR[0]
    Rl, Rr = LR[-1][0], LR[1][1]
    ans = max(ans, max(Rr-Rl, 0) + Lr-Ll)
    for i, (l, r) in enumerate(LR[1:-1], 1):
        Ll = l
        Rr = LR[i+1][1]
        ans = max(ans, max(Rr-Rl, 0) + max(Lr-Ll, 0))
    print(ans)
else:
    mid_l = 0
    mid_r = float("inf")
    LR.sort(key=itemgetter(0))
    Lr = float("inf")
    Ls = []
    for l, r in LR[:-1]:
        Ll = l
        Lr = min(Lr, r)
        Ls.append(Lr-Ll)
    Rs = []
    Rl, Rr = 0, float("inf")
    for l, r in LR[:0:-1]:
        Rl = max(Rl, l)
        Rr = min(Rr, r)
        Rs.append(Rr - Rl)
    Rs.reverse()
    for l, r in zip(Ls, Rs):
        ans = max(ans, l+r)
    print(ans)