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 pleasure(a,b):
    if a[1]<b[0] or b[1]<a[0]:
        return [0,0]
    if a[0]<=b[0]<=a[1]<=b[1]:
        return [b[0],a[1]]
    if a[0]<=b[0]<=b[1]<=a[1]:
        return [b[0],b[1]]
    if b[0]<=a[0]<=b[1]<=a[1]:
        return [a[0],b[1]]
    if b[0]<=a[0]<=a[1]<=b[1]:
        return [a[0],a[1]]

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

first_contest = List[0]

# 問題1に対して最も楽しめない問題をsecond_contestに回す
worst_problem = 0
worst_pleasure_value = 100000000
for i in range(N):
    now_pleasure = pleasure(List[0],List[i])
    now_pleasure_value = now_pleasure[1]-now_pleasure[0]+1
    if worst_pleasure_value > now_pleasure_value:
        worst_pleasure_value = now_pleasure_value
        worst_problem = i
    if worst_pleasure_value == 0: #楽しさ0だったら探索終了
        break
second_contest = List[worst_problem]

# 順にどちらのコンテストに入れるかを考えていく
for i in range(N):
    pf = pleasure(first_contest,List[i])
    ps = pleasure(second_contest,List[i])
    if (pf[1]-pf[0]+1) - (first_contest[1]-first_contest[0]+1) > (ps[1]-ps[0]+1) - (second_contest[1]-second_contest[0]+1):
        first_contest = pf
    else:
        second_contest = ps
        
print(first_contest[1]-first_contest[0]+1
    +second_contest[1]-second_contest[0]+1)

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])
    W.sort(key=lambda x: x[0])

    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:
n = int(input())
p = []
max_p = 1001001001
max_i = 0
max_len = 0
for i in range(n):
  l, r = map(int, input().split())
  p.append([l, r])
  if max_p > l:
    max_p = l
    max_i = i
    max_len = r - l
  elif max_p == l:
    if r - l > max_len:
      max_i = i
      max_len = r - l
min_p = 0
min_i = 0
for i in range(n):
  dif = min(p[max_i][0] - p[i][0], 0) + min(p[i][1] - p[max_i][1], 0)
  if dif < min_p:
    min_p = dif
    min_i = i
g1 = []
g2 = []
max_l = p[max_i][0]
max_r = p[max_i][1]
min_l = p[min_i][0]
min_r = p[min_i][1]
g1.append(p[max_i])
g2.append(p[min_i])
for i in range(n):
  if i == max_i or i == min_i:
    continue
  if min(max_l - p[i][0], 0) + min(p[i][1] - max_r, 0) < min(min_l - p[i][0], 0) + min(p[i][1] - min_r, 0):
    g2.append(p[i])
  else:
    g1.append(p[i])
g1_l = 0
g1_r = 1001001001
g2_l = 0
g2_r = 1001001001
for i in range(len(g1)):
  if g1[i][0] > g1_l:
    g1_l = g1[i][0]
  if g1[i][1] < g1_r:
    g1_r = g1[i][1]
for i in range(len(g2)):
  if g2[i][0] > g2_l:
    g2_l = g2[i][0]
  if g2[i][1] < g2_r:
    g2_r = g2[i][1]
s_g1 = max(g1_r - g1_l + 1, 0)
s_g2 = max(g2_r - g2_l + 1, 0)
ans = s_g1 + s_g2
print(ans)

Submission status: Wrong Answer

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

Solution code:
N = int(input())
A = []
for i in range(N):
    L,R = map(int,input().split())
    A.append([L,R,i])
B = sorted(A)
C = sorted(A, key=lambda x: x[1])
l0,l1,m0,m1 = B[N-1][0],B[N-1][1],C[0][0],C[0][1]
I,J = B[N-1][2],C[0][2]
M = 0
if I == J:
    for i in range(N):
        a1,a0 = A[i][1],A[i][0]
        if M <= a1-a0:
            M = a1-a0
    ans = m1-l0 + M+2
else:
    for i in range(N):
        if i == I or i == J:
            continue
        a1,a0 = A[i][1],A[i][0]
        if M <= a1-a0:
            M = a1-a0
    ans1 = m1-l0 + M
    for i in range(N):
        a1,a0 = B[i][1],B[i][0]
        if min([l1,a1])-l0 + m1-m0>l1-l0 + m1-max([m0,a0]):
            l1 = min([l1,a1])
        else:
            m0 = max([m0,a0])
    ans2 = l1-l0+m1-m0
    l0,l1,m0,m1 = B[N-1][0],B[N-1][1],C[0][0],C[0][1]
    for i in range(N-1,-1,-1):
        a1,a0 = C[i][1],C[i][0]
        if min([l1,a1])-l0 + m1-m0>l1-l0 + m1-max([m0,a0]):
            l1 = min([l1,a1])
        else:
            m0 = max([m0,a0])
    ans3 = l1-l0+m1-m0
    ans = max([ans1+2,ans2+2,ans3+2,0])
print(ans)

Submission status: Wrong Answer

Solution code:
import sys
def I(): return int(sys.stdin.readline())
def LI(): return list(map(int,sys.stdin.readline().split()))
mod = 10**9 + 7
inf = float('inf')
maxlen = int(0)
maxi = 0
N = I()
LR =[[] for _ in range(N)]
LR2 =[]
for i in range(N):
    LR[i] = LI()
    C = LR[i][1] - LR[i][0]
    if C>maxlen:
        maxlen = C
        maxi = i
for i in range(N):
    if not i==maxi:
        LR2.append(LR[i])

LR.sort()
rightend = LR[0][1]
leftend  = LR[N-1][0]

live1 = [[] for _ in range(N)]
live2 = [[] for _ in range(N)]
live1[0] = LR[0]
live2[N-1] = LR[N-1]
#print(rightend)
#print(leftend)

cur1 = -1; cur2 = -1
for i in range(1,N):
    if live1[i-1][1]>LR[i][1]:
        rightend = LR[i][1]
    if rightend<LR[i][0]:
        cur1 = i-1
        break
    live1[i] = [max(live1[i-1][0],LR[i][0]),min(live1[i-1][1],LR[i][1])]

for i in range(N-2,-1,-1):
    if live2[i+1][1]<LR[i][0]:
        leftend = LR[i][0]
    if leftend>LR[i][1]:
        cur2 = i+1
        break
    live2[i] = [max(live2[i+1][0],LR[i][0]),min(live2[i+1][1],LR[i][1])]
#print(cur1,cur2)
#print(live1)
#print(live2)
#print(leftend)
if cur1==-1:
    cur1=N-2
if cur2==-1:
    cur2=1
ansC = [0]*N
for i in range(cur2,cur1+2):
    ansC[i] = (live1[i-1][1]-live1[i-1][0]+1) + (live2[i][1]-live2[i][0]+1)

ans = max(max(ansC),maxlen+1)
####################################################
if ans==maxlen+1:
    LR2.sort()
    live3 = LR2[0]
    for i in range(1,N-1):
        live3 = [max(live3[0],LR2[i][0]),min(live3[1],LR2[i][1])]
    ansd = live3[1]-live3[0]+1
    ans = max(ansd,0)+ans
print(ans)

Submission status: Wrong Answer

Solution code:
from operator import itemgetter

N = int(input())
L = [[int(x) for x in input().split()] for _ in range(N)]


for i in range(N):
    L[i].append(L[i][1]-L[i][0])
    
L.sort()
L.sort(key=itemgetter(2))
    

la = L[0][0]
ra = L[0][1]
lb = L[1][0]
rb = L[1][1]
ans = L[1][2]+1
for i in range(2, N):
    ta = max(0, min(ra, L[i][1])-max(la, L[i][0])+1) + rb-lb+1
    tb = max(0, min(rb, L[i][1])-max(lb, L[i][0])+1) + ra-la+1
    tc = max(0, min(ra, rb)-max(la,lb)+1) + L[i][1]-L[i][0]+1
    if max(ta, tb, tc) == ta:
        la = max(la, L[i][0])
        ra = min(ra, L[i][1])
    elif max(ta, tb, tc) == tb:
        lb = max(lb, L[i][0])
        rb = min(rb, L[i][1])
    else:
        la = max(la, lb)
        ra = min(ra, rb)
        lb = L[i][0]
        rb = L[i][1]
        
ans = max(ans, max(0, ra-la+1) + max(0, rb-lb+1))


print(ans)

Submission status: Wrong Answer

Solution code:
N = int(input())
LR = [[]]*N
max_n = 0
min_n = 10**9
for i in range(N):
    LR[i] = list(map(int, input().split()))
    min_n = min(min_n, LR[i][0])
    max_n = max(max_n, LR[i][1])

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


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

Submission status: Wrong Answer

Solution code:
import sys
input = sys.stdin.readline
N = int(input())
lr = [list(map(int, input().split())) for _ in range(N)]
lr.sort(key = lambda x: (x[1], -x[0]))
#print(lr)
dpx = [0] * N
dpy = [0] * N
dpx[0] = lr[0][1] - lr[0][0] + 1
def check(x, y):
  if x[1] >= y[0]:
    return [max(x[0], y[0]), min(x[1], y[1])]
  else: return [-1, -1]
ran = [0, 10 ** 9]
for i in range(N - 1, -1, -1):
  ran = check(lr[i], ran)
  dpy[i] = ran[1] - ran[0] + 1
  #print(lr[i])
ran = lr[0]
for i in range(N):
  ran = check(lr[i], ran)
  dpx[i] = ran[1] - ran[0] + 1
  #print(lr[i])
#print(dpx, dpy)
res = 0
for i in range(N - 1):
  res = max(dpx[i] + dpy[i + 1], res)
print(res)

Submission status: Wrong Answer

Solution code:
import sys
from collections import Counter, deque, defaultdict
from math import factorial
from fractions import Fraction
import heapq, bisect
import math
import itertools
sys.setrecursionlimit(10 ** 5 + 10)
INF = 10**15 +5
MOD = 10**9 + 7
def input(): return sys.stdin.readline().strip()
def INT(): return int(input())
def MAP(): return map(int, input().split())
def LIST(): return list(map(int, input().split()))

n = INT()
tests = []
for i in range(n):
    l, r = MAP()
    tests.append((l, r))
lmax = tests[0]
for i in range(1,n):
    if tests[i][0] > lmax[0]:
        lmax = tests[i]
rmin = tests[0]
for i in range(1,n):
    if tests[i][1] < rmin[1]:
        rmin = tests[i]

tests.remove(lmax)
if lmax == rmin:
    rmin = tests[0]
    
left = rmin
right = lmax

for i in range(len(tests)):
    l = tests[i][0]
    r = tests[i][1]
    if max(l-left[0], 0) + max(left[1]-r, 0) <= max(l-right[0], 0) + max(right[1]-r, 0):
        left = [max(l, left[0]), min(r, left[1])]
    else:
        right = [max(l, right[0]), min(r, right[1])]

print(left[1] - left[0] + right[1] - right[0] + 2)

Submission status: Wrong Answer

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][0]:
        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 lr[::-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: Wrong Answer

Solution code:
# -*- coding: utf-8 -*-
"""
Created on Mon Nov  4 01:34:42 2019

@author: miz18
"""


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

Lsorted = sorted(sorted(LR,key=lambda x:x[1],reverse=True),key = lambda x: x[0],reverse=True)
Rsorted = sorted(sorted(LR,key=lambda x:x[0]),key = lambda x: x[1])

Lmax = Lsorted[0]
Rmin = Rsorted[0]
if Lmax[2]==Rmin[2]:
    LRdif = [LR[i][1]-LR[i][0]+1 for i in range(N)]
    LRdif.sort()
    print(Lmax[1]-Lmax[0]+1+LRdif[-1])
else:
#    M1 = Lmax[1]
#    M2 = Rmin[0]
#    for l,r in LR:
#        if r-Lmax[0] > Rmin[1]-l:
#            M1 = max(min(r,M1),Lmax[0]-1)
#        else:
#            M2 = min(max(l,M2),Rmin[1]+1)
#    print(M1-Lmax[0]+1 + Rmin[1]-M2+1)
    Rsorted = sorted(sorted(LR,key=lambda x:x[0]),key=lambda x:[1])
    maxL = Rmin[0]
    maxscore = 0
    for i in range(N):
        if Rsorted[i][2]!=Lmax[2]:
            maxL = max(Rsorted[i][0],maxL)
            nowminR = Rsorted[i+1][1]
            maxscore =max(maxscore, max(nowminR-Lmax[0]+1,0) + max(Rmin[1]-maxL+1,0))
        else:
            break

                
#    Lsorted = sorted(sorted(LR,key=lambda x:x[1],reverse=True),key=lambda x:[0],reverse=True)
#    minR = Lmax[1]
#    for i in range(1,N):
#        nowmaxL = min(Lsorted[i][0],Rmin[0])
#        maxscore =max(maxscore, max(minR-Lmax[0]+1,0) + max(Rmin[1]-nowmaxL+1,0))
#        minR = max(Lsorted[i][1],minR)

    if Rsorted[N-1][2]==Lmax[2]:
        print(-1)
    else:
        print(maxscore)

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)

print(max(ans_ls))

Submission status: Wrong Answer

Solution code:
N, *LR = map(int, open(0).read().split())
LR = sorted((r - l + 1, l, r) for l, r in zip(*[iter(LR)] * 2))

_, max_l, min_r = LR[0]
A = []
for _, l, r in LR:
    max_l, min_r = max(max_l, l), min(min_r, r)
    A.append(max(0, min_r - max_l + 1))

_, max_l, min_r = LR[-1]
B = []
for _, l, r in reversed(LR):
    max_l, min_r = max(max_l, l), min(min_r, r)
    B.append(max(0, min_r - max_l + 1))

B.pop()

print(max(a + b for a, b in zip(A, reversed(B))))

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]) + abs(q[1] - most_fun_q[1])

    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:
# -*- coding: utf-8 -*-
"""
Created on Mon Nov  4 01:34:42 2019

@author: miz18
"""


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

Lsorted = sorted(sorted(LR,key=lambda x:x[1],reverse=True),key = lambda x: x[0],reverse=True)
Rsorted = sorted(sorted(LR,key=lambda x:x[0]),key = lambda x: x[1])

Lmax = Lsorted[0]
Rmin = Rsorted[0]
if Lmax[2]==Rmin[2]:
    LRdif = [LR[i][1]-LR[i][0]+1 for i in range(N)]
    LRdif.sort()
    print(Lmax[1]-Lmax[0]+1+LRdif[-1])
else:
#    M1 = Lmax[1]
#    M2 = Rmin[0]
#    for l,r in LR:
#        if r-Lmax[0] > Rmin[1]-l:
#            M1 = max(min(r,M1),Lmax[0]-1)
#        else:
#            M2 = min(max(l,M2),Rmin[1]+1)
#    print(M1-Lmax[0]+1 + Rmin[1]-M2+1)
    Rsorted = sorted(sorted(LR,key=lambda x:x[0]),key=lambda x:[1])
    maxL = Rmin[0]
    maxscore = 0
    for i in range(N):
        if Rsorted[i][2]!=Lmax[2]:
            maxL = max(Rsorted[i][0],maxL)
            nowminR = Rsorted[i+1][1]
            maxscore =max(maxscore, max(nowminR-Lmax[0]+1,0) + max(Rmin[1]-maxL+1,0))
        else:
            break
                
#    Lsorted = sorted(sorted(LR,key=lambda x:x[1],reverse=True),key=lambda x:[0],reverse=True)
#    minR = Lmax[1]
#    for i in range(1,N):
#        nowmaxL = min(Lsorted[i][0],Rmin[0])
#        maxscore =max(maxscore, max(minR-Lmax[0]+1,0) + max(Rmin[1]-nowmaxL+1,0))
#        minR = max(Lsorted[i][1],minR)

    print(maxscore)

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

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

Solution code:
import sys
input = sys.stdin.readline
N = int(input())
lr = [list(map(int, input().split())) for _ in range(N)]
lr.sort(key = lambda x: (x[1], x[0]))
#print(lr)
dpx = [0] * N
dpy = [0] * N
dpx[0] = lr[0][1] - lr[0][0] + 1
def check(x, y):
  if x[1] >= y[0]:
    return [max(x[0], y[0]), min(x[1], y[1])]
  else: return [-1, -1]
ran = [0, 10 ** 9]
for i in range(N - 1, -1, -1):
  ran = check(lr[i], ran)
  dpy[i] = ran[1] - ran[0] + 1
  #print(lr[i])
ran = lr[0]
for i in range(N):
  ran = check(lr[i], ran)
  dpx[i] = ran[1] - ran[0] + 1
  #print(lr[i])
#print(dpx, dpy)
res = 0
for i in range(N - 1):
  res = max(dpx[i] + dpy[i + 1], res)
print(res)

Submission status: Wrong Answer

Solution code:
from operator import itemgetter

N = int(input())
L = [[int(x) for x in input().split()] for _ in range(N)]


for i in range(N):
    L[i].append(L[i][1]-L[i][0])
    
L.sort()
L.sort(key=itemgetter(2))
    

la = L[0][0]
ra = L[0][1]
lb = L[1][0]
rb = L[1][1]

for i in range(2, N):
    ta = max(0, min(ra, L[i][1])-max(la, L[i][0])+1) + rb-lb+1
    tb = max(0, min(rb, L[i][1])-max(lb, L[i][0])+1) + ra-la+1
    tc = max(0, min(ra, rb)-max(la,lb)+1) + L[i][1]-L[i][0]+1
    if max(ta, tb, tc) == ta:
        la = max(la, L[i][0])
        ra = min(ra, L[i][1])
    elif max(ta, tb, tc) == tb:
        lb = max(lb, L[i][0])
        rb = min(rb, L[i][1])
    else:
        la = max(la,lb)
        ra = min(ra, rb)
        lb = L[i][0]
        rb = L[i][1]
        
ans = max(0, ra-la+1) + max(0, rb-lb+1)


print(ans)

Submission status: Wrong Answer

Solution code:
import sys
input = sys.stdin.readline
N = int(input())
mG = [tuple(map(int, input().split())) for _ in range(N)]

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

# print(mG)###

vA1 = []
vL1 = []
vR1 = []
L = -1
R = 10**9 + 1

for l,r in mG[:-1]:
    L = max(L, l)
    R = min(R, r)
    vL1.append(L)
    vR1.append(R)
    vA1.append(R-L+1)

# print(vA1)###

vA2 = []
vL2 = []
vR2 = []
L = -1
R = 10**9 + 1

for l,r in mG[:0:-1]:
    L = max(L, l)
    R = min(R, r)
    vL2.append(L)
    vR2.append(R)
    vA2.append(R-L+1)

# print(vA2)###

print(max(a+b for a,b in zip(vA1, vA2[::-1])))

Submission status: Wrong Answer

Solution code:
N = int(input())
LR = [[]]*N
max_n = 0
min_n = 10**9
for i in range(N):
    LR[i] = list(map(int, input().split()))
    min_n = min(min_n, LR[i][0])
    max_n = max(max_n, LR[i][1])

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


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 = set1[1] - set1[0]+1 + set2[1]-set2[0]+1

print(ans1)

Submission status: Wrong Answer

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

Solution code:
n = int(input())
p = []
min_l = 1001001001
min_i = 0
min_len = 0
for i in range(n):
  l, r = map(int, input().split())
  p.append([l, r])
  if l < min_l:
    min_l = l
    min_i = i
    min_len = l - r
  elif l == min_l:
    if l - r < min_len:
      min_i = i
      min_len = l - r
max_l = 0
max_i = 0
max_len = 0
for i in range(n):
  if p[i][0] > max_l:
    max_l = p[i][0]
    max_i = i
    max_len = p[i][1] - p[i][0]
  elif p[i][0] == max_l:
    if p[i][1] - p[i][0] < max_len:
      max_i = i
      max_len = p[i][1] - p[i][0]
g1 = []
g2 = []
max_l = p[max_i][0]
max_r = p[max_i][1]
min_l = p[min_i][0]
min_r = p[min_i][1]
g1.append(p[max_i])
g2.append(p[min_i])
for i in range(n):
  if i == max_i or i == min_i:
    continue
  if min(max_l - p[i][0], 0) + min(p[i][1] - max_r, 0) < min(min_l - p[i][0], 0) + min(p[i][1] - min_r, 0):
    g2.append(p[i])
  else:
    g1.append(p[i])
g1_l = 0
g1_r = 1001001001
g2_l = 0
g2_r = 1001001001
for i in range(len(g1)):
  if g1[i][0] > g1_l:
    g1_l = g1[i][0]
  if g1[i][1] < g1_r:
    g1_r = g1[i][1]
for i in range(len(g2)):
  if g2[i][0] > g2_l:
    g2_l = g2[i][0]
  if g2[i][1] < g2_r:
    g2_r = g2[i][1]
s_g1 = max(g1_r - g1_l + 1, 0)
s_g2 = max(g2_r - g2_l + 1, 0)
ans = s_g1 + s_g2
print(ans)

Submission status: Wrong Answer

Solution code:
n=int(input())
l=[list(map(int,input().split())) for i in range(n)]
l=sorted(l)
a=[]
b=[]
c=[]

for i in range(n):
    a.append(l[i][0])
    b.append(l[i][1])

for i in range(1,n):
    x=min(b[:i])-max(a[:i])
    y=min(b[i:])-max(a[i:])

    if x>(-1) and y>(-1):
        c.append(x+y)

print(max(c)+2)

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

Submission status: Wrong Answer

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

lis.sort(key=lambda x:x[1],reverse=True)
lis.sort(key=lambda x:x[0])
  #print(left)
print(0)
exit()
min_right = [0 for i in range(n)]
now = 10 ** 9
for i in range(n):
    now = min(now,lis[i][1])
    min_right[i] = now


right_max_left = lis[-1][0]
right_min_right = lis[-1][1]
left_max_left = 0
left_min_right = 0

ans = 0

for i in range(1,n):
    left_max_left = lis[-(i+1)][0]
    left_min_right = min_right[-(i+1)]

    ans = max(ans,max(0,left_min_right-left_max_left+1)+max(0,right_min_right-right_max_left+1))
  #    print(left_max_left,left_min_right,right_max_left,right_min_right)

    right_min_right = min(right_min_right,lis[-(i+1)][1])
    right_max_left = max(right_max_left,lis[-(i+1)][0])


lis.sort(key=lambda x: x[0])
lis.sort(key=lambda x: x[1])
# print(list)

max_left = [0 for i in range(n)]
now = 0
for i in range(n):
    now = max(now, lis[i][0])
    max_left[i] = now

right_max_left = 0
right_min_right = 0
left_max_left = lis[-1][0]
left_min_right = lis[-1][1]


for i in range(1, n):
    right_min_right = lis[-(i + 1)][1]
    right_max_left = max_left[-(i + 1)]

    ans = max(ans, max(0, left_min_right - left_max_left + 1) + max(0, right_min_right - right_max_left + 1))
  #    print(left_max_left,left_min_right,right_max_left,right_min_right)


    left_min_right = min(left_min_right, lis[-(i + 1)][1])
    left_max_left = max(left_max_left, lis[-(i + 1)][0])

print(ans)

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], reverse=True)
    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()