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: Wrong Answer

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],x[0]))
    box0 = [lr[0][0], lr[0][1]]
    box1 = [lr[1][0], lr[1][1]]
    m=max(box0[1]-box0[0]+1,0)+max(box1[1]-box1[0]+1,0)
    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]
    ans=m
    lr.sort(key=lambda x: (x[0], x[1]))
    min_r = [0] * n
    min_r[0] = lr[0][1]
    min_l = [0] * n
    min_l[n-1] = lr[n-1][1]
    for i in range(1,n):
        min_r[i] = min(min_r[i - 1], lr[i][1])
    for i in range(n-2,-1,-1):
        min_l[i] = min(min_l[i + 1], lr[i][1])
    ans=max([max(min_r[i] - lr[i][0]+1,0) + max(min_l[i + 1] - lr[n - 1][0]+1,0) for i in range(n - 1)])
    for i in range(n):
        ans=max(lr[i][1]-lr[i][0]+1,ans)
    print(ans)
main()

Submission status: Wrong Answer

Solution code:
import sys

input = sys.stdin.readline

ri = lambda: int(input())
rs = lambda: input().rstrip()
ril = lambda: list(map(int, input().split()))
rsl = lambda: input().rstrip().split()
ris = lambda n: [ri() for _ in range(n)]
rss = lambda n: [rs() for _ in range(n)]
rils = lambda n: [ril() for _ in range(n)]
rsls = lambda n: [rsl() for _ in range(n)]

n = ri()
ls = rils(n)
res = max(r - l + 1 for l, r in ls)

ls.sort(key=lambda x: x[1])
lmax0 = [ls[0][0]]
rmin0 = [ls[0][1]]
for i in range(1, n - 1):
    lmax0.append(max(lmax0[-1], ls[i][0]))
    rmin0.append(min(rmin0[-1], ls[i][1]))

lmax1 = [ls[-1][0]]
rmin1 = [ls[-1][1]]
for i in range(1, n - 1):
    lmax1.append(max(lmax1[-1], ls[n - i - 1][0]))
    rmin1.append(min(rmin1[-1], ls[n - i - 1][1]))

s0 = [max(0, r - l + 1) for l, r in zip(lmax0, rmin0)]
s1 = [max(0, r - l + 1) for l, r in zip(lmax1, rmin1)]
for x, y in zip(s0, reversed(s1)):
    res = max(res, x + y)

ls.sort(key=lambda x: x[0])
lmax0 = [ls[0][0]]
rmin0 = [ls[0][1]]
for i in range(1, n - 1):
    lmax0.append(max(lmax0[-1], ls[i][0]))
    rmin0.append(min(rmin0[-1], ls[i][1]))

lmax1 = [ls[-1][0]]
rmin1 = [ls[-1][1]]
for i in range(1, n - 1):
    lmax1.append(max(lmax1[-1], ls[n - i - 1][0]))
    rmin1.append(min(rmin1[-1], ls[n - i - 1][1]))

s0 = [max(0, r - l + 1) for l, r in zip(lmax0, rmin0)]
s1 = [max(0, r - l + 1) for l, r in zip(lmax1, rmin1)]
for x, y in zip(s0, reversed(s1)):
    res = max(res, x + y)

ls.sort(key=lambda x: -x[1])
lmax0 = [ls[0][0]]
rmin0 = [ls[0][1]]
for i in range(1, n - 1):
    lmax0.append(max(lmax0[-1], ls[i][0]))
    rmin0.append(min(rmin0[-1], ls[i][1]))

lmax1 = [ls[-1][0]]
rmin1 = [ls[-1][1]]
for i in range(1, n - 1):
    lmax1.append(max(lmax1[-1], ls[n - i - 1][0]))
    rmin1.append(min(rmin1[-1], ls[n - i - 1][1]))

s0 = [max(0, r - l + 1) for l, r in zip(lmax0, rmin0)]
s1 = [max(0, r - l + 1) for l, r in zip(lmax1, rmin1)]
for x, y in zip(s0, reversed(s1)):
    res = max(res, x + y)

ls.sort(key=lambda x: -x[0])
lmax0 = [ls[0][0]]
rmin0 = [ls[0][1]]
for i in range(1, n - 1):
    lmax0.append(max(lmax0[-1], ls[i][0]))
    rmin0.append(min(rmin0[-1], ls[i][1]))

lmax1 = [ls[-1][0]]
rmin1 = [ls[-1][1]]
for i in range(1, n - 1):
    lmax1.append(max(lmax1[-1], ls[n - i - 1][0]))
    rmin1.append(min(rmin1[-1], ls[n - i - 1][1]))

s0 = [max(0, r - l + 1) for l, r in zip(lmax0, rmin0)]
s1 = [max(0, r - l + 1) for l, r in zip(lmax1, rmin1)]
for x, y in zip(s0, reversed(s1)):
    res = max(res, x + y)

print(res)

Submission status: Wrong Answer

Solution code:
import sys

def solve(N: int, L: "List[int]", R: "List[int]"):
    LR= list(zip(L,R))
    LR.sort(key=lambda x:x[0])

    answer = 0
    for i in range(1,N):
        a = 0
        mi = 0
        ma = 10**9
        for lr in LR[:i]:
            mi = max(lr[0],mi)
            ma = min(lr[1],ma)
        a += ma-mi+1
        
        mi = 0
        ma = 10**9
        for lr in LR[i:]:
            mi = max(lr[0],mi)
            ma = min(lr[1],ma)
        a += ma-mi+1

        answer = max(a,answer)
    print(answer)
    return


def main():
    def iterate_tokens():
        for line in sys.stdin:
            for word in line.split():
                yield word
    tokens = iterate_tokens()
    N = int(next(tokens))  # type: int
    L = [int()] * (N)  # type: "List[int]"
    R = [int()] * (N)  # type: "List[int]"
    for i in range(N):
        L[i] = int(next(tokens))
        R[i] = int(next(tokens))
    solve(N, L, R)

if __name__ == '__main__':
    main()

Submission status: Wrong Answer

Solution code:
# 一番長いLRが最低の楽しさ
# 一番長いLRと重なるのが一番少ないものはもう一つにする
# 一番長いLRが最低の楽しさ 一番長いLRが複数ある場合どうする？
# 一番長い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のグループにいれる
        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)
        #print(right_an, 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))

Submission status: Wrong Answer

Solution code:
import sys
read = sys.stdin.buffer.read
readline = sys.stdin.buffer.readline
readlines = sys.stdin.buffer.readlines

N = int(readline())
m = map(int,read().split())
LR = list(zip(m,m))

# 片方に集める場合
max_width = max(R-L+1 for L,R in LR)

LR.sort()

INF=10**18
# Rが最小のもの
i = -1
opt_R = INF
for n,(L,R) in enumerate(LR):
    if opt_R>=R:
        opt_R=R
        i=n
j = -1
opt_L = -INF
for n,(L,R) in enumerate(LR):
    if opt_L<=L:
        opt_L=L
        j=n

if i==j:
    L,R = LR[i]
    answer = max_width + (R-L+1)
    print(answer)
    exit()

L1,R1 = LR[i]; L2,R2 = LR[j]

LR = [(L,R) for n,(L,R) in enumerate(LR) if n != i and n != j]

LR.sort()

n = len(LR)
dpl = [(-INF,INF)]*(n+1)
dpr = [(-INF,INF)]*(n+1)

for i in range(n):
    a,b = dpl[i]
    L,R = LR[i]
    dpl[i+1] = (max(a,L),min(b,R))
for i in range(n,0,-1):
    a,b = dpr[i]
    L,R = LR[i-1]
    dpr[i-1] = (max(a,L),min(b,R))

answer = 0
for (a,b),(c,d) in zip(dpl,dpr):
    l1 = max(L1,a); r1 = min(R1,b)
    l2 = max(L2,c); r2 = min(R2,d)
    x = max(0,r1-l1+1) + max(0,r2-l2+1)
    if answer < x:
        answer = x

if answer < max_width:
    answer = max_width

print(answer)

Submission status: Wrong Answer

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

LR.sort()
LR.sort(key=lambda x: x[1] - x[0], reverse=True)

i1, i2, ans1 = 0, 1, LR[0][1] - LR[0][0]
while i2 + 1 < N and LR[i2 + 1][1] - LR[i2 + 1][0] == ans1:
    i2 += 1
ans2 = LR[i2][1] - LR[i2][0]
L1, R1, L2, R2 = LR[i1][0], LR[i1][1], LR[i2][0], LR[i2][1]

for i in range(N - 1, 0, -1):
    if not i == i2:
        diff1 = max(R1 - L1 + 1, 0) - \
            max(min(LR[i][1], R1) - max(LR[i][0], L1) + 1, 0)
        diff2 = max(R2 - L2 + 1, 0) - \
            max(min(LR[i][1], R2) - max(LR[i][0], L2) + 1, 0)
        if diff1 < diff2:
            L1, R1 = max(LR[i][0], L1), min(LR[i][1], R1)
        else:
            L2, R2 = max(LR[i][0], L2), min(LR[i][1], R2)

print(max(R1 - L1 + 1, 0) + max(R2 - L2 + 1, 0))

Submission status: Wrong Answer

Solution code:
N = int(input())
LR = [[]]*N

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


LR.sort(key=lambda x: x[1] - x[0], reverse=True)

max_n = LR[0][1]
min_n = LR[0][0]
set2 = [LR[0][0], LR[0][1]]
set1 = [LR[1][0], LR[1][1]]
LR = sorted(LR[2:], key=lambda x: x[1]-x[0])

for i in range(N-2):
    if max((min(set1[1], LR[i][1]) - max(set1[0], LR[i][0]))+1, 0)+(set2[1]-set2[0]+1) >= max((min(set2[1], LR[i][1]) - max(set2[0], LR[i][0])+1), 0) + (set1[1]-set1[0]+1):
        set1 = [max(set1[0], LR[i][0]), min(set1[1], LR[i][1])]
    else:
        set2 = [max(set2[0], LR[i][0]), min(set2[1], LR[i][1])]

ans1 = max(set1[1] - set1[0]+1, 0) + max(set2[1]-set2[0]+1, 0)


print(max(ans1, max_n - min_n + 1))

Submission status: Wrong Answer

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(0,1-max(dp[i][0],lr[i+2][0])+min(dp[i][1],lr[i+2][1]))+max(0,1+dp[i][3]-dp[i][2])+2
	b=max(0,1-max(dp[i][2],lr[i+2][0])+min(dp[i][3],lr[i+2][1]))+max(0,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=max(0,1-dp[n-2][0]+dp[n-2][1])+max(0,1-dp[n-2][2]+dp[n-2][3])+2

hikidashi=lr[0]
del lr[0]
lr.append(hikidashi)

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(0,1-max(dp2[i][0],lr[i+2][0])+min(dp2[i][1],lr[i+2][1]))+max(0,1+dp2[i][3]-dp2[i][2])+2
	b=max(0,1-max(dp2[i][2],lr[i+2][0])+min(dp2[i][3],lr[i+2][1]))+max(0,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=max(0,-dp2[n-2][0]+dp2[n-2][1]+1)+max(0,-dp2[n-2][2]+dp2[n-2][3]+1)


print(max(tanoshisa,tanoshisa2))

Submission status: Wrong Answer

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

ls.sort(key = lambda x:x[1], reverse=True)
ls.sort(key = lambda x:x[0])

INF = 1001001001001001001
min_ls1 = [INF] * n
min_ls1[0] = ls[0][1]
for i in range(1, n):
    min_ls1[i] = min(min_ls1[i-1], ls[i][1])

min_ls2 = [INF] * n
min_ls2[-1] = ls[-1][1]
for i in reversed(range(n-1)):
    min_ls2[i] = min(min_ls2[i+1], ls[i][1])

ans_ls = [0] * (n-1)

for i in range(n-1):
    p1 = ls[i][0]
    p2 = min_ls1[i]
    p3 = ls[-1][0]
    p4 = min_ls2[i+1]
    ans_ls[i] = max(p2 - p1 + 1, 0) + max(p4 - p3 + 1, 0)

ans1 = max(ans_ls)

ls.sort(key = lambda x:x[0], reverse=True)
ls.sort(key = lambda x:x[1])

max_ls1 = [INF] * n
max_ls1[0] = ls[0][0]
for i in range(1, n):
    max_ls1[i] = max(max_ls1[i-1], ls[i][0])

max_ls2 = [INF] * n
max_ls2[-1] = ls[-1][0]
for i in reversed(range(n-1)):
    max_ls2[i] = max(max_ls2[i+1], ls[i][0])

ans_ls = [0] * (n-1)

for i in range(n-1):
    p1 = max_ls1[i]
    p2 = ls[0][1]
    p3 = max_ls2[i+1]
    p4 = ls[i+1][1]
    ans_ls[i] = max(p2 - p1 + 1, 0) + max(p4 - p3 + 1, 0)

ans2 = max(ans_ls)

print(max(ans1, ans2))

Submission status: Wrong Answer

Solution code:
def search_width(array_a, array_b):
    l, r = 0, 0
    if array_a[0] < array_b[0]:
        l = array_b[0]
    else:
        l = array_a[0]
    
    if array_a[1] < array_b[1]:
        r = array_a[1]
    else:
        r = array_b[1]
    return l,r
  

n = int(input())

array = []
for _ in range(n):
    a, b = map(int, input().split())
    l = [a, b]
    array.append(l)

first = array[0]

min = 10 ** 10
min_i = 1

for i in range(1, len(array)):
    l,r = search_width(first, array[i])
    if min > r - l:
        min = r - l
        min_i = i
        
second = array[min_i]

min_f = first[1] - first[0] + 1
min_s = second[1] - second[0] + 1
for i in range(1, len(array)):
    l,r = search_width(first, array[i])
    min_a = r - l + 1
    l,r = search_width(second, array[i])
    min_b = r - l + 1
    
    min_fa = min_f - min_a
    min_sb = min_s - min_b
    if min_fa > min_sb:
        if min_b < min_s:
            min_s = min_b
    else:
        if min_a < min_f:
            min_f = min_a

ans = min_f + min_s
print(ans)

Submission status: Wrong Answer

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

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

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

from time import time


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

    ans = 0

    ll, lr, area_l = e[-1]  # 最大
    rl, rr, area_r = -1, -1, -1  # 最小

    l, r, _ = e[0]
    ll = max(ll, l)
    lr = min(lr, r)

    for i in range(1, n - 1):
        l, r, _ = e[i]
        if rl == -1:
            rl = l
            rr = r
        else:
            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)))

    ll, lr, area_l = e[-1]  # 最大
    rl, rr, area_r = e[0]  # 最小

    for i in range(1, n - 1):
        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 = time()

    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: Wrong Answer

Solution code:
import sys
read = sys.stdin.buffer.read
readline = sys.stdin.buffer.readline
readlines = sys.stdin.buffer.readlines

N = int(readline())
m = map(int,read().split())
LR = list(zip(m,m))

# 片方に集める場合
max_width = max(R-L+1 for L,R in LR)

LR.sort()

INF=10**18
# Rが最小のもの
i = -1
opt_R = INF
for n,(L,R) in enumerate(LR):
    if opt_R>R:
        opt_R=R
        i=n
j = -1
opt_L = -INF
for n,(L,R) in enumerate(LR):
    if opt_L<=L:
        opt_L=L
        j=n

if i==j:
    L,R = LR[i]
    answer = max_width + (R-L+1)
    print(answer)
    exit()

L1,R1 = LR[i]; L2,R2 = LR[j]

LR = [(L,R) for n,(L,R) in enumerate(LR) if n != i and n != j]

LR.sort()

n = len(LR)
dpl = [(-INF,INF)]*(n+1)
dpr = [(-INF,INF)]*(n+1)

for i in range(n):
    a,b = dpl[i]
    L,R = LR[i]
    dpl[i+1] = (max(a,L),min(b,R))
for i in range(n,0,-1):
    a,b = dpr[i]
    L,R = LR[i-1]
    dpr[i-1] = (max(a,L),min(b,R))

answer = 0
for (a,b),(c,d) in zip(dpl,dpr):
    l1 = max(L1,a); r1 = min(R1,b)
    l2 = max(L2,c); r2 = min(R2,d)
    x = max(0,r1-l1+1) + max(0,r2-l2+1)
    if answer < x:
        answer = x

if answer < max_width:
    answer = max_width

print(answer)

Submission status: Wrong Answer

Solution code:
import sys
read = sys.stdin.buffer.read
readline = sys.stdin.buffer.readline
readlines = sys.stdin.buffer.readlines

N = int(readline())
m = map(int,read().split())
LR = list(zip(m,m))

# 片方に集める場合
max_width = max(R-L+1 for L,R in LR)

LR.sort()

INF=10**18
# Rが最小のもの
i = -1
opt_R = INF
for n,(L,R) in enumerate(LR):
    if opt_R>=R:
        opt_R=R
        i=n
j = -1
opt_L = -INF
for n,(L,R) in enumerate(LR):
    if opt_L<L:
        opt_L=L
        j=n

if i==j:
    L,R = LR[i]
    answer = max_width + (R-L+1)
    print(answer)
    exit()

L1,R1 = LR[i]; L2,R2 = LR[j]

LR = [(L,R) for n,(L,R) in enumerate(LR) if n != i and n != j]

LR.sort()

n = len(LR)
dpl = [(-INF,INF)]*(n+1)
dpr = [(-INF,INF)]*(n+1)

for i in range(n):
    a,b = dpl[i]
    L,R = LR[i]
    dpl[i+1] = (max(a,L),min(b,R))
for i in range(n,0,-1):
    a,b = dpr[i]
    L,R = LR[i-1]
    dpr[i-1] = (max(a,L),min(b,R))

answer = 0
for (a,b),(c,d) in zip(dpl,dpr):
    l1 = max(L1,a); r1 = min(R1,b)
    l2 = max(L2,c); r2 = min(R2,d)
    x = max(0,r1-l1+1) + max(0,r2-l2+1)
    if answer < x:
        answer = x

if answer < max_width:
    answer = max_width

print(answer)

Submission status: Wrong Answer

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[1][0]
Rmax = LR[1][1]
lpre = Lmax - Lmin + 1
rpre = Rmax - Rmin + 1

for a in LR[2:]:
    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: Wrong Answer

Solution code:
import sys

def answer():
    input = sys.stdin.readline
    N = int(input())
    questions = [list(map(int, input().split())) for _ in range(N)]

    questions.sort(key=lambda x: x[0], reverse=True)
    distances = [q[1]-q[0]+1 for q in questions]

    index_most_fun = distances.index(max(distances))
    most_fun_q = questions.pop(index_most_fun)

    def calc_overlap(q1, q2):
        return max(min(q1[1], q2[1]) - max(q1[0], q2[0]), 0)

    def calc_overlap_for_most_fun(q):
        return calc_overlap(q, most_fun_q)

    def calc_diff(q):
        return abs(q[0] - most_fun_q[0] + q[1] - most_fun_q[1])

    questions.sort(key=lambda x: x[0])
    diff_from_most_fun = list(map(calc_diff, questions))
    index_most_far = diff_from_most_fun.index(max(diff_from_most_fun))
    most_far_q = questions.pop(index_most_far)

    def calc_overlap_for_most_far(q):
        return calc_overlap(q, most_far_q)

    def calc_score(q):
        return max(q[1] - q[0] + 1, 0)

    fun = most_fun_q
    far = most_far_q
    score_fun = calc_score(fun)
    score_far = calc_score(far)

    for q in questions:
        diff_fun = score_fun - calc_overlap_for_most_fun(q)
        diff_far = score_far - calc_overlap_for_most_far(q)

        if diff_fun > diff_far:
            far = [max(q[0], far[0]), min(q[1], far[1])]
            score_far = calc_score(far)
        else:
            fun = [max(q[0], fun[0]), min(q[1], fun[1])]
            score_fun = calc_score(fun)

    print(score_fun + score_far)


if __name__ == '__main__':
    answer()

Submission status: Wrong Answer

Solution code:
# 一番長いLRが最低の楽しさ
# 一番長いLRと重なるのが一番少ないものはもう一つにする
# 一番長いLRが最低の楽しさ 一番長いLRが複数ある場合どうする？
# 一番長い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のグループにいれる
        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)
        #print(right_an, 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))

Submission status: Wrong Answer

Solution code:
def f():
    N = int(input())
    W = sorted(list(([tuple(map(int, input().split())) for _ in range(N)])), key=lambda x: x[1] - x[0])
    W.sort()

    val_left = [0] * (N - 1)  # W[:k]
    left = -1
    right = 10 ** 9 + 1
    for k in range(N - 1):
        left = max(left, W[k][0])
        right = min(right, W[k][1])
        if left > right:
            break
        val_left[k] = right - left + 1

    val_right = [0] * (N - 1)  # W[k:]
    left = -1
    right = 10 ** 9 + 1
    for k in range(N - 1, 0, -1):
        left = max(left, W[k][0])
        right = min(right, W[k][1])
        if left > right:
            break
        val_right[k - 1] = right - left + 1

    res = 0
    for k in range(N - 1):
        res = max(res, val_left[k] + val_right[k])
    print(res)


if __name__ == '__main__':
    f()

Submission status: Wrong Answer

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],x[0]))
    box0 = [lr[0][0], lr[0][1]]
    box1 = [lr[1][0], lr[1][1]]
    m=max(box0[1]-box0[0]+1,0)+max(box1[1]-box1[0]+1,0)
    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: Wrong Answer

Solution code:
import sys
read = sys.stdin.buffer.read
readline = sys.stdin.buffer.readline
readlines = sys.stdin.buffer.readlines

N = int(readline())
m = map(int,read().split())
LR = list(zip(m,m))

# 片方に集める場合
max_width = max(R-L+1 for L,R in LR)

LR.sort()

INF=10**18
# Rが最小のもの
i = -1
opt_R = INF
for n,(L,R) in enumerate(LR):
    if opt_R>R:
        opt_R=R
        i=n
j = -1
opt_L = -INF
for n,(L,R) in enumerate(LR):
    if opt_L<L:
        opt_L=L
        j=n

if i==j:
    L,R = LR[i]
    answer = max_width + (R-L+1)
    print(answer)
    exit()

L1,R1 = LR[i]; L2,R2 = LR[j]

LR = [(L,R) for n,(L,R) in enumerate(LR) if n != i and n != j]

LR.sort()

n = len(LR)
dpl = [(-INF,INF)]*(n+1)
dpr = [(-INF,INF)]*(n+1)

for i in range(n):
    a,b = dpl[i]
    L,R = LR[i]
    dpl[i+1] = (max(a,L),min(b,R))
for i in range(n,0,-1):
    a,b = dpr[i]
    L,R = LR[i-1]
    dpr[i-1] = (max(a,L),min(b,R))

answer = 0
for (a,b),(c,d) in zip(dpl,dpr):
    l1 = max(L1,a); r1 = min(R1,b)
    l2 = max(L2,c); r2 = min(R2,d)
    x = max(0,r1-l1+1) + max(0,r2-l2+1)
    if answer < x:
        answer = x

if answer < max_width:
    answer = max_width

print(answer)

Submission status: Wrong Answer

Solution code:
n=int(input())
l=[]
for i in range(n):
    l.append(list(map(int,input().split())))
l=sorted(l,key=lambda x: x[0]-x[1])
ans1=l[0][1]-l[0][0]+1
ans2=l[1][1]-l[1][0]+1
lm=-1
rm=10**10
for i in range(1,n):
    lm=max(lm,l[i][0])
    rm=min(rm,l[i][1])
ans1+=max(0,rm-lm+1)
lm=-1
rm=10**10
for i in range(n):
    if i==1:
        continue
    lm=max(lm,l[i][0])
    rm=min(rm,l[i][1])
ans2+=max(0,rm-lm+1)
ans3=max(0,min(l[0][1],l[1][1])-max(l[0][0],l[1][0])+1)
lm=-1
rm=10**10
for i in range(2,n):
    lm=max(lm,l[i][0])
    rm=min(rm,l[i][1])
ans3+=max(0,rm-lm+1)
ans4=0
l.sort()
lm=-1
rm=10**10
t=10**10
f=0
for i in range(n):
    lm=max(lm,l[i][0])
    rm=min(rm,l[i][1])
    if lm>=rm:
        f+=1
        lm=l[i][0]
        rm=l[i][1]
        if f==2:
            break
    else:
        if f==0:
            t=min(rm-lm+1,t)
ans4=0
if f==1:
    ans4=t+rm-lm+1        
print(max(ans1,ans2,ans3,ans4))

Submission status: Wrong Answer

Solution code:
n=int(input())
lr=[[0]*2 for _ in range(n)]
for i in range(n):
    lr[i] = list( map(int,input().split()) )
sortedlr = sorted(lr)
C1L=[sortedlr[i-1][0] for i in range(1,n)]
C2L=[sortedlr[-1][0]]*(n-1)

c1right=sortedlr[0][1]
c2right=sortedlr[-1][1]
C1R=[]
C2R=[]
for i in range(1,n):
    c1right=min(c1right,sortedlr[i-1][1])
    C1R.append(c1right)

    c2right=min(c2right,sortedlr[n-i][1])
    C2R.append(c2right)
C2R.reverse()

ans1 = max( max(0,C1R[i]-C1L[i]+1) + max(0,C2R[i]-C2L[i]+1) for i in range(len(C1R)))
ans2 = max( temp[1]-temp[0] for temp in sortedlr)+1
print(max(ans1,ans2))

Submission status: Wrong Answer

Solution code:
#なんですかこの汚いコードは

n=int(input())
p=[list(map(int,input().split()))for _ in range(n)]
pr,pl=[i for i in p],[i for i in p]
pr.sort(key=lambda x:[x[0],x[1]])
prr=[i for i,j in pr]
pl.sort(key=lambda x:[x[1],x[0]])
pll=[j for i,j in pl]

from bisect import bisect_right as br
from bisect import bisect_left as bl

a,b=[0,10**9],[-1,10**9+1]
aa,bb=[a[0],a[1]],[b[0],b[1]]
ai=0
bj=n
a=pr[0]
while a!=aa or b!=bb:
  aa,bb=[a[0],a[1]],[b[0],b[1]]
  j=br(prr,a[1])
  for i in range(j,bj):
    #print(b,pr[i],"b")
    if pr[i][1]<b[0] or b[1]<pr[i][0]:print(0);exit()
    b=[max(b[0],pr[i][0]),min(b[1],pr[i][1])]
  bj=j
  i=bl(pll,b[0])
  for j in range(ai,i):
    #print(a,pl[i],"a")
    if pl[i][1]<a[0] or a[1]<pl[i][0]:print(0);exit()
    a=[max(a[0],pl[i][0]),min(a[1],pl[i][1])]
  ai=i
be=[]
for i,j in p:
  if j<a[0] or a[1]<i or j<b[0] or b[1]<i:continue
  if i<=a[0]<=a[1]<=j or i<=b[0]<=b[1]<=j:continue
  be.append([i,j])
if b==[-1,10**9+1]:
  if len(be)==0:print(0);exit()
  ii=0
  ma=be[0][1]-be[0][0]
  for i in range(1,len(be)):
    if ma<=be[i][1]-be[i][0]:
      ma=be[i][1]-be[i][0]
      ii=i
  b=be[ii]
be=[]
for i,j in p:
  if j<a[0] or a[1]<i or j<b[0] or b[1]<i:continue
  if i<=a[0]<=a[1]<=j or i<=b[0]<=b[1]<=j:continue
  be.append([i,j])
#print(a)
#print(b)
#print(be)
#print("-----------")
for i,j in be:
  aij=max(i-a[0],0)+max(0,a[1]-j)
  bij=max(i-b[0],0)+max(0,b[1]-j)
  if aij>bij:
    b=[max(i,b[0]),min(j,b[1])]
  else:
    a=[max(i,a[0]),min(j,a[1])]
#print(a)
#print(b)
print(a[1]-a[0]+1 + b[1]-b[0]+1)

Submission status: Wrong Answer

Solution code:
import sys

def answer():
    input = sys.stdin.readline
    N = int(input())
    questions = [list(map(int, input().split())) for _ in range(N)]

    questions.sort(key=lambda x: x[0], reverse=True)
    distances = [q[1]-q[0]+1 for q in questions]

    index_most_fun = distances.index(max(distances))
    most_fun_q = questions.pop(index_most_fun)

    def calc_overlap(q1, q2):
        return max(min(q1[1], q2[1]) - max(q1[0], q2[0]), 0)

    def calc_overlap_for_most_fun(q):
        return calc_overlap(q, most_fun_q)

    def calc_overlap_diff(q):
        return min(q[1], most_fun_q[1]) - max(q[0], most_fun_q[0])

    questions.sort(key=lambda x: x[0])
    diff_from_most_fun = list(map(calc_overlap_diff, questions))
    index_most_far = diff_from_most_fun.index(min(diff_from_most_fun))
    most_far_q = questions.pop(index_most_far)

    def calc_overlap_for_most_far(q):
        return calc_overlap(q, most_far_q)

    def calc_score(q):
        return max(q[1] - q[0] + 1, 0)

    fun = most_fun_q
    far = most_far_q
    score_fun = calc_score(fun)
    score_far = calc_score(far)

    for q in questions:
        diff_fun = score_fun - calc_overlap_for_most_fun(q)
        diff_far = score_far - calc_overlap_for_most_far(q)

        if diff_fun > diff_far:
            far = [max(q[0], far[0]), min(q[1], far[1])]
            score_far = calc_score(far)
        else:
            fun = [max(q[0], fun[0]), min(q[1], fun[1])]
            score_fun = calc_score(fun)

    print(score_fun + score_far)


if __name__ == '__main__':
    answer()

Submission status: Wrong Answer

Solution code:
import sys
n = int(input())
lm, rm = 0, 10**10
fm, lt = 10**9, 1
seg = []
for _ in range(n):
	l, r = map(int, input().split())
	seg.append([l, r])
	lm = max(lm, l)
	rm = min(rm, r)
	fm = min(fm, l)
	lt = max(lt, r)

#print(lm, rm)
#fm, lt = , 10**9
amb = []
for l, r in seg:
	if rm < l and r < lm:
		print(0)
		sys.exit()
	elif lm == l and rm == r:
		print(rm-lm+1 + max([x[1]-x[0]+1 for x in seg]))
		sys.exit()
	elif rm < l:
		lt = min(lt, r)
	elif r < lm:
		fm = max(fm, l)
	else:
		lt = min(lt, r)
		fm = max(fm, l)
		amb.append([l, r])

#print(fm, lt)

if not amb:
	print(rm-fm+1 + lt-lm+1)

else:
	#print(amb)
	#print(rm, lm)
	amb.sort(key=lambda x: (x[0], x[1]))

	lr_max = [10**10 for _ in range(len(amb))] + [lt]
	lr_max[-2] = amb[-1][1]
	for i in range(len(amb)-2, -1, -1):
		lr_max[i] = min(lr_max[i+1], amb[i][1])

	#print(lr_max)

	ans = rm-fm+1 + lr_max[0]-lm+1
	for i in range(len(amb)):
		ans = max(ans, rm-max(fm, amb[i][0])+1 + lr_max[i+1]-lm+1)

	print(ans)