Go: // A Way to Invite Friends(友だちの誘い方)
// http://judge.u-aizu.ac.jp/onlinejudge/description.jsp?id=2331&lang=ja
package main

import (
	"bufio"
	"fmt"
	"math"
	"os"
	"strconv"
)

const N_MAX = 1000001

func main() {
	sc := bufio.NewScanner(os.Stdin)
	sc.Split(bufio.ScanWords)

	n := nextInt(sc)
	allow_list := make([]int, N_MAX+100)
	nMax := 0

	for i := 0; i < n; i++ {
		a, b := nextInt(sc), nextInt(sc)
		allow_list[a] += 1
		allow_list[b+1] += -1
		nMax = int(math.Max(float64(nMax), float64(b)))
	}

	for i := 2; i <= N_MAX; i++ {
		allow_list[i] += allow_list[i-1]
	}

	res := 0
	for i := 2; i <= nMax; i++ {
		if i-1 <= allow_list[i] { // i人誘う時に行きたい人の人数を超えないか(自分を含めて)
			res = i - 1 // 自分は除く
		}
	}

	fmt.Println(res)
}

func nextInt(sc *bufio.Scanner) int {
	sc.Scan()
	i, err := strconv.Atoi(sc.Text())
	if err != nil {
		panic(err)
	}
	return i
}

C++: #define  _CRT_SECURE_NO_WARNINGS
#pragma comment (linker, "/STACK:526000000")

#include "bits/stdc++.h"

using namespace std;
typedef string::const_iterator State;
#define eps 1e-11L
#define MAX_MOD 1000000007LL
#define GYAKU 500000004LL

#define MOD 998244353LL
#define seg_size 262144*2LL
#define pb push_back
#define mp make_pair
typedef long long ll;
#define REP(a,b) for(long long (a) = 0;(a) < (b);++(a))
#define ALL(x) (x).begin(),(x).end()

unsigned long xor128() {
	static unsigned long x = 123456789, y = 362436069, z = 521288629, w = time(NULL);
	unsigned long t = (x ^ (x << 11));
	x = y; y = z; z = w;
	return (w = (w ^ (w >> 19)) ^ (t ^ (t >> 8)));
}

void init() {
	//iostream::sync_with_stdio(false);
	cout << fixed << setprecision(20);
}

#define int ll
void solve() {
	while (true) {
		int n;
		cin >> n;
		vector<pair<int, int>> inputs;
		REP(i, n) {
			int a, b;
			cin >> a >> b;
			inputs.push_back(mp(a, b));
		}
		sort(ALL(inputs));
		int ans = 0;
		priority_queue<int,vector<int>,greater<int>> next;
		next.push(1e9);
		REP(i, inputs.size()) {
			next.push(inputs[i].second);
			while (next.size() > next.top()) {
				next.pop();
			}
			if (next.size() >= inputs[i].first) {
				ans = max(ans, (long long)next.size() - 1LL);
			}
		}
		cout << ans << endl;
		return;
	}
}
#undef int
int main() {
	init();
	solve();
}

C++: #include <iostream>
using namespace std;

int main(void){
    int n;
    cin >> n;
    int a[101010] = {};
    int l, r;
    for (int i = 0; i < n; i++) {
        cin >> l >> r;
        a[l]++;
        a[r+1]--;
    }
    for (int i = 0; i < n; i++) a[i+1] += a[i];
    
    int ans = 0;
    for (int i = 2; i < n+1; i++) {
        if (a[i]+1 >= i) {
            ans = max(ans, i);
        }
    }
    if (ans) ans--;
    cout << ans << endl;
}

C++: #include <iostream>

struct Range {
	int beg;
	int end;
}; 

int main(void){
	int n;
	std::cin >> n;
	Range* ps = new Range[n];

	for(int i=0; i<n; i++) std::cin >> ps[i].beg >> ps[i].end;

//	i???"?????????"??????????????°
	int mps = 1;
	for(int i=1; i<=n; i++){
		int cps = 0;
		bool f = false;
		for(int j=0; j<n; j++){
			if(i >= ps[j].beg && i <= ps[j].end){
				cps ++;
				if(cps >= i-1){
					f = true;
					break;
				}
			}
		}
		if(f) mps = i;
	}

	std::cout << (mps-1) << '\n';

//	system("pause");
	return 0;
}

C++: #include<bits/stdc++.h>
using namespace std;

int data[100001][100002] = {0};

int main(){
	int n;
	cin >> n;

	//init
	/*
	for(int i = 0; i < 100001; i++){
		for(int j = 0; j < 100002; j++) data[i][j] = 0;
	}*/

	//input
	for(int i = 1; i <= n; i++){
		int r, l;
		cin >> r >> l;
		for(int j = r; j <= l; j++) data[i][j] = 1;
	}

	vector<int> v;
	for(int i = 1; i <= n; i++){
		int ok = 0;
		for(int j = 1; j <= n; j++){
			if(data[j][i]) ok++;
		}
		if(ok + 1 >= i) v.push_back(i);
	}

	/*
	for(int i = 1; i <= n; i++){
		for(int j = 1; j <= n; j++) cout << data[i][j] << " ";
		cout << endl;
	}
	for(int i = 0; i < (int)v.size(); i++) cout << v[i] << endl;
	*/

	if(v.size()){
		sort(v.begin(), v.end());
		cout << v[v.size() - 1] - 1 << endl;
	}
	else cout << 0 << endl;

	return 0;
}

C++: #include<bits/stdc++.h>
using namespace std;

#define int long long
typedef pair<int,int>pint;
typedef vector<int>vint;
typedef vector<pint>vpint;
#define pb push_back
#define mp make_pair
#define fi first
#define se second
#define all(v) (v).begin(),(v).end()
#define rep(i,n) for(int i=0;i<(n);i++)
#define reps(i,f,n) for(int i=(f);i<(n);i++)
#define each(it,v) for(__typeof((v).begin()) it=(v).begin();it!=(v).end();it++)
template<class T,class U>inline void chmin(T &t,U f){if(t>f)t=f;}
template<class T,class U>inline void chmax(T &t,U f){if(t<f)t=f;}

int N;
int S[111111];

signed main(){
    cin>>N;
    rep(i,N){
        int a,b;
        cin>>a>>b;
        b++;
        S[a]++;S[b]--;
    }
    rep(i,100100)S[i+1]+=S[i];

    for(int i=N+1;i>=1;i--){
        if(S[i]>=i-1){
            cout<<i-1<<endl;
            return 0;
        }
    }
    return 0;
}

C++: #include<iostream>
#include<algorithm>
using namespace std;
int a[100001],b[100001];
int main(){
	int n;
	cin>>n;
	for(int i=0;i<n;i++)
		cin>>a[i]>>b[i];
	sort(a,a+n);
	sort(b,b+n);
	b[n]=-1;
	int ans=1,top=0;
	int k=1;
	for(int i=0;i<n;i++){
		k++;
		while(b[top]==k)
			top++,k--;
		if(a[i]<=k)
			ans=max(ans,k);
	}
	cout<<ans-1<<endl;
	return 0;
}

C++: #include <bits/stdc++.h>
#define int long long
#define double long double
#define INF 1e18
using namespace std;
int x[100100];
signed main() {
  for (int i = 0; i < 100100; i++) x[i]=0;
  int N; cin >> N;
  for (int i = 0; i < N; i++) {
    int a,b; cin >> a >> b;
    if (a > N) continue;
    for (int j = a; j <= min(b,N); j++) {
      x[j]++;
    }
  }
  int ans = 0;
  for (int i = 0; i <= N; i++) {
    if (x[i] >= i) ans=max(ans,x[i]);
  }
  ans = max(0LL, ans-1);
  cout << ans << endl;
}

C++: #include <iostream>
#include <algorithm>

using namespace std;

typedef pair<int,int> pii;
pii friends[100001];
int dp[100001];
int N;

bool check(int m){
	m++;
	int cnt=0;
	for(int i=0;i<N;i++)
		if(friends[i].first<=m&&friends[i].second>=m)cnt++;
	return (cnt>=(m-1));
}

int main(){

	cin>>N;
	for(int i=0;i<N;i++)cin>>friends[i].first>>friends[i].second;
	int lb=0;
	int ub=N+1;
	while(ub-lb>1){
		int mid=(ub+lb)/2;
		if(check(mid))lb=mid;
		else ub=mid;
	}
	cout<<lb<<endl;

	return 0;
}

C++: #include<bits/stdc++.h>
#define rep(i,n) for(int i=0;i<n;i++)
#define INF 10000000
using namespace std;
typedef pair<int,int> P;
int main(){
    vector<int> allow(100001,0);
    int n;
    cin >> n;
    rep(i,n){
        int a,b;
        cin >> a >> b;
        allow[a-1]++;
        allow[b]--;
    }
    int ans;
    for(int i=0; i<=n; i++){
        if(i) allow[i] += allow[i-1];
        if(allow[i] >= i) ans = i;
    }
    cout << ans << endl;
    return 0;
}