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:
from operator import itemgetter
n = int(input())
ps  = []
for i in range(n):
    l,r = map(int, input().split())
    ps.append((r-l+1,l,r))

# 右端の小さい順
cnt = 0
r_min = -1
l_max = 0
r_max = 0
l_min = 10**10
psr = sorted(ps,key=itemgetter(2))
cnts = []
for n,l,r in psr:
    r_max = max(r,r_max)
    l_min = min(l,l_min)
    # print(l_max, r_min, n,l,r)
    if l > r_min:
        cnt += 1
        # cnts.append(r_min - l_max + 1)
        r_min = r
        l_max = l
    else:
        l_max = max(l_max, l)
    if cnt == 3:
        print(0)
        exit()
# print(cnt,l_min,r_max)
# 島が1つしかなくて、かつ全域を覆う範囲が存在する
ps = sorted(ps,reverse=True)
if cnt == 1 and ps[0][1] == l_min and ps[0][2] == r_max:
    print(ps[0][0] + ps[-1][0])
    exit()



psl = sorted(ps,key=itemgetter(1),reverse = True)
a_range = psl[0]
psr = sorted(psl[1:],key=itemgetter(2))
b_range = psr[0]
ps = sorted(psr[1:])
# print(a_range,b_range)
# print(ps)

for n, l, r in ps:
    an,al,ar = a_range
    bn,bl,br = b_range
    al2 = max(al,l)
    ar2 = min(ar,r)
    an2 = ar2 - al2 + 1
    bl2 = max(bl,l)
    br2 = min(br,r)
    bn2 = br2 - bl2 + 1
    if an2 <= 0 and bn2 <= 0:
        print(0)
        exit()
    elif an2 > 0 and bn2 <= 0:
        a_range = (an2, al2, ar2)
    elif bn2 > 0 and an2 <= 0:
        b_range = (bn2, bl2, br2)
    else:
        if an2 + bn  < bn2 + an:
            #bを更新
            b_range = (bn2, bl2, br2)
        else:
            a_range = (an2, al2, ar2)

an, _, _ = a_range
bn, _, _ = b_range
print(an+bn)

Submission status: Wrong Answer

Solution code:
from itertools import accumulate
N,*L = map(int, open(0).read().split())
ls = [0]*N
ml = -1
nr = -1
li = -1
mr = 10**9
nl = -1
ri = -1
length = 0
for i,(a,b) in enumerate(zip(*[iter(L)]*2)):
  ls[i] = a,b
  if ml<a:
    ml = a
    nr = b
    li = i
  if b<mr:
    mr = b
    nl = a
    ri = i
  length = max(length, b-a+1)

ans = max(mr-ml+1,0)+length
if ri!=li:
  a,b = min(li,ri),max(li,ri)
  ls.pop(b)
  ls.pop(a)
  N -= 2
else:
  ls.pop(li)
  N -= 1
if N==0:
  print(ans)
  exit()
ls.sort()
r = list(accumulate((c[1] for c in ls[::-1]), min))[::-1]
l = list(c[0] for c in ls)
ans = max(ans, max(mr-nl+1,0)+max(r[0]-ml+1,0))
for i in range(N-1):
  xl = max(nl,l[i])
  xr = min(nr,r[i+1])
  ans = max(ans, max(mr-xl+1,0)+max(xr-ml+1,0))
xl = max(nl,l[N-1])
ans = max(ans, max(mr-xl+1,0)+nr-ml+1)
print(ans)

Submission status: Wrong Answer

Solution code:
import sys
input = sys.stdin.readline
n = int(input())
a = [list(map(int, input().split())) for i in range(n)]
lmx = 0
lmxi = -1
rmn = 10**18
rmni = -1
ansls = []
anstmp = 0
a.sort(reverse=True)
for i in range(n):
  if a[i][0] >= lmx:
    lmx = a[i][0]
    lmxi = i
  if rmn >= a[i][1]:
    rmn = a[i][1]
    rmni = i
for i in range(n):
  if i not in (lmxi,rmni):
    anstmp = max(anstmp,a[i][1]-a[i][0]+1)
ansls.append(anstmp+max(0,rmn-lmx+1))
rmnt = a[0][1]
rmnls = [0 for i in range(n)]
rmnls[-1] = a[-1][1]
for i in range(n-1)[::-1]:
  rmnls[i] = min(rmnls[i+1],a[i][1])
for i in range(n-1):
  if i in (lmxi,rmni):
    continue
  rmnt = min(rmnt,a[i][1])
  anstmp = 0
  anstmp += max(0,rmn-a[i+1][0]+1)
  anstmp += max(0,rmnt-lmx+1)
  ansls.append(anstmp)
print(max(ansls))

Submission status: Wrong Answer

Solution code:
import sys
input = sys.stdin.readline
n = int(input())
a = [list(map(int, input().split())) for i in range(n)]
lmx = 0
lmxi = -1
rmn = 10**18
rmni = -1
ansls = []
anstmp = 0
a.sort()
for i in range(n):
  if a[i][0] >= lmx:
    lmx = a[i][0]
    lmxi = i
  if rmn >= a[i][1]:
    rmn = a[i][1]
    rmni = i
for i in range(n):
  if i not in (lmxi,rmni):
    anstmp = max(anstmp,a[i][1]-a[i][0]+1)
ansls.append(anstmp+max(0,rmn-lmx+1))
rmnt = 10**18
rmnls = [0 for i in range(n)]
rmnls[-1] = a[-1][1]
for i in range(n-1)[::-1]:
  rmnls[i] = min(rmnls[i+1],a[i][1])
for i in range(n-1):
  if i in (lmxi,rmni):
    continue
  rmnt = rmnls[i+1]
  anstmp = 0
  anstmp += max(0,rmn-a[i][0]+1)
  anstmp += max(0,rmnt-lmx+1)
  ansls.append(anstmp)
print(max(ansls))

Submission status: Wrong Answer

Solution code:
import sys
input = sys.stdin.readline
n = int(input())
a = [list(map(int, input().split())) for i in range(n)]
lmx = 0
lmxi = -1
rmn = 10**18
rmni = -1
ansls = []
anstmp = 0
a.sort()
for i in range(n):
  if a[i][0] > lmx:
    lmx = a[i][0]
    lmxi = i
  if rmn > a[i][1]:
    rmn = a[i][1]
    rmni = i
for i in range(n):
  if i not in (lmxi,rmni):
    anstmp = max(anstmp,a[i][1]-a[i][0]+1)
ansls.append(anstmp+max(0,rmn-lmx+1))
rmnt = 10**18
rmnls = [0 for i in range(n)]
rmnls[-1] = a[-1][1]
for i in range(n-1)[::-1]:
  rmnls[i] = min(rmnls[i+1],a[i][1])
for i in range(n-1):
  if i in (lmxi,rmni):
    continue
  rmnt = rmnls[i+1]
  anstmp = 0
  anstmp += max(0,rmn-a[i][0]+1)
  anstmp += max(0,rmnt-lmx+1)
  ansls.append(anstmp)
print(max(ansls))

Submission status: Wrong Answer

Solution code:
N = int(input())
A = sorted([list(map(int,input().split())) for _ in range(N)],key=lambda x:x[0])
cnt1 = 0
for i in range(N):
    if cnt1<A[i][1]-A[i][0]+1:
        ind = i
        cnt1 = A[i][1]-A[i][0]+1
Rf = [0 for _ in range(N)]
Rf[0] = A[0][1]
for i in range(1,N):
    Rf[i] = min(Rf[i-1],A[i][1])
Rb = [0 for _ in range(N)]
Rb[N-1] = A[N-1][1]
for i in range(N-2,-1,-1):
    Rb[i] = min(Rb[i+1],A[i][1])
cnt2=0
for i in range(N-1):
    cnt2 = max(cnt2,max(0,Rf[i]-A[i][0]+1)+max(0,Rb[i+1]-A[N-1][0]+1))
if Rf[-1]>=A[-1][0]:
    Lmax = A[-1][0]
    if A[-1][0]==A[ind][0]:
        Lmax = A[-2][0]
    Rmin = Rf[-1]
    if Rf[-1]==A[ind][1]:
        Rmin = 10**10
        for i in range(N):
            if i!=ind:
                Rmin = min(Rmin,A[i][1])
    print(cnt1+Rmin-Lmax+1)
else:
    print(max(cnt1,cnt2))

Submission status: Wrong Answer

Solution code:
N = int(input())
A = sorted([list(map(int,input().split())) for _ in range(N)],key=lambda x:x[0])
cnt = 0
for i in range(N):
    cnt = max(cnt,A[i][1]-A[i][0]+1)
Rf = [0 for _ in range(N)]
Rf[0] = A[0][1]
for i in range(1,N):
    Rf[i] = min(Rf[i-1],A[i][1])
Rb = [0 for _ in range(N)]
Rb[N-1] = A[N-1][1]
for i in range(N-2,-1,-1):
    Rb[i] = min(Rb[i+1],A[i][1])
for i in range(N-1):
    cnt = max(cnt,max(0,Rf[i]-A[i][0]+1)+max(0,Rb[i+1]-A[N-1][0]+1))
print(cnt)

Submission status: Wrong Answer

Solution code:
INFTY = 10**10
N = int(input())
A = sorted([list(map(int,input().split())) for _ in range(N)],key=lambda x:x[0])
Rf = [INFTY for _ in range(N)]
Rf[0] = A[0][1]
for i in range(1,N):
    Rf[i] = min(Rf[i-1],A[i][1])
Rb = [INFTY for _ in range(N)]
Rb[N-1] = A[N-1][1]
for i in range(N-2,-1,-1):
    Rb[i] = min(Rb[i+1],A[i][1])
cnt = max(0,Rf[N-1]-A[N-1][0]+1)
for i in range(N-1):
    cnt = max(cnt,max(0,Rf[i]-A[i][0]+1)+max(0,Rb[i+1]-A[N-1][0]+1))
print(cnt)

Submission status: Wrong Answer

Solution code:
n = int(input())
l = []
ans = 0
a_max = 0
a_min = 1000000005
b_max = 0
b_min = 1000000005
for i in range(n):
  a, b = map(int, input().split())
  l.append([a, b])
  if a > a_max:
    index_amax = i
    a_max = a
  if a < a_min:
    index_amin = i
    a_min = i
  if b > b_max:
    index_bmax = i
    b_max = b
  if b < b_min:
    index_bmin = i
    b_min = b
if index_amax != index_bmin:
  lo_2 = l[index_amax][0]
  lo_1 = l[index_bmin][0]
  hi_2 = l[index_amax][1]
  hi_1 = l[index_bmin][1]
else:
  if a_max - a_min > b_max - b_min:
    lo_1 = l[index_amin][0]
    hi_1 = l[index_amin][1]
    lo_2 = l[index_amax][0]
    hi_2 = l[index_amax][1]
  else:
    lo_2 = l[index_bmax][0]
    hi_2 = l[index_bmax][1]
    lo_1 = l[index_amax][0]
    hi_1 = l[index_amax][1]
l.remove([lo_2, hi_2])
l.remove([lo_1, hi_1])
l.sort()
l_lo1 = [0 for i in range(n - 1)]
l_hi1 = [0 for i in range(n - 1)]
l_lo2 = [0 for i in range(n - 1)]
l_hi2 = [0 for i in range(n - 1)]
l_lo1[0] = lo_1
l_hi1[0] = hi_1
for i in range(1, n - 1):
  l_lo1[i] = max(l_lo1[i - 1], l[i - 1][0])
  l_hi1[i] = min(l_hi1[i - 1], l[i - 1][1])
l_lo2[n - 2] = lo_2
l_hi2[n - 2] = hi_2
for i in range(1, n - 1):
  l_lo2[n - 2 - i] = max(l_lo2[n - 1 - i], l[n - 2 - i][0])
  l_hi2[n - 2 - i] = min(l_hi2[n - 1 - i], l[n - 2 - i][1])
for i in range(n - 1):
  tmp1 = max(0, l_hi1[i] - l_lo1[i] + 1)
  tmp2 = max(0, l_hi2[i] - l_lo2[i] + 1)
  ans = max(ans, tmp1 + tmp2)
print(ans)

Submission status: Wrong Answer

Solution code:
n = int(input())
l = []
l2 = []
ans = 0
for i in range(n):
  a, b = map(int, input().split())
  l.append([a, b])
  l2.append([b, a])
l.sort()
l2.sort()
lo_1 = l2[0][1]
hi_1 = l2[0][0]
lo_2 = l2[-1][1]
hi_2 = l2[-1][0]
l.remove([lo_1, hi_1])
l.remove([lo_2, hi_2])
l_lo1 = [0 for i in range(n - 1)]
l_hi1 = [0 for i in range(n - 1)]
l_lo2 = [0 for i in range(n - 1)]
l_hi2 = [0 for i in range(n - 1)]
l_lo1[0] = lo_1
l_hi1[0] = hi_1
for i in range(1, n - 1):
  l_lo1[i] = max(l_lo1[i - 1], l[i - 1][0])
  l_hi1[i] = min(l_hi1[i - 1], l[i - 1][1])
l_lo2[n - 2] = lo_2
l_hi2[n - 2] = hi_2
for i in range(1, n - 1):
  l_lo2[n - 2 - i] = max(l_lo2[n - 1 - i], l[n - 2 - i][0])
  l_hi2[n - 2 - i] = min(l_hi2[n - 1 - i], l[n - 2 - i][1])
for i in range(n - 1):
  tmp1 = max(0, l_hi1[i] - l_lo1[i] + 1)
  tmp2 = max(0, l_hi2[i] - l_lo2[i] + 1)
  ans = max(ans, tmp1 + tmp2)
print(ans)

Submission status: Wrong Answer

Solution code:
n = int(input())
L,R = [0]*n,[0]*n

for i in range(n):
    L[i],R[i] = map(int,input().split())
    R[i] += 1

M = L.index(max(L))
m = R.index(min(R))

d = [R[i] - L[i] for i in range(n)]
ans = max(d) + max(R[m] - L[M], 0)

if m == M:
    print(ans)
    exit()
if n == 2:
    print(d[m] + d[M])
    exit()
a,b = [0]*n,[0]*n
for i in range(n):
    a[i],b[i] = max(R[i]-L[M],R[M]-L[M]) , max(R[m]-L[i],0,R[m]-L[m])
ab = sorted(list(zip(a,b)),reverse = True)
mb = [0]*n
for i in range(n)[::-1]:
    if i == n-1:
        mb[i] = ab[i][1]
    else:
        mb[i] = min(ab[i][1], mb[i+1])
for i in range(n-1):
    ans = max(ans,ab[i][0] + mb[i + 1])
ans = max(ans , d[M] + mb[0],ab[n-1][0] + d[m])

print(ans)

Submission status: Wrong Answer

Solution code:
n = int(input())
L,R = [0]*n,[0]*n

for i in range(n):
    L[i],R[i] = map(int,input().split())
    R[i] += 1

M = L.index(max(L))
m = R.index(min(R))

d = [R[i] - L[i] for i in range(n)]
ans = max(d) + max(R[m] - L[M], 0)

if m == M:
    print(ans)
    exit()
if n == 2:
    print(d[m] + d[M])
    exit()
a,b = [0]*n,[0]*n
for i in range(n):
    a[i],b[i] = max(R[i]-L[M],R[M]-L[M]) , max(R[m]-L[i],0,R[m]-L[m])
ab = sorted(list(zip(a,b)),reverse = True)
mb = [0]*n
for i in range(n)[::-1]:
    if i == n-1:
        mb[i] = ab[i][1]
    else:
        mb[i] = min(ab[i][1], mb[i+1])
for i in range(n-1):
    ans = max(ans,ab[i][1] + mb[i + 1])
ans = max(ans , d[M] + mb[0],ab[n-1][0] + d[m])

print(ans)

Submission status: Wrong Answer

Solution code:
n = int(input())
L,R = [0]*n,[0]*n

for i in range(n):
    L[i],R[i] = map(int,input().split())
    R[i] += 1

M = L.index(max(L))
m = R.index(min(R))

d = [R[i] - L[i] for i in range(n)]
ans = max(d) + max(R[m] - L[M], 0)

if m == M:
    print(ans)
    exit()
if n == 2:
    print(d[m] + d[M])
    exit()
a,b = [0]*n,[0]*n
for i in range(n):
    a[i],b[i] = max(R[i]-L[M],R[M]-L[M]) , max(R[m]-L[i],0,R[m]-L[m])
ab = sorted(list(zip(a,b)),reverse = True)
mb = [0]*n
for i in range(n)[::-1]:
    if i == n-1:
        mb[i] = ab[i][1]
    else:
        mb[i] = min(ab[i][1], mb[i+1])
for i in range(n-1):
    ans = max(ans,a[i] + mb[i + 1])
ans = max(ans , d[M] + mb[0],a[n-1] + d[m])

print(ans)

Submission status: Wrong Answer

Solution code:
n = int(input())
L,R = [0]*n,[0]*n

for i in range(n):
    L[i],R[i] = map(int,input().split())
    R[i] += 1

M = L.index(max(L))
m = R.index(min(R))

d = [R[i] - L[i] for i in range(n)]
ans = max(d) + max(R[m] - L[M], 0)

if m == M:
    print(ans)
    exit()
if n == 2:
    print(d[m] + d[M])
    exit()
a,b = [0]*n,[0]*n
for i in range(n):
    a[i],b[i] = max(R[i]-L[M],0) , max(R[m]-L[i],0)
ab = sorted(list(zip(a,b)),reverse = True)
mb = [0]*n
for i in range(n)[::-1]:
    if i == n-1:
        mb[i] = ab[i][1]
    else:
        mb[i] = min(ab[i][1], mb[i+1])
for i in range(n-1):
    ans = max(ans,a[i] + mb[i + 1])
ans = max(ans , d[M] + mb[0],a[n-1] + d[m])

print(ans)

Submission status: Wrong Answer

Solution code:
n = int(input())
L,R = [0]*n,[0]*n
for i in range(n):
    L[i],R[i] = map(int,input().split())
    R[i] += 1

M = L.index(max(L))
m = R.index(min(R))

d = [R[i] - L[i] for i in range(n)]
ans = max(d) + max(R[m] - L[M], 0)
if m == M:
    print(ans)
    exit()
if n == 2:
    print(d[m] + d[M])
    exit()
a,b = [0]*n,[0]*n
for i in range(n):
    a[i],b[i] = max(R[i]-L[M],0) , -max(R[m]-L[i],0)
ab = sorted(list(zip(a,b)),reverse = True)
mb = [0]*n
for i in range(n)[::-1]:
    if i == n-1:
        mb[i] = -ab[i][1]
    else:
        mb[i] = min(-ab[i][1], mb[i+1])
for i in range(n-1):
    ans = max(ans,min(a[i],d[M]) + min(mb[i + 1],d[m]))
ans = max(ans , d[M] + min(mb[0], d[m]), min(a[len(a)-1],d[M]) + d[m])

print(ans)

Submission status: Wrong Answer

Solution code:
import sys
stdin = sys.stdin

ni = lambda: int(ns())
na = lambda: list(map(int, stdin.readline().split()))
nn = lambda: list(stdin.readline().split())
ns = lambda: stdin.readline().rstrip()

n = ni()
u = []

for i in range(n):
    l,r = na()
    u.append((l,r,r-l+1))

if n == 2:
    print(u[0][2]+u[1][2])
    exit()

c = sorted(u)
b = list(reversed(c))
a = sorted(u, key = lambda x: x[1])

aa = a[0][1]
bb = b[0][0]

aaa = 0
if bb <= aa:
    ans = max(u, key = lambda x:x[2])[2]
    aaa = ans+(aa-bb+1)

d = [c[-1][1]]*n
for i in range(2,n+1):
    d[-i] = min(c[-i][1],d[-i+1])

ans = 0
v = a[1][1]-bb+1 if a[1][1] >= bb else 0
ans = (aa-a[0][0]+1) + v

for j in range(n-1):
    q = c[j]
    e = d[j+1]
    dif = e-bb+1 if e >= bb else 0
    ans = max(ans, (aa-q[0]+1)+dif)

v = aa-b[1][0]+1 if aa >= b[1][0] else 0
ans = max(ans, (b[0][1]-bb+1) + v)

print(max(ans,aaa))

Submission status: Wrong Answer

Solution code:
n = int(input())
L,R = [0]*n,[0]*n
for i in range(n):
    L[i],R[i] = map(int,input().split())
    R[i] += 1

M = L.index(max(L))
m = R.index(min(R))

d = [R[i] - L[i] if M != i != m else 0 for i in range(n)]
ans = max(d) + max(R[m] - L[M], 0)
d[m] = R[m] - L[m]
d[M] = R[M] - L[M]
if m == M:
    print(ans)
    exit()
if n == 2:
    print(d[m] + d[M])
    exit()
a,b = [0]*(n-2),[0]*(n-2)
j = 0
for i in range(n):
    if m != i != M:
        a[j],b[j] = max(R[i]-L[M],0) , max(R[m]-L[i],0)
        j += 1
ab = sorted(list(zip(a,b)),reverse = True)
mb = [0]*len(a)
for i in range(len(a))[::-1]:
    if i == len(a)-1:
        mb[i] = ab[i][1]
    else:
        mb[i] = min(ab[i][1], mb[i+1])
for i in range(len(a) - 1):
    ans = max(ans,min(a[i],d[M]) + min(mb[i + 1],d[m]))
ans = max(ans , d[M] + min(mb[0], d[m]), min(a[len(a)-1],d[M]) + d[m])

print(ans)

Submission status: Wrong Answer

Solution code:
import sys
stdin = sys.stdin

ni = lambda: int(ns())
na = lambda: list(map(int, stdin.readline().split()))
nn = lambda: list(stdin.readline().split())
ns = lambda: stdin.readline().rstrip()

n = ni()
u = []

for i in range(n):
    l,r = na()
    u.append((l,r,r-l+1))

c = sorted(u)
b = list(reversed(c))
a = sorted(u, key = lambda x: x[1])

aa = a[0][1]
bb = b[0][0]

aaa = 0
if bb <= aa:
    ans = max(u, key = lambda x:x[2])[2]
    aaa = ans+(aa-bb+1)

d = [c[-1][1]]*n
for i in range(2,n+1):
    d[-i] = min(c[-i][1],d[-i+1])

ans = 0
#v = a[1][1]-bb+1 if a[1][1] >= bb else 0
#ans = (aa-a[0][0]+1) + v

for j in range(n-1):
    q = c[j]
    e = d[j+1]
    dif = e-bb+1 if e >= bb else 0
    ans = max(ans, (aa-q[0]+1)+dif)

#v = aa-b[1][0]+1 if aa >= b[1][0] else 0
#ans = max(ans, (b[0][1]-bb+1) + v)

print(max(ans,aaa))

Submission status: Wrong Answer

Solution code:
import sys
stdin = sys.stdin

ni = lambda: int(ns())
na = lambda: list(map(int, stdin.readline().split()))
nn = lambda: list(stdin.readline().split())
ns = lambda: stdin.readline().rstrip()

n = ni()
u = []

for i in range(n):
    l,r = na()
    u.append((l,r,r-l+1))

c = sorted(u)
b = list(reversed(c))
a = sorted(u, key = lambda x: x[1])

aa = a[0][1]
bb = b[0][0]

aaa = 0
if bb <= aa:
    ans = max(u, key = lambda x:x[2])[2]
    aaa = ans+(aa-bb+1)

d = [c[-1][1]]*n
for i in range(2,n+1):
    d[-i] = min(c[-i][1],d[-i+1])

ans = 0
#v = a[1][1]-bb+1 if a[1][1] >= bb else 0
#ans = (aa-a[0][0]+1) + v

for j in range(n-1):
    q = c[j]
    e = d[j+1]
    dif = e-bb+1 if e >= bb else 0
    ans = max(ans, (aa-q[0]+1)+dif)

v = aa-b[1][0]+1 if aa >= b[1][0] else 0
ans = max(ans, (b[0][1]-bb+1) + v)

print(max(ans,aaa))

Submission status: Wrong Answer

Solution code:
import sys
stdin = sys.stdin

ni = lambda: int(ns())
na = lambda: list(map(int, stdin.readline().split()))
nn = lambda: list(stdin.readline().split())
ns = lambda: stdin.readline().rstrip()

n = ni()
u = []

for i in range(n):
    l,r = na()
    u.append((l,r,r-l+1))

c = sorted(u)
b = list(reversed(c))
a = sorted(u, key = lambda x: x[1])

aa = a[0][1]
bb = b[0][0]

aaa = 0
if bb <= aa:
    ans = max(u, key = lambda x:x[2])[2]
    aaa = ans+(aa-bb+1)

d = [c[-1][1]]*n
for i in range(2,n+1):
    d[-i] = min(c[-i][1],d[-i+1])

v = a[1][1]-bb+1 if a[1][1] >= bb else 0
ans = (aa-a[0][0]+1) + v

for j in range(n-1):
    q = c[j]
    e = d[j+1]
    dif = e-bb+1 if e >= bb else 0
    ans = max(ans, (aa-max(a[0][0],q[0])+1)+dif)

v = aa-b[1][0]+1 if aa >= b[1][0] else 0
ans = max(ans, (b[0][1]-bb+1) + v)

print(max(ans,aaa))

Submission status: Wrong Answer

Solution code:
import sys
stdin = sys.stdin

ni = lambda: int(ns())
na = lambda: list(map(int, stdin.readline().split()))
nn = lambda: list(stdin.readline().split())
ns = lambda: stdin.readline().rstrip()

n = ni()
u = []

for i in range(n):
    l,r = na()
    u.append((l,r,r-l+1))

c = sorted(u)
b = list(reversed(c))
a = sorted(u, key = lambda x: x[1])

aa = a[0][1]
bb = b[0][0]

aaa = 0
if bb <= aa:
    ans = max(u, key = lambda x:x[2])[2]
    aaa = ans+(aa-bb+1)

d = [c[-1][1]]*n
for i in range(2,n+1):
    d[-i] = min(c[-i][1],d[-i+1])

v = a[1][1]-bb+1 if a[1][1] >= bb else 0
ans = (aa-a[0][0]+1) + v

for j in range(n-1):
    q = c[j]
    e = d[j+1]
    dif = e-bb+1 if e >= bb else 0
    ans = max(ans, (aa-max(a[0][1],q[0])+1)+dif)

v = aa-b[1][0]+1 if aa >= b[1][0] else 0
ans = max(ans, (b[0][1]-bb+1) + v)

print(max(ans,aaa))

Submission status: Wrong Answer

Solution code:
import sys
stdin = sys.stdin

ni = lambda: int(ns())
na = lambda: list(map(int, stdin.readline().split()))
nn = lambda: list(stdin.readline().split())
ns = lambda: stdin.readline().rstrip()

n = ni()
u = []

for i in range(n):
    l,r = na()
    u.append((l,r,r-l+1,i))

c = sorted(u)
b = list(reversed(c))
a = sorted(u, key = lambda x: x[1])

aa = a[0][1]
bb = b[0][0]

aaa = 0
if bb <= aa:
    ans = max(u, key = lambda x:x[2])[2]
    aaa = ans+(aa-bb+1)

d = [c[-1][1]]*n
for i in range(2,n+1):
    d[-i] = min(c[-i][1],d[-i+1])

v = a[1][1]-bb+1 if a[1][1] >= bb else 0
ans = (aa-a[0][0]+1) + v

for j in range(n-1):
    q = c[j]
    #if q[3] == b[0][3] or q[3] == a[0][3]:
        #continue
    e = d[j+1]
    dif = e-bb+1 if e >= bb else 0
    ans = max(ans, (aa-q[0]+1)+dif)

v = aa-b[1][0]+1 if aa >= b[1][0] else 0
ans = max(ans, (b[0][1]-bb+1) + v)

print(max(ans,aaa))

Submission status: Wrong Answer

Solution code:
n = int(input())
L,R = [0]*n,[0]*n
for i in range(n):
    L[i],R[i] = map(int,input().split())
    R[i] += 1

M = L.index(max(L))
m = R.index(min(R))

d = [R[i] - L[i] if M != i != m else 0 for i in range(n)]
ans = max(d) + max(R[m] - L[M], 0)
d[m] = R[m] - L[m]
d[M] = R[M] - L[M]
if m == M:
    print(ans)
    exit()
if n == 2:
    print(d[m] + d[M])
    exit()
a,b = [0]*(n-2),[0]*(n-2)
j = 0
for i in range(n):
    if m != i != M:
        a[j],b[j] = max(R[i]-L[M],0) , -max(R[m]-L[i],0)
        j += 1
ab = sorted(list(zip(a,b)),reverse = True)
mb = [0]*len(a)
for i in range(len(a))[::-1]:
    if i == len(a)-1:
        mb[i] = -ab[i][1]
    else:
        mb[i] = min(-ab[i][1], mb[i+1])
for i in range(len(a) - 1):
    ans = max(ans,min(a[i],d[M]) + min(mb[i + 1],d[m]))
ans = max(ans , d[M] + min(mb[0], d[m]), min(a[len(a)-1],d[M]) + d[m])

print(ans)

Submission status: Wrong Answer

Solution code:
import sys
stdin = sys.stdin

ni = lambda: int(ns())
na = lambda: list(map(int, stdin.readline().split()))
nn = lambda: list(stdin.readline().split())
ns = lambda: stdin.readline().rstrip()

n = ni()
u = []

for i in range(n):
    l,r = na()
    u.append((l,r,r-l+1,i))

c = sorted(u)
b = list(reversed(c))
a = sorted(u, key = lambda x: x[1])

aa = a[0][1]
bb = b[0][0]

aaa = 0
"""
if bb <= aa:
    ans = max(u, key = lambda x:x[2])[2]
    aaa = ans+(aa-bb+1)
"""
d = [c[-1][1]]*n
for i in range(2,n+1):
    d[-i] = min(c[-i][1],d[-i+1])

v = a[1][1]-bb+1 if a[1][1] >= bb else 0
ans = (aa-a[0][0]+1) + v

for j in range(n-1):
    q = c[j]
    if q[3] == b[0][3] or q[3] == a[0][3]:
        continue
    e = d[j+1]
    dif = e-bb+1 if e >= bb else 0
    ans = max(ans, (aa-q[0]+1)+dif)

v = aa-b[1][0]+1 if aa >= b[1][0] else 0
ans = max(ans, (b[0][1]-bb+1) + v)

print(max(ans,aaa))

Submission status: Wrong Answer

Solution code:
import sys
stdin = sys.stdin

ni = lambda: int(ns())
na = lambda: list(map(int, stdin.readline().split()))
nn = lambda: list(stdin.readline().split())
ns = lambda: stdin.readline().rstrip()

n = ni()
u = []

for i in range(n):
    l,r = na()
    u.append((l,r,r-l+1,i))

c = sorted(u)
b = list(reversed(c))
a = sorted(u, key = lambda x: x[1])

aa = a[0][1]
bb = b[0][0]

aaa = 0
if bb <= aa:
    ans = max(u, key = lambda x:x[2])[2]
    aaa = ans+(aa-bb+1)

d = [c[-1][1]]*n
for i in range(2,n+1):
    d[-i] = min(c[-i][1],d[-i+1])

v = a[1][1]-bb+1 if a[1][1] >= bb else 0
ans = (aa-a[0][0]+1) + v
for j in range(n-1):
    q = c[j]
    if q[3] == b[0][3] or q[3] == a[0][3]:
        continue
    e = d[j+1]
    dif = e-bb+1 if e >= bb else 0
    ans = max(ans, (aa-q[0]+1)+dif)
v = aa-b[1][0]+1 if aa >= b[1][0] else 0
ans = max(ans, (b[0][1]-bb+1) + v)

print(max(ans,aaa))

Submission status: Wrong Answer

Solution code:
import sys
stdin = sys.stdin

ni = lambda: int(ns())
na = lambda: list(map(int, stdin.readline().split()))
nn = lambda: list(stdin.readline().split())
ns = lambda: stdin.readline().rstrip()

n = ni()
u = []

for i in range(n):
    l,r = na()
    u.append((l,r,r-l+1,i))

c = sorted(u)
b = list(reversed(c))
a = sorted(u, key = lambda x: x[1])

aa = a[0][1]
bb = b[0][0]
aaa = 0
if bb < aa:
    ans = max(u, key = lambda x:x[2])[2]
    aaa = ans+(aa-bb+1)

v = a[1][1]-bb+1 if a[1][1] >= bb else 0
ans = (aa-a[0][0]+1) + v

d = [c[-1][1]]*n
for i in range(2,n+1):
    d[-i] = min(c[-i][1],d[-i+1])

for j in range(n-1):
    q = c[j]
    if q[3] == b[0][3] or q[3] == a[0][3]:
        continue
    e = d[j+1]
    dif = e-bb+1 if e >= bb else 0
    ans = max(ans, (aa-q[0]+1)+dif)
v = aa-b[1][0]+1 if aa >= b[1][0] else 0
ans = max(ans, (b[0][1]-bb+1) + v)
print(max(ans,aaa))

Submission status: Wrong Answer

Solution code:
#!/usr/bin/env python3
from collections import defaultdict,deque
from heapq import heappush, heappop
from bisect import bisect_left, bisect_right
import sys, random, itertools, math
sys.setrecursionlimit(10**5)
input = sys.stdin.readline
sqrt = math.sqrt
def LI(): return list(map(int, input().split()))
def LF(): return list(map(float, input().split()))
def LI_(): return list(map(lambda x: int(x)-1, input().split()))
def II(): return int(input())
def IF(): return float(input())
def LS(): return list(map(list, input().split()))
def S(): return list(input().rstrip())
def IR(n): return [II() for _ in range(n)]
def LIR(n): return [LI() for _ in range(n)]
def FR(n): return [IF() for _ in range(n)]
def LFR(n): return [LI() for _ in range(n)]
def LIR_(n): return [LI_() for _ in range(n)]
def SR(n): return [S() for _ in range(n)]
def LSR(n): return [LS() for _ in range(n)]
mod = 1000000007
inf = 1e10

class SlidingWindowAggregation:
    """ https://scrapbox.io/data-structures/Sliding_Window_Aggregation """
    
    def __init__(self, f=sum):
        self.f = f
        self.front_stack = deque()
        self.back_stack = deque()
    
    def empty(self):
        return self.front_stack or self.back_stack

    def __len__(self):
        return len(self.front_stack) + len(self.back_stack)

    def fold_all(self):
        try:
            assert self.empty(), "Both stack is empty"
            if not self.front_stack:
                return self.back_stack[-1][1]
            elif not self.back_stack:
                return self.front_stack[-1][1]
            else:
                return self.f(self.front_stack[-1][1], self.back_stack[-1][1])
        except AssertionError as err:
            print("AssertionError :", err)

    def push(self, x):
        if not self.back_stack:
            self.back_stack.append((x, x))
        else:
            tmp = self.f(self.back_stack[-1][1], x)
            self.back_stack.append((x, tmp))

    def popleft(self):
        try:
            assert self.empty(), "Both stack is empty"
            if not self.front_stack:
                x,fx = self.back_stack.pop()
                self.front_stack.append((x, x))
                while self.back_stack:
                    x, fx = self.back_stack.pop()
                    fx = self.f(x, self.front_stack[-1][1])
                    self.front_stack.append((x, fx))
            self.front_stack.pop()

        except AssertionError as err:
            print("AssertionError :", err)

#solve
def solve():
    n = II()
    lr = LIR(n)
    maxl = max([l for l, r in lr])
    minr = min([r for l, r in lr])
    ans = max(minr - maxl, 0) + max([r - l for l, r in lr])
    lisminr = SlidingWindowAggregation(min)
    lr.sort()
    for _, r in lr:
        lisminr.push(r)

    for l, r in lr[:-1]:
        lisminr.popleft()
        ans = max(ans, max(minr - l, 0) + max(lisminr.fold_all() - maxl, 0))
    print(ans + 2)
    return


#main
if __name__ == '__main__':
    solve()

Submission status: Wrong Answer

Solution code:
import sys
input = sys.stdin.buffer.readline
 
def main():
    N = int(input())
    x = [tuple(map(int,input().split())) for _ in range(N)]
    
    u,d = [0 for _ in range(N)],[0 for _ in range(N)]
    x.sort(key=lambda x:(x[0],-x[1]))
 
    l,r = 0,10**10
    for i,v in enumerate(x):
        a,b = v
        if b < l or r < a:
            continue
        else:
            l,r = max(a,l),min(b,r)
            u[i] = r-l+1

    l,r = 0,10**10
    for i,v in enumerate(x[::-1]):
        a,b = v
        if b < l or r < a:
            continue
        else:
            l,r = max(a,l),min(b,r)
            d[-i-1] = r-l+1

    ans = 0
    for i in range(N-1):
        ans = max(ans,u[i]+d[i+1])
    
    print(ans)
 
if __name__ == "__main__":
    main()

Submission status: Wrong Answer

Solution code:
def kazu(p, q):
    return max(p[0], q[0]), min(p[1], q[1]), max(-max(p[0], q[0]) + min(p[1], q[1]) + 1, 0)
def kazu1(p):
    return p[1] - p[0] + 1
def main():
    N = int(input())
    l = []
    for _ in range(N):
        L, R = map(int, input().split())
        l.append((L, R))
    l.sort(key=lambda x:x[1])
    dp= (l[0], l[1])
    for i in range(2, N):
        a, b, c = kazu(dp[0], l[i]) 
        d, e, f = kazu(dp[1], l[i])
        g, h, j = kazu(dp[0], dp[1])
        m = max(f + kazu1(dp[0]), c + kazu1(dp[1]), j + kazu1(l[i]))
        if  m == f + kazu1(dp[0]):
            dp = ((d,e),dp[0])
        elif m == c + kazu1(dp[1]):
            dp = ((a,b),dp[1])
        else:
            dp = ((g,h), l[i])
    return kazu1(dp[0]) + kazu1(dp[1])
print(main())

Submission status: Wrong Answer

Solution code:
def kazu(p, q):
    return max(p[0], q[0]), min(p[1], q[1]), max(-max(p[0], q[0]) + min(p[1], q[1]) + 1, 0)
def kazu1(p):
    return p[1] - p[0] + 1
def main():
    N = int(input())
    l = []
    for _ in range(N):
        L, R = map(int, input().split())
        l.append((L, R))
    l.sort(key=lambda x:x[1])
    dp= [(l[0], l[1])]
    for i in range(2, N):
        a, b, c = kazu(dp[i-2][0], l[i]) 
        d, e, f = kazu(dp[i-2][1], l[i])
        if  f + kazu1(dp[i-2][0]) > c + kazu1(dp[i-2][1]):
            dp.append(((d,e),dp[i-2][0]))
        else:
            dp.append(((a,b),dp[i-2][1]))
    return kazu1(dp[-1][0]) + kazu1(dp[-1][1])
print(main())

Submission status: Wrong Answer

Solution code:
def kazu(p, q):
    return max(p[0], q[0]), min(p[1], q[1]), max(-max(p[0], q[0]) + min(p[1], q[1]) + 1, 0)
def kazu1(p):
    return p[1] - p[0] + 1
def main():
    N = int(input())
    l = []
    for _ in range(N):
        L, R = map(int, input().split())
        l.append((L, R))
    l.sort(key=lambda x:x[0])
    dp= [(l[0], l[1])]
    for i in range(2, N):
        a, b, c = kazu(dp[i-2][0], l[i]) 
        d, e, f = kazu(dp[i-2][1], l[i])
        if  f + kazu1(dp[i-2][0]) > c + kazu1(dp[i-2][1]):
            dp.append(((d,e),dp[i-2][0]))
        else:
            dp.append(((a,b),dp[i-2][1]))
    return kazu1(dp[-1][0]) + kazu1(dp[-1][1])
print(main())

Submission status: Wrong Answer

Solution code:
def kazu(p, q):
    return max(p[0], q[0]), min(p[1], q[1]), max(-max(p[0], q[0]) + min(p[1], q[1]) + 1, 0)
def kazu1(p):
    return p[1] - p[0] + 1
def main():
    N = int(input())
    l = []
    for _ in range(N):
        L, R = map(int, input().split())
        l.append((L, R))
    dp= [(l[0], l[1])]
    for i in range(2, N):
        a, b, c = kazu(dp[i-2][0], l[i]) 
        d, e, f = kazu(dp[i-2][1], l[i])
        if  f + kazu1(dp[i-2][0]) > c + kazu1(dp[i-2][1]):
            dp.append(((d,e),dp[i-2][0]))
        else:
            dp.append(((a,b),dp[i-2][1]))
    return kazu1(dp[-1][0]) + kazu1(dp[-1][1])
print(main())