Problem: 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.

Submission attempts (in order):


 一番長いLRが複数ある場合どうする？

N = int(input())
LR = [list(map(int, input().split())) for _ in range(N)][::-1]
LR.sort(key=lambda x: x[1]-x[0], reverse=True)
ma = LR[0][1] - LR[0][0] + 1
left_ma = LR[0][0]
right_ma = LR[0][1]

ma2 = ma
another = 10 ** 9
another_length = 0 
LR.pop(0)

another_length = LR[0][1] - LR[0][0] + 1
left_an = LR[0][0]
right_an = LR[0][1]

point = ma + another_length
LR.pop(0)
for l, r in LR:
    if max(0, min(right_ma, r) - max(left_ma, l)) + another_length < ma + max(0, min(right_an, r) - max(left_an, l)):
        
        another_length = max(0, min(right_an, r) - max(left_an, l) + 1)
        point = ma + another_length
        right_an = min(r, right_an)
        left_an = max(l, left_an)
        
    else:
        ma = max(0, min(right_ma, r) - max(left_ma, l) + 1)
        point = ma + another_length
        right_ma = min(r, right_ma)
        left_ma = max(l, left_ma)

print(max(ma2, point))




 一番長いLRが複数ある場合どうする？

N = int(input())
LR = [list(map(int, input().split())) for _ in range(N)]
ma = 0
for i, (l, r) in enumerate(LR):
    if r-l > ma:
        ma = r-l+1
        i_ma = i
        left_ma = l
        right_ma = r
another = 10 ** 9
another_length = 0 
LR.pop(i_ma)
for i, (l, r) in enumerate(LR):
    if min(right_ma, r) - max(left_ma, l) < another:
        if r-l > another_length:
            another = min(right_ma, r) - max(left_ma, l)
            another_length = r-l+1
            i_an = i
            left_an = l
            right_an = r
point = ma + another_length
LR.pop(i_an)
for l, r in LR:
    if max(0, min(right_ma, r) - max(left_ma, l)) + another_length <= ma + max(0, min(right_an, r) - max(left_an, l)):
        
        another_length = max(0, min(right_an, r) - max(left_an, l) + 1)
        point = ma + another_length
        right_an = min(r, right_an)
        left_an = max(l, left_an)
        
    else:
        ma = max(0, min(right_ma, r) - max(left_ma, l) + 1)
        point = ma + another_length
        right_ma = min(r, right_ma)
        left_ma = max(l, left_ma)

print(point)




 一番長いLRが複数ある場合どうする？

N = int(input())
LR = [list(map(int, input().split())) for _ in range(N)][::-1]
LR.sort(key=lambda x: x[1]-x[0], reverse=True)
ma = LR[0][1] - LR[0][0] + 1
left_ma = LR[0][0]
right_ma = LR[0][1]

ma2 = ma
another = 10 ** 9
another_length = 0 
LR.pop(0)
for i, (l, r) in enumerate(LR):
    if min(right_ma, r) - max(left_ma, l) < another:
        if r-l > another_length:
            another = min(right_ma, r) - max(left_ma, l)
            another_length = r-l+1
            i_an = i
            left_an = l
            right_an = r
point = ma + another_length
LR.pop(i_an)
for l, r in LR:
    if max(0, min(right_ma, r) - max(left_ma, l)) + another_length < ma + max(0, min(right_an, r) - max(left_an, l)):
        
        another_length = max(0, min(right_an, r) - max(left_an, l) + 1)
        point = ma + another_length
        right_an = min(r, right_an)
        left_an = max(l, left_an)
        
    else:
        ma = max(0, min(right_ma, r) - max(left_ma, l) + 1)
        point = ma + another_length
        right_ma = min(r, right_ma)
        left_ma = max(l, left_ma)

print(max(ma2, point))




 一番長いLRが複数ある場合どうする？

N = int(input())
LR = [list(map(int, input().split())) for _ in range(N)]
ma = 0
for i, (l, r) in enumerate(LR):
    if r-l > ma:
        ma = r-l+1
        i_ma = i
        left_ma = l
        right_ma = r
ma2 = ma
another = 10 ** 9
another_length = 0 
LR.pop(i_ma)
for i, (l, r) in enumerate(LR):
    if min(right_ma, r) - max(left_ma, l) < another:
        if r-l > another_length:
            another = min(right_ma, r) - max(left_ma, l)
            another_length = r-l+1
            i_an = i
            left_an = l
            right_an = r
point = ma + another_length
LR.pop(i_an)
for l, r in LR:
    if max(0, min(right_ma, r) - max(left_ma, l)) + another_length < ma + max(0, min(right_an, r) - max(left_an, l)):
        
        another_length = max(0, min(right_an, r) - max(left_an, l) + 1)
        point = ma + another_length
        right_an = min(r, right_an)
        left_an = max(l, left_an)
        
    else:
        ma = max(0, min(right_ma, r) - max(left_ma, l) + 1)
        point = ma + another_length
        right_ma = min(r, right_ma)
        left_ma = max(l, left_ma)

print(max(ma2, point))




 一番長いLRが複数ある場合どうする？

N = int(input())
LR = [list(map(int, input().split())) for _ in range(N)][::-1]
LR.sort(key=lambda x: x[1]-x[0], reverse=True)
ma = LR[0][1] - LR[0][0] + 1
left_ma = LR[0][0]
right_ma = LR[0][1]

ma2 = ma
another = 10 ** 9
another_length = 0 
LR.pop(0)

another_length = LR[1][1] - LR[1][0] + 1
left_an = LR[1][0]
right_an = LR[1][1]

point = ma + another_length
LR.pop(1)
for l, r in LR:
    if max(0, min(right_ma, r) - max(left_ma, l)) + another_length < ma + max(0, min(right_an, r) - max(left_an, l)):
        
        another_length = max(0, min(right_an, r) - max(left_an, l) + 1)
        point = ma + another_length
        right_an = min(r, right_an)
        left_an = max(l, left_an)
        
    else:
        ma = max(0, min(right_ma, r) - max(left_ma, l) + 1)
        point = ma + another_length
        right_ma = min(r, right_ma)
        left_ma = max(l, left_ma)

print(max(ma2, point))




 一番長いLRが複数ある場合どうする？

N = int(input())
LR = [list(map(int, input().split())) for _ in range(N)]
LR.sort(key=lambda x: x[1]-x[0], reverse=True)
ma = LR[0][1] - LR[0][0] + 1
left_ma = LR[0][0]
right_ma = LR[0][1]

ma2 = ma
another = 10 ** 9
another_length = 0 
LR.pop(0)
for i, (l, r) in enumerate(LR):
    if min(right_ma, r) - max(left_ma, l) < another:
        if r-l > another_length:
            another = min(right_ma, r) - max(left_ma, l)
            another_length = r-l+1
            i_an = i
            left_an = l
            right_an = r
point = ma + another_length
LR.pop(i_an)
for l, r in LR:
    if max(0, min(right_ma, r) - max(left_ma, l)) + another_length <= ma + max(0, min(right_an, r) - max(left_an, l)):
        
        another_length = max(0, min(right_an, r) - max(left_an, l) + 1)
        point = ma + another_length
        right_an = min(r, right_an)
        left_an = max(l, left_an)
        
    else:
        ma = max(0, min(right_ma, r) - max(left_ma, l) + 1)
        point = ma + another_length
        right_ma = min(r, right_ma)
        left_ma = max(l, left_ma)

print(max(ma2, point))




 一番長いLRが複数ある場合どうする？

N = int(input())
LR = [list(map(int, input().split())) for _ in range(N)]
LR.sort(key=lambda x: x[1]-x[0], reverse=True)
ma = LR[0][1] - LR[0][0] + 1
left_ma = LR[0][0]
right_ma = LR[0][1]

ma2 = ma
another = 10 ** 9
another_length = 0 
LR.pop(0)
for i, (l, r) in enumerate(LR):
    if min(right_ma, r) - max(left_ma, l) < another:
        if r-l > another_length:
            another = min(right_ma, r) - max(left_ma, l)
            another_length = r-l+1
            i_an = i
            left_an = l
            right_an = r
point = ma + another_length
LR.pop(i_an)
for l, r in LR:
    if max(0, min(right_ma, r) - max(left_ma, l)) + another_length < ma + max(0, min(right_an, r) - max(left_an, l)):
        
        another_length = max(0, min(right_an, r) - max(left_an, l) + 1)
        point = ma + another_length
        right_an = min(r, right_an)
        left_an = max(l, left_an)
        
    else:
        ma = max(0, min(right_ma, r) - max(left_ma, l) + 1)
        point = ma + another_length
        right_ma = min(r, right_ma)
        left_ma = max(l, left_ma)

print(max(ma2, point))




 一番長いLRが複数ある場合どうする？

N = int(input())
LR = [list(map(int, input().split())) for _ in range(N)][::-1]
LR.sort(key=lambda x: x[1]-x[0], reverse=True)
ma = LR[0][1] - LR[0][0] + 1
left_ma = LR[0][0]
right_ma = LR[0][1]

ma2 = ma
another = 10 ** 9
another_length = 0 
LR.pop(0)
for i, (l, r) in enumerate(LR):
    if min(right_ma, r) - max(left_ma, l) < another:
        if r-l > another_length:
            another = min(right_ma, r) - max(left_ma, l)
            another_length = r-l+1
            i_an = i
            left_an = l
            right_an = r
point = ma + another_length
LR.pop(i_an)
for l, r in LR:
    if max(0, min(right_ma, r) - max(left_ma, l)) + another_length <= ma + max(0, min(right_an, r) - max(left_an, l)):
        
        another_length = max(0, min(right_an, r) - max(left_an, l) + 1)
        point = ma + another_length
        right_an = min(r, right_an)
        left_an = max(l, left_an)
        
    else:
        ma = max(0, min(right_ma, r) - max(left_ma, l) + 1)
        point = ma + another_length
        right_ma = min(r, right_ma)
        left_ma = max(l, left_ma)

print(max(ma2, point))




 一番長いLRが複数ある場合どうする？

N = int(input())
LR = [list(map(int, input().split())) for _ in range(N)]
ma = 0
for i, (l, r) in enumerate(LR):
    if r-l > ma:
        ma = r-l+1
        i_ma = i
        left_ma = l
        right_ma = r
another = 10 ** 9
another_length = 0 
LR.pop(i_ma)
for i, (l, r) in enumerate(LR):
    if min(right_ma, r) - max(left_ma, l) < another:
        if r-l > another_length:
            another = min(right_ma, r) - max(left_ma, l)
            another_length = r-l+1
            i_an = i
            left_an = l
            right_an = r
point = ma + another_length
LR.pop(i_an)
for l, r in LR:
    if max(0, min(right_ma, r) - max(left_ma, l)) + another_length < ma + max(0, min(right_an, r) - max(left_an, l)):
        
        another_length = max(0, min(right_an, r) - max(left_an, l) + 1)
        point = ma + another_length
        right_an = min(r, right_an)
        left_an = max(l, left_an)
        
    else:
        ma = max(0, min(right_ma, r) - max(left_ma, l) + 1)
        point = ma + another_length
        right_ma = min(r, right_ma)
        left_ma = max(l, left_ma)

print(point)


import sys

stdin = sys.stdin

ri = lambda: int(rs())
rl = lambda: list(map(int, stdin.readline().split()))  # applies to only numbers
rs = lambda: stdin.readline().rstrip()  

N = ri()
L = []
R = []
for _ in range(N):
    l, r = rl()
    L.append(l)
    R.append(r)

max_l = max(L)
min_R = min(R)
ab = [(max(0, r - max_l + 1), max(0, min_R - l + 1)) for l, r in zip(L, R)]
ab.sort()
answer = max(r - l + 1 for l, r in zip(L, R)) + max(0, min_R - max_l + 1)
mi = float('inf')

for i in range(N-1):
    mi = min(mi, ab[i][1])
    answer = max(answer, mi + ab[i+1][0])

print(answer)


Verdicts: Wrong Answer

Wrong Answer

Wrong Answer

Wrong Answer

Runtime Error

Wrong Answer

Wrong Answer

Wrong Answer

Wrong Answer

Accepted