Problem:


Problem B: 友だちの誘い方

明日から、待ちに待った夏休みが始まります。なのでわたしは、友だちを誘って、海に遊びに行くこ
とに決めました。

けれども、わたしの友だちには、恥ずかしがりやさんが多いです。その人たちは、あまり多くの人が
いっしょに来ると知ったら、きっと嫌がるでしょう。

ほかにも、わたしの友だちには、目立ちたがりやさんも多いです。その人たちは、いっしょに来る人
があまり多くないと知れば、きっと嫌がるでしょう。

それと、わたしの友だちには、いつもは目立ちたがりやなのに実は恥ずかしがりやさんな人もいま
す。その人たちは、いっしょに来る人が多すぎても少なすぎても、きっと嫌がるでしょう。

こういうのは、大勢で行った方が楽しいはずです。だからわたしは、できるだけたくさんの友だちを
誘いたいと思っています。けれども、嫌がる友だちを無理やり連れていくのはよくありません。

いったい、わたしは最大で何人の友だちを誘うことができるのでしょうか。

わたしは、こういう頭を使いそうな問題が大の苦手です。なので、あなたにお願いがあります。もし
よろしければ、わたしの代わりにこの問題を解いていただけないでしょうか。いえ、決して無理にと
は言いません。けれど、もし解いていただるのでしたら、わたしはとても嬉しいです。

Input

N

a1 b1

a2 b2

.

.

.

aN bN

入力の１行目には、整数N（1 ≤ N ≤ 100,000）が書かれている。これは、友だちの数をあらわす。

続くN行には、整数 ai と整数 bi（2 ≤ ai ≤ bi ≤ 100,001）が、空白区切りで書かれている。１＋ i 行目に書かれた整数 ai とbi は、i 番目の友だちは海に行く人数が ai 人以上 bi 人以下でないと嫌がることをあらわす。海に行く人数には「わたし」も含まれることに注意せよ。

Output

嫌がる友だちが出ないように、海に誘うことのできる友だちの最大人数を出力せよ。

Sample Input 1

4
2 5
4 7
2 4
3 6

Sample output 1

3

Sample Input 2

5
8 100001
7 100001
12 100001
8 100001
3 100001

Sample output 2

0

Sample Input 3

6
2 9
4 8
6 7
6 6
5 7
2 100001

Sample output 3

5



Sample input:
4
2 5
4 7
2 4
3 6


Sample output:
3

Submission status: Wrong Answer

Solution code:
l=[list(map(int,input().split())) for _ in range(int(input()))]
l=sorted(l,key=lambda x:x[0])
m=[0 for i in range(100002)]
c=1
for i in l:
    if i[0]<=c+1:
        m[i[0]]+=1
        m[i[1]]-=1
        c+=1
for i in range(2,100001):
    m[i]+=m[i-1]
print(max(m))

Submission status: Wrong Answer

Solution code:
l=[list(map(int,input().split())) for _ in range(int(input()))]
l=sorted(l,key=lambda x:x[0])
m=[0 for i in range(100002)]
c=1
for i in l:
    if i[0]<=c+1:
        m[i[0]]+=1
        m[i[1]]-=1
        c+=1
for i in range(2,100002):
    m[i]+=m[i-1]
print(max(m))

Submission status: Wrong Answer

Solution code:
l=[list(map(int,input().split())) for _ in range(int(input()))]
l=sorted(l,key=lambda x:x[0])
m=[0 for i in range(100003)]
c=1
for i in l:
    if i[0]<=c+1 and c+1<=i[1]:
        if i[0]!=i[1]:
            m[i[0]]+=1
            m[i[1]]-=1
        else:
            m[i[0]]+=1
        c+=1
for i in range(2,100003):
    m[i]+=m[i-1]
print(max(m))

Submission status: Wrong Answer

Solution code:
import bisect

N=int(raw_input())
tlist=[]
for i in range(N):
    [a,b]=[int(x) for x in raw_input().split()]
    tlist.append([a,b])
tlist.sort(key=lambda x:x[1])
blist=[x[1] for x in tlist]
#case N
for k in range(N,0,-1):
    alist=sorted([x[0] for x in tlist[bisect.bisect(blist,k+1):]])
    alist=alist[:bisect.bisect(alist,k+1)]
    if len(alist)==k:
        print k
        break
else:
    print 0

Submission status: Wrong Answer

Solution code:
import bisect

N=int(raw_input())
tlist=[]
for i in range(N):
    [a,b]=[int(x) for x in raw_input().split()]
    tlist.append([a,b])
tlist.sort(key=lambda x:x[1])
blist=[x[1] for x in tlist]
#case N
for k in range(N,0,-1):
    alist=sorted([x[0] for x in tlist[bisect.bisect_left(blist,k+1):]])
    alist=alist[:bisect.bisect(alist,k+1)]
    if len(alist)==k:
        print k
        break
else:
    print 0

Submission status: Wrong Answer

Solution code:
import bisect

N=int(raw_input())
tlist=[]
for i in range(N):
    [a,b]=[int(x) for x in raw_input().split()]
    tlist.append([a,b])
tlist.sort(key=lambda x:x[1])
blist=[x[1] for x in tlist]
#case N
bb=bisect.bisect_left(blist,N+1)
alist=sorted([x[0] for x in tlist[bb:]])
tlist=tlist[bb:]
blist=blist[bb:]
alist=alist[:bisect.bisect(alist,N+1)]
if len(alist)>=N:
    print N
else:
    for k in range(N-1,0,-1):
        bb=bisect.bisect_left(blist,k+1)
        newa=[x[0] for x in tlist[bb:]]
        tlist=tlist[bb:]
        blist=blist[bb:]
        for a in newa:
            bisect.insort_left(alist,a)
        alist=alist[:bisect.bisect(alist,k+1)]
        if len(alist)>=k:
            print k
            break
    else:
        print 0

Submission status: Wrong Answer

Solution code:
N=int(input())

friendlist=[0 for i in range(N+1)]

for i in range(N):
    a,b=[int(j) for j in input().split(" ")]
    for k in range(a,min(N+1,b+1)):
        friendlist[k]+=1

k=N
i=0
while k>=0:
    if friendlist[k]>=k-1:
        i=k-1
        break
    k-=1

print(i)

Submission status: Wrong Answer

Solution code:
N=int(input())

friendlist=[0 for i in range(N+2)]


for i in range(N):
    a,b=[int(j) for j in input().split(" ")]
    for k in range(a,min(b+1,N+1)):
        friendlist[k]+=1


#ab=[[int(i) for i in input().split(" ")] for j in range(N)]

#print(ab)

k=N+1
i=0
while k>=0:
    if friendlist[k]>=k-1:
        i=k-1
        break
    k-=1

print(i)

Submission status: Wrong Answer

Solution code:


N=int(input())

friendlist=[0 for i in range(N+2)]

"""
for i in range(N):
    a,b=[int(j) for j in input().split(" ")]
    for k in range(a,min(b+1,N+2)):
        friendlist[k]+=1
"""


ab=[[int(i) for i in input().split(" ")] for j in range(N)]
a=[ab[i][0] for i in range(N)]
b=[ab[i][1] for i in range(N)]

a.sort()
b.sort()



for i in range(1,N):
    for j in range(a[i-1],min(a[i],N+2)):
        friendlist[j]+=i
for i in range(a[N-1],N+2):
    friendlist[i]+=N

for i in range(1,N):
    for j in range(b[i-1]+1,min(b[i]+1,N+2)):
        friendlist[j]-=j
for i in range(b[N-1]+1,N+2):
    friendlist[i]-=N
        


#"""

k=N+1
i=0
while k>=0:
    if friendlist[k]>=k-1:
        i=k-1
        break
    k-=1

print(i)

Submission status: Wrong Answer

Solution code:
def appoint(x, l, h):
    if l <= x <= h:
        return 1
    else:
        return 0


N = int(raw_input())
friends = [list(map(int,raw_input().split())) for i in range(N)]
min_group = min(x[0] for x in friends)
max_group = max(x[0] for x in friends)

t = 0
ans = 1
for i in range(min_group, max_group):
    z = sum([appoint(i + 1, lo, hi) for lo, hi in friends])
    if i + 1 == z:
        ans = z
        t = 1
    if t == 1 and i + 1 != z:
        break

ans = ans + 1
print ans

Submission status: Wrong Answer

Solution code:
n = int(input())
l = [0 for i in range(n+1)]
for i in range(n):
  [a,b] = list(map(int,input().split()))
  if a <= n:
    l[a] += 1
    if b <= n:
      l[b] -= 1
ans = 0
for i in range(n+1):
  if i != 0:
    l[i] = l[i-1] + l[i]
  if l[i] >= i-1:
    ans = i-1
print(ans)

Submission status: Wrong Answer

Solution code:
import heapq
n=int(input())
abs=sorted([list(map(int,input().split())) for _ in range(n)])
s=0
best=0
heap=[]
for k in range(1,n+1):
    while(s < n and abs[s][0] <= k+1):
        heapq.heappush(heap, -abs[s][1])
        s+=1
    while(len(heap) > 0 and -heap[0] <= k):
        heapq.heappop(heap)
    if(len(heap) >= k):
        best = k
print(best)

Submission status: Wrong Answer

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

diff = [0] * 100001
for k in l :
    diff[k[0] - 1] += 1
    diff[k[1]] -= 1

hist = [0,0]
for i in range(2, N + 1) :
    hist.append(hist[-1] + diff[i-1])

print(max([y for (x,y) in zip(hist, range(N + 1)) if x + 1 >= y]))

Submission status: Wrong Answer

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

diff = [0] * 100002
for k in l :
    diff[k[0] - 1] += 1
    diff[k[1]] -= 1

hist = [0,0]
for i in range(2, N + 1) :
    hist.append(hist[-1] + diff[i-1])

print(max([0] + [y - 1 for (x,y) in zip(hist, range(N + 1)) if x + 1 >= y]))

Submission status: Wrong Answer

Solution code:
N = int(input())
MAXP = 100010

imos = [0 for i in range(MAXP)]
for i in range(N):
    a,b = map(int,input().split())
    imos[a-1] += 1
    imos[b] -= 1
nums = [0]
for im in imos:
    nums.append(nums[-1] + im)

for i,p in enumerate(reversed(nums)):
    n = MAXP - 1 - i
    if p-1 >= n and n >= 0:
        print(n)
        exit()
print(0)

Submission status: Wrong Answer

Solution code:
N = int(input())

t = [0 for i in range(100002)]

for i in range(N):
    a,b = [int(i) for i in input().split(' ')]
    t[a-1] +=1
    t[b] -= 1

ans = 0

for i in range(1,100001):
    t[i] += t[i-1]
    if (t[i] > i):
        ans = i

print(ans)

Submission status: Wrong Answer

Solution code:
N = int(input())

t = [0 for i in range(100002)]

for i in range(N):
    a,b = [int(i) for i in input().split(' ')]
    t[a-1] +=1
    t[b] -= 1

ans = 0

for i in range(0,100001):
    t[i] += t[i-1]
    if (t[i] >= i):
        ans = i

print(ans)

Submission status: Accepted

Solution code:
from itertools import accumulate
n = int(input())
lst = [0] * 100002
for _ in range(n):
  a, b = map(int, input().split())
  lst[a - 1] += 1
  lst[b] -= 1
lst = list(accumulate(lst))
for i in range(n, -1, -1):
  if i <= lst[i]:
    print(i)
    break

Submission status: Accepted

Solution code:
memo = [0]*100010
n = input()
for i in xrange(n):
    a, b = map(int, raw_input().split())
    memo[a] += 1
    memo[b+1] -= 1
ans = 0
su = 0
for i in xrange(100005):
    su += memo[i]
    if su+1>=i:
        ans = max(ans, i-1)
print ans

Submission status: Accepted

Solution code:
import sys

class Imos1D:
    def __init__(self,sz):
        self.__seq = [0] * sz

    def update(self, a, b): # [a,b)
        self.__seq[a] += 1
        self.__seq[b] -= 1

    def calc(self):
        for i in range(len(self.__seq) - 1):
            self.__seq[i + 1] += self.__seq[i]

    def get(self,i):
        return self.__seq[i]

def main():
    n = int(input())
    imos = Imos1D(1000000 + 10)

    for i in range(n):
        a, b = map(int, sys.stdin.readline().split())
        imos.update(a, b + 1)

    imos.calc()

    # solve
    ans = 1
    for i in range(n + 2):
        if imos.get(i) >= i - 1:
            ans = max(ans, i)

    print(ans - 1) # ???????????????


if __name__ == '__main__':
    main()

Submission status: Accepted

Solution code:
N = int(input())
MAX_N = int(1e5) + 2
acc = [0] * (MAX_N + 1)
acc[0] += 1
acc[MAX_N] -= 1
for _ in range(N):
    a, b = map(int, input().split())
    acc[a] += 1
    acc[b + 1] -= 1
for i in range(MAX_N):
    acc[i + 1] += acc[i]
for i in reversed(range(MAX_N)):
    if acc[i] >= i:
        print(i - 1)
        break

Submission status: Accepted

Solution code:


N=int(input())

friendlist=[0 for i in range(N+2)]

"""
for i in range(N):
    a,b=[int(j) for j in input().split(" ")]
    for k in range(a,min(b+1,N+2)):
        friendlist[k]+=1
"""


ab=[[int(i) for i in input().split(" ")] for j in range(N)]
a=[ab[i][0] for i in range(N)]
b=[ab[i][1] for i in range(N)]

a.sort()
b.sort()



for i in range(1,N):
    for j in range(a[i-1],min(a[i],N+2)):
        friendlist[j]+=i
for i in range(a[N-1],N+2):
    friendlist[i]+=N

for i in range(1,N):
    for j in range(b[i-1]+1,min(b[i]+1,N+2)):
        friendlist[j]-=i
for i in range(b[N-1]+1,N+2):
    friendlist[i]-=N
        


#"""

k=N+1
i=0
while k>=0:
    if friendlist[k]>=k-1:
        i=k-1
        break
    k-=1

print(i)

Submission status: Accepted

Solution code:
n = int(input())
l = [0 for i in range(n+2)]
for i in range(n):
  [a,b] = list(map(int,input().split()))
  if a <= n+1:
    l[a] += 1
    if b+1 <= n+1:
      l[b+1] -= 1
ans = 0
for i in range(n+2):
  if i != 0:
    l[i] = l[i-1] + l[i]
  if l[i] >= i-1:
    ans = i-1
print(ans)

Submission status: Accepted

Solution code:
import heapq
n=int(input())
abs=sorted([list(map(int,input().split())) for _ in range(n)])
s=0
best=0
heap=[]
for k in range(1,n+1):
    while(s < n and abs[s][0] <= k+1):
        heapq.heappush(heap, abs[s][1])
        s+=1
    while(len(heap) > 0 and heap[0] <= k):
        heapq.heappop(heap)
    if(len(heap) >= k):
        best = k
print(best)

Submission status: Accepted

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

diff = [0] * 100002
for k in l :
    diff[k[0] - 1] += 1
    diff[k[1]] -= 1

hist = [0,0]
for i in range(2, N + 2) :
    hist.append(hist[-1] + diff[i-1])

print(max([0] + [y - 1 for (x,y) in zip(hist, range(N + 2)) if x + 1 >= y]))

Submission status: Accepted

Solution code:
N = int(input())
MAXP = 100010

imos = [0 for i in range(MAXP)]
for i in range(N):
    a,b = map(int,input().split())
    imos[a-1] += 1
    imos[b] -= 1
nums = [0]
for im in imos:
    nums.append(nums[-1] + im)

for i,p in enumerate(reversed(nums)):
    n = MAXP - 1 - i
    if n == 0: break
    if p >= n:
        print(n)
        exit()
print(0)

Submission status: Accepted

Solution code:
import math,string,itertools,fractions,heapq,collections,re,array,bisect,sys,random,time,copy,functools

sys.setrecursionlimit(10**7)
inf = 10**20
eps = 1.0 / 10**10
mod = 998244353

def LI(): return [int(x) for x in sys.stdin.readline().split()]
def LI_(): return [int(x)-1 for x in sys.stdin.readline().split()]
def LF(): return [float(x) for x in sys.stdin.readline().split()]
def LS(): return sys.stdin.readline().split()
def I(): return int(sys.stdin.readline())
def F(): return float(sys.stdin.readline())
def S(): return input()
def pf(s): return print(s, flush=True)


def main():
    rr = []

    while True:
        n = I()
        aa = [LI() for _ in range(n)]
        t = [0] * 100003
        for a,b in aa:
            t[a] += 1
            t[b+1] -= 1
        s = 0
        r = 0
        for i in range(100002):
            s += t[i]
            if i-1 <= s:
                r = i - 1
        rr.append(r)
        break

    return '\n'.join(map(str, rr))


print(main())

Submission status: Accepted

Solution code:
N = int(input())

t = [0 for i in range(100002)]

for i in range(N):
    a,b = [int(i) for i in input().split(' ')]
    t[a-1] +=1
    t[b] -= 1

ans = 0
#print(t)
for i in range(1,100002):
    t[i] += t[i-1]
    if (t[i] >= i):
        ans = i
#print(t)
print(ans)

Submission status: Accepted

Solution code:
N = int(input())
R = [0 for i in range(100003)]
for i in range(N):
    a, b = map(int, input().split())
    R[a] += 1
    R[b+1] -= 1
x = 1
y = 1
for i in range(100003):
    x += R[i]
    if x >= i:
        y = max(i, y)
print(y-1)

Submission status: Accepted

Solution code:
n=int(input())
t=[0]*1000002

for i in range(n):
    a,b=map(int,input().split())
    t[a-1]+=1
    t[b]-=1
    
ans=0
for i in range(1000002):
    t[i]+=t[i-1]
    if i<=t[i]:
        ans=i

print(ans)

Submission status: Accepted

Solution code:
n = int(input())
a = [0] * (10**5 + 2)
for i in range(n) :
    mi, ma = map(int, input().split())
    a[mi-1] += 1
    a[ma] -= 1
for i in range(n) :
    a[i+1] += a[i]
    
ans = 0
for i in range(n, 0, -1) :
    if a[i] >= i :
        ans = i
        break
print(ans)

Submission status: Accepted

Solution code:
n=int(input())
l=[0 for i in range(100003)]
for i in range(n):
    a,b=map(int,input().split())
    l[a]+=1
    l[b+1]-=1
su=1
res=1
for i in range(100003):
    su+=l[i]
    if su>=i:
        res=max(i,res)
print(res-1)

Submission status: Accepted

Solution code:
from collections import defaultdict,deque
import sys,heapq,bisect,math,itertools,string,queue,copy,time
sys.setrecursionlimit(10**8)
INF = float('inf')
mod = 10**9+7
eps = 10**-7
def inp(): return int(input())
def inpl(): return list(map(int, input().split()))
def inpl_str(): return list(input().split())



N = inp()
MAX = 100005
imos = [0]*MAX
for _ in range(N):
    a,b = inpl()
    imos[a] += 1
    imos[b+1] -= 1

for i in range(1,MAX):
    imos[i] += imos[i-1]

#print(imos[:10])
ans = 0
for i in range(MAX):
    if imos[i] >= i-1:
        ans = max(ans,i-1)
print(ans)

Submission status: Accepted

Solution code:
def main():
    N = int(input())
    ab = [list(map(int, input().split())) for i in range(N)]

    imos = [0]*(N+3)
    for a, b in ab:
        imos[min(a,N+2)] += 1
        imos[min(b+1,N+2)] -= 1
    for i in range(N+1):
        imos[i+1] += imos[i]
    #print(imos)

    ans = 0
    for i, imo in enumerate(imos):
        if (imo+1 >= i):
            ans = i-1
    print(ans)

main()

Submission status: Accepted

Solution code:
n = int(input())
li = [0] * 100003
ans = 0

for i in range(n):
    a,b = map(int,input().split())
    li[a] += 1
    li[b + 1] -= 1

for i in range(1,len(li)):
    li[i] += li[i-1]
    if i -1 <= li[i]:
        ans = i-1
print(ans)

Submission status: Accepted

Solution code:

N = int(input())
a = []
b = []

for i in range(N):

    an , bn = map(int,input().split())

    a.append(an)
    b.append(bn)

a.sort()
b.sort()

a.append(1000000)
b.append(1000000)

lis = [0]

for i in range(N+1):

    i += 1
    now = lis[i - 1]

    while a[0] == i:

        now += 1
        del a[0]

    while b[0] == i-1:

        now -= 1
        del b[0]

    lis.append(now)

ans = 0
#print (lis)
for i in range(len(lis)):

    if i <= lis[i]+1 and ans < i:
        ans = i

print (ans - 1)

Submission status: Accepted

Solution code:
N = int(input())
a=[]
p = 0
for i in range(N):
    q=[int(j) for j in input().split()]
    a.append([q[0]-1,q[1]])
    p = max(p, q[1])

table=[0]*(p+1)
for i in range(len(a)):
    table[a[i][0]]+=1
    table[a[i][1]]-=1

for i in range(1,p+1):
    table[i]+=table[i-1]

ans=0
for i, val in enumerate(table):
    if val >= i and i <= N:
        ans = max(i, ans)

print(ans)

Submission status: Accepted

Solution code:
#!/usr/bin/env python3
n = int(input())

# imos[k]: わたしがちょうどk人誘うときに来れる最大人数
imos = [0] * (10**5 + 2)
for _ in range(n):
    a, b = map(int, input().split())
    imos[a - 1] += 1
    imos[b] -= 1
for i in range(n):
    imos[i + 1] += imos[i]

ans = 0
for i in range(n + 1):
    if imos[i] >= i:
        ans = max(ans, i)
print(ans)

Submission status: Accepted

Solution code:
n=int(input())
l=[]
q=0
for i in range(n):
    p=[int(j) for j in input().split()]
    q=max(q,p[1]+1)
    l.append([p[0],p[1]+1])
a=[]
table=[0]*(q+1)
for i in range(n):
    table[l[i][0]]+=1
    table[l[i][1]]-=1
for i in range(1,q+1):
    table[i]+=table[i-1]
ans=0
for i in range(len(table)):
    if i<=table[i]+1 and i<=n+1:
        ans=max(ans,i)



print(ans-1)

Submission status: Accepted

Solution code:
#!/usr/bin/env python3
n = int(input())
li = [0] * (10**5 + 3)
for _ in range(n):
    a, b = map(int, input().split())
    li[a] += 1
    li[b + 1] -= 1
for i in range(n + 1):
    li[i + 1] += li[i]
ans = 0
for i in range(1, n + 2):
    if i - 1 <= li[i]:
        ans = max(ans, i - 1)
print(ans)

Submission status: Accepted

Solution code:
N = int(input())
import sys
nn = [0 for _ in range(10**5+3)]
for _ in range(N):
  a,b = map(int,input().split())
  nn[a] += 1
  nn[b+1] -= 1
ans = 0
sss = 1
for i in range(len(nn)):
  sss += nn[i]
  if i <= sss:
    ans = i
print(ans-1)

Submission status: Accepted

Solution code:
N = int(input())
A = [0] * 101010
for i in range(N):
    a, b = map(int, input().split())
    A[a] += 1
    A[b+1] -= 1
ans = 1
cuma = A[0]
for i, a in enumerate(A[1:], 1):
    cuma += a
    if cuma >= i-1:
        ans = i
print(ans-1)

Submission status: Accepted

Solution code:
n = int(input())
arr = [0]*(10**6+2)
for _ in range(n):
    a,b = map(int,input().split())
    a,b = a-1,b-1
    b = min(b,n)
    arr[a] += 1
    if b+1<=n:
        arr[b+1] -= 1
for i in range(n):
    arr[i+1] += arr[i]
res = -float('inf')
for i,j in enumerate(arr[:n+1][::-1]):
    if (n-i)<=j:
        res = max(res,n-i)
print(max(res,0))

Submission status: Accepted

Solution code:
import itertools
n=int(input())
table=[0 for i in range(100003)]
table[0]=1
for i in range(n):
    a,b=map(int,input().split())
    table[a]+=1
    table[b+1]-=1
table=list(itertools.accumulate(table))
for i in range(len(table)):
    if table[i]>=i:
        ans=i
print(ans-1)

Submission status: Runtime Error

Solution code:
import bisect

N=int(raw_input())
tlist=[]
for i in range(N):
    [a,b]=[int(x) for x in raw_input().split()]
    tlist.append([a,b])
tlist.sort(key=lambda x:x[1])
blist=[x[1] for x in tlist]
#case N
for k in range(N,0,-1):
    alist=sorted([x[0] for x in tlist[bisect.bisect_left(blist,k+1):]])
    alist=alist[:bisect.bisect(alist,k+1)]
    if len(alist)>=k:
        print k
        break
else:
    print 0

Submission status: Runtime Error

Solution code:
import bisect

N=int(raw_input())
tlist=[]
for i in range(N):
    [a,b]=[int(x) for x in raw_input().split()]
    tlist.append([a,b])
tlist.sort(key=lambda x:x[1])
blist=[x[1] for x in tlist]
#case N
bb=bisect.bisect_left(blist,N+1)
alist=sorted([x[0] for x in tlist[bb:]])
tlist=tlist[:bb]
blist=blist[:bb]
alist=alist[:bisect.bisect(alist,N+1)]
if len(alist)>=N:
    print N
else:
    for k in range(N-1,0,-1):
        bb=bisect.bisect_left(blist,k+1)
        newa=[x[0] for x in tlist[bb:]]
        tlist=tlist[:bb]
        blist=blist[:bb]
        for a in newa:
            bisect.insort_left(alist,a)
        alist=alist[:bisect.bisect(alist,k+1)]
        if len(alist)>=k:
            print k
            break
    else:
        print 0

Submission status: Runtime Error

Solution code:
N = int(input())
P = []
for i in range(N):
    a, b = map(int, input().split())
    P.append((a, b))

Fmaxim = 0
for i in range(N+1):
    Pcount = 1
    for a, b in P:
        if a <= i+1 <= b:
            Pcount += 1
    if Pcount >= i+1:
        Fmaxim = max(Fmaxim, i)

print(Fmaxim)

Submission status: Runtime Error

Solution code:
N = int(input())
P = []
for i in range(N):
    a, b = map(int, input().split())
    if a <= N+1:
        P.append((a, b))
if len(P) > 0:
    for i in range(len(P), -1, -1):
        Pcount = 1
        for a, b in P:
            if a <= i+1 <= b:
                Pcount += 1
        if Pcount >= i+1:
            print(i)
            break
else:
    print('0')

Submission status: Runtime Error

Solution code:
N = int(input())
P = []
bmax = 0
for i in range(N):
    a, b = map(int, input().split())
    bmax = max(bmax, b)
    if a <= N+1:
        P.append((a, b))
if len(P) > 0:
    for i in range(min(len(P), bmax), -1, -1):
        Pcount = 1
        for a, b in P:
            if a <= i+1 <= b:
                Pcount += 1
        if Pcount >= i+1:
            print(i)
            break
else:
    print('0')

Submission status: Runtime Error

Solution code:
N=int(input())

friendlist=[0 for i in range(N+2)]

for i in range(N):
    a,b=[int(j) for j in input().split(" ")]
    for k in range(a,min(N+2,b+1)):
        friendlist[k]+=1

k=N+1
i=0
while k>=0:
    if friendlist[k]>=k-1:
        i=k-1
        break
    k-=1

print(i)

Submission status: Runtime Error

Solution code:
N=int(input())

friendlist=[0 for i in range(N+2)]


for i in range(N):
    a,b=[int(j) for j in input().split(" ")]
    for k in range(a,min(b+1,N+2)):
        friendlist[k]+=1


#ab=[[int(i) for i in input().split(" ")] for j in range(N)]

#print(ab)

k=N+1
i=0
while k>=0:
    if friendlist[k]>=k-1:
        i=k-1
        break
    k-=1

print(i)

Submission status: Runtime Error

Solution code:
def appoint(x, l, h):
    if l <= x <= h:
        return 1
    else:
        return 0


N = int(input())
friends = [list(map(int, input().split())) for i in range(N)]
min_group = min(x[0] for x in friends)
max_group = max(x[0] for x in friends)

t = 0
ans = 1
for i in range(min_group, max_group):
    z = sum([appoint(i + 1, lo, hi) for lo, hi in friends])
    if i + 1 == z:
        ans = z
        t = 1
    if t == 1 and i + 1 != z:
        break

print(ans - 1)

Submission status: Runtime Error

Solution code:
def appoint(x, l, h):
        if l <= x <= h:
            return 1
        else:
            return 0
     
     
    N = int(raw_input())
    friends = [list(map(int, raw_input().split())) for i in range(N)]
    min_group = min(x[0] for x in friends)
    max_group = max(x[0] for x in friends)
     
    t = 0
    ans = 1
    for i in range(min_group, max_group):
        z = sum([appoint(i + 1, lo, hi) for lo, hi in friends])
        if i + 1 == z:
            ans = z
            t = 1
        if t == 1 and i + 1 != z:
            break
     
    ans = ans - 1
    print ans

Submission status: Runtime Error

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

diff = [0] * 100001
for k in l :
    diff[k[0] - 1] += 1
    diff[k[1]] -= 1

hist = [0,0]
for i in range(2, N + 1) :
    hist.append(hist[-1] + diff[i-1])

print(max([y - 1 for (x,y) in zip(hist, range(N + 1)) if x + 1 >= y]))

Submission status: Runtime Error

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

diff = [0] * 100001
for k in l :
    diff[k[0] - 1] += 1
    diff[k[1]] -= 1

hist = [0,0]
for i in range(2, N + 1) :
    hist.append(hist[-1] + diff[i-1])

print(max([0] + [y - 1 for (x,y) in zip(hist, range(N + 1)) if x + 1 >= y]))

Submission status: Runtime Error

Solution code:
N = int(input())

t = [0 for i in range(100001)]

for i in range(N):
    a,b = [int(i) for i in input().split(' ')]
    t[a-1] +=1
    t[b] -= 1

ans = 0

for i in range(1,100001):
    t[i] += t[i-1]
    if (t[i] > i):
        ans = i

print(ans)